forked from rstackjs/rstack-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojectCache.ts
More file actions
35 lines (28 loc) · 1.19 KB
/
Copy pathprojectCache.ts
File metadata and controls
35 lines (28 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { mkdir, readFile, writeFile } from 'node:fs/promises';
import path from 'node:path';
const cacheGitignore = '*\n';
type ProjectCacheResult =
{ status: 'available'; path: string } | { status: 'unavailable'; path: string; error: unknown };
/** Returns the disposable cache directory for a resolved Rstack project root. */
const getProjectCacheDir = (rootPath: string): string => path.join(rootPath, '.rstack', 'cache');
/** Creates the project cache directory without making cache failures fatal. */
const ensureProjectCacheDir = async (rootPath: string): Promise<ProjectCacheResult> => {
const cachePath = getProjectCacheDir(rootPath);
const ignorePath = path.join(cachePath, '.gitignore');
try {
if ((await readFile(ignorePath, 'utf8')) === cacheGitignore) {
return { status: 'available', path: cachePath };
}
} catch {
// Create or repair the marker below.
}
try {
await mkdir(cachePath, { recursive: true });
await writeFile(ignorePath, cacheGitignore);
return { status: 'available', path: cachePath };
} catch (error) {
return { status: 'unavailable', path: cachePath, error };
}
};
export { ensureProjectCacheDir };
export type { ProjectCacheResult };