Skip to content

Commit fe9c028

Browse files
authored
feat(fmt): use dedicated cache directory (#229)
1 parent faee251 commit fe9c028

7 files changed

Lines changed: 13 additions & 11 deletions

File tree

packages/rstack/src/fmt/cacheStore.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { randomUUID } from 'node:crypto';
22
import { mkdir, readFile, rename, rm, writeFile } from 'node:fs/promises';
33
import path from 'node:path';
44

5-
const fmtCacheFileName = 'fmt-v1.json';
5+
const fmtCacheFileName = 'v1.json';
66
const fmtCacheVersion = 1;
77

88
type FmtCacheState = 'clean' | 'dirty';

packages/rstack/src/fmt/cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ const runFmtCLI = async (args: string[]): Promise<void> => {
301301
const cacheDir = await ensureProjectCacheDir(config.rootPath);
302302
if (cacheDir.status === 'available') {
303303
cacheContext = {
304-
filePath: path.join(cacheDir.path, fmtCacheFileName),
304+
filePath: path.join(cacheDir.path, 'fmt', fmtCacheFileName),
305305
rootPath: config.rootPath,
306306
};
307307
}

packages/rstack/tests/cli/fmt/index.test.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,17 +170,19 @@ test.each([
170170
['list-different', ['--list-different']],
171171
] as const)('uses the default cache in %s mode', (_, args) => {
172172
writeProjectFile('index.ts', 'const value = 1;\n');
173+
writeProjectFile('.rstack/cache/fmt-v1.json', 'legacy');
173174

174175
const result = runFmt([...args, 'index.ts']);
175176

176177
expect(result.status).toBe(0);
177178
expect(readProjectFile('.rstack/cache/.gitignore')).toBe('*\n');
178-
expect(JSON.parse(readProjectFile('.rstack/cache/fmt-v1.json'))).toMatchObject({
179+
expect(JSON.parse(readProjectFile('.rstack/cache/fmt/v1.json'))).toMatchObject({
179180
version: 1,
180181
files: {
181182
'index.ts': [expect.any(String), expect.any(String), 'clean'],
182183
},
183184
});
185+
expect(readProjectFile('.rstack/cache/fmt-v1.json')).toBe('legacy');
184186
});
185187

186188
test('--no-cache bypasses cache reads and writes', () => {
@@ -209,9 +211,9 @@ test('uses an explicit config root cache from a subdirectory', () => {
209211

210212
expect(result.status).toBe(0);
211213
expect(readProjectFile('packages/app/index.ts')).toBe('const value = 1;\n');
212-
expect(existsSync(path.join(projectPath, '.rstack/cache/fmt-v1.json'))).toBe(true);
214+
expect(existsSync(path.join(projectPath, '.rstack/cache/fmt/v1.json'))).toBe(true);
213215
expect(existsSync(path.join(appPath, '.rstack'))).toBe(false);
214-
expect(JSON.parse(readProjectFile('.rstack/cache/fmt-v1.json'))).toMatchObject({
216+
expect(JSON.parse(readProjectFile('.rstack/cache/fmt/v1.json'))).toMatchObject({
215217
files: {
216218
'packages/app/index.ts': [expect.any(String), expect.any(String), 'clean'],
217219
},
@@ -221,14 +223,14 @@ test('uses an explicit config root cache from a subdirectory', () => {
221223
test('recovers from a corrupted cache', () => {
222224
writeProjectFile('index.ts', 'const value = 1;\n');
223225
const first = runFmt(['--check', 'index.ts']);
224-
writeProjectFile('.rstack/cache/fmt-v1.json', '{');
226+
writeProjectFile('.rstack/cache/fmt/v1.json', '{');
225227

226228
const second = runFmt(['--check', 'index.ts']);
227229

228230
expect(second.status).toBe(0);
229231
expect(normalizeDuration(second.stdout)).toBe(normalizeDuration(first.stdout));
230232
expect(second.stderr).toBe(first.stderr);
231-
expect(JSON.parse(readProjectFile('.rstack/cache/fmt-v1.json'))).toMatchObject({ version: 1 });
233+
expect(JSON.parse(readProjectFile('.rstack/cache/fmt/v1.json'))).toMatchObject({ version: 1 });
232234
});
233235

234236
test('formats without a writable cache directory', () => {

website/docs/en/guide/cli/fmt.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ Disable the persistent formatting cache for the current invocation:
129129
rs fmt --no-cache
130130
```
131131

132-
Without this option, `rs fmt` stores cache data in `.rstack/cache` under the Rstack configuration root. `--no-cache` prevents the command from reading, creating, or updating that cache. Stdin formatting never uses the persistent cache.
132+
Without this option, `rs fmt` stores cache data in `.rstack/cache/fmt` under the Rstack configuration root. `--no-cache` prevents the command from reading, creating, or updating that cache. Stdin formatting never uses the persistent cache.
133133

134134
See [Cache](../formatting#cache) for cache behavior and cleanup guidance.
135135

website/docs/en/guide/formatting.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ define.fmt({
178178

179179
`rs fmt` uses a persistent cache by default for file-based `--write`, `--check`, and `--list-different` runs. Cache entries use file content and final formatting options, so changing either causes the file to be formatted again. Files that use custom Prettier plugins currently bypass the cache.
180180

181-
The default cache file is `.rstack/cache/fmt-v1.json` under the Rstack configuration root. When a command runs from a subdirectory, it continues to use the cache next to the resolved `rstack.config.*` file. Stdin formatting does not use this cache.
181+
The default cache directory is `.rstack/cache/fmt` under the Rstack configuration root. When a command runs from a subdirectory, it continues to use the cache next to the resolved `rstack.config.*` file. Stdin formatting does not use this cache.
182182

183183
Use [`--no-cache`](./cli/fmt#--no-cache) to run without reading, creating, or updating the cache:
184184

website/docs/zh/guide/cli/fmt.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ rs fmt -l
129129
rs fmt --no-cache
130130
```
131131

132-
默认情况下,`rs fmt` 会将缓存数据保存在 Rstack 配置根目录下的 `.rstack/cache` 中。`--no-cache` 会阻止命令读取、创建或更新该缓存。stdin 格式化始终不会使用持久化缓存。
132+
默认情况下,`rs fmt` 会将缓存数据保存在 Rstack 配置根目录下的 `.rstack/cache/fmt` 中。`--no-cache` 会阻止命令读取、创建或更新该缓存。stdin 格式化始终不会使用持久化缓存。
133133

134134
缓存行为和清理方式请参考[缓存](../formatting#cache)
135135

website/docs/zh/guide/formatting.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ define.fmt({
178178

179179
`rs fmt` 默认会在基于文件的 `--write``--check``--list-different` 调用中使用持久化缓存。缓存条目基于文件内容和最终格式化选项;任意一项发生变化时,文件都会重新格式化。使用自定义 Prettier 插件的文件目前会绕过缓存。
180180

181-
默认缓存文件位于 Rstack 配置根目录下的 `.rstack/cache/fmt-v1.json`。从子目录运行命令时,仍会使用解析到的 `rstack.config.*` 文件旁的缓存。stdin 格式化不会使用该缓存。
181+
默认缓存目录位于 Rstack 配置根目录下的 `.rstack/cache/fmt`。从子目录运行命令时,仍会使用解析到的 `rstack.config.*` 文件旁的缓存。stdin 格式化不会使用该缓存。
182182

183183
使用 [`--no-cache`](./cli/fmt#--no-cache) 可以在运行时跳过缓存读取、创建和更新:
184184

0 commit comments

Comments
 (0)