Skip to content

Commit 5c314b3

Browse files
committed
feat: move CLI cache to OS temp directory to avoid project pollution
1 parent b9a2d59 commit 5c314b3

2 files changed

Lines changed: 15 additions & 5 deletions

File tree

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,13 @@ npx tsslint --project packages/*/tsconfig.json --vue-project apps/web/tsconfig.j
121121

122122
# Using brace expansion for multiple patterns
123123
npx tsslint --project {tsconfig.json,packages/*/tsconfig.json,extensions/*/tsconfig.json}
124+
125+
> [!NOTE]
126+
> The brace expansion pattern (`{a,b}`) is handled by your shell (e.g., bash, zsh) before the command is executed. The CLI receives a list of arguments.
127+
>
128+
> For glob patterns (`*`), TSSLint uses an internal `glob` library to ensure cross-platform compatibility (especially on Windows) and to support advanced features like file watching, where the original pattern is needed.
129+
>
130+
> **CLI Caching**: The CLI uses a file system cache to speed up subsequent runs. The cache files are stored in your operating system's temporary directory (`os.tmpdir()`) to avoid polluting your project's file system. The cache is automatically invalidated when the project's `tsslint.config.ts` or the CLI arguments change.
124131
```
125132
126133
> [!TIP]

packages/cli/lib/cache.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import core = require('@tsslint/core');
22
import path = require('path');
33
import fs = require('fs');
4+
import os = require('os');
45

56
export type CacheData = Record<string /* fileName */, core.FileLintCache>;
67

@@ -9,14 +10,14 @@ export function loadCache(
910
configFilePath: string,
1011
createHash: (path: string) => string = btoa
1112
): CacheData {
12-
const outDir = getDotTsslintPath(configFilePath);
13+
const outDir = getTsslintCachePath(configFilePath, createHash);
1314
const cacheFileName = createHash(path.relative(outDir, configFilePath)) + '_' + createHash(JSON.stringify(process.argv)) + '_' + createHash(path.relative(outDir, tsconfig)) + '.cache.json';
1415
const cacheFilePath = path.join(outDir, cacheFileName);
1516
const cacheFileStat = fs.statSync(cacheFilePath, { throwIfNoEntry: false });
1617
const configFileStat = fs.statSync(configFilePath, { throwIfNoEntry: false });
1718
if (cacheFileStat?.isFile() && cacheFileStat.mtimeMs > (configFileStat?.mtimeMs ?? 0)) {
1819
try {
19-
return require(cacheFilePath);
20+
return JSON.parse(fs.readFileSync(cacheFilePath, 'utf8'));
2021
} catch {
2122
return {};
2223
}
@@ -30,13 +31,15 @@ export function saveCache(
3031
cache: CacheData,
3132
createHash: (path: string) => string = btoa
3233
): void {
33-
const outDir = getDotTsslintPath(configFilePath);
34+
const outDir = getTsslintCachePath(configFilePath, createHash);
3435
const cacheFileName = createHash(path.relative(outDir, configFilePath)) + '_' + createHash(JSON.stringify(process.argv)) + '_' + createHash(path.relative(outDir, tsconfig)) + '.cache.json';
3536
const cacheFilePath = path.join(outDir, cacheFileName);
3637
fs.mkdirSync(outDir, { recursive: true });
3738
fs.writeFileSync(cacheFilePath, JSON.stringify(cache));
3839
}
3940

40-
function getDotTsslintPath(configFilePath: string): string {
41-
return path.resolve(configFilePath, '..', 'node_modules', '.tsslint');
41+
function getTsslintCachePath(configFilePath: string, createHash: (path: string) => string): string {
42+
const projectRoot = path.resolve(configFilePath, '..');
43+
const projectHash = createHash(projectRoot);
44+
return path.join(os.tmpdir(), 'tsslint-cache-' + projectHash);
4245
}

0 commit comments

Comments
 (0)