-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtemp-dir.ts
More file actions
60 lines (55 loc) · 1.95 KB
/
temp-dir.ts
File metadata and controls
60 lines (55 loc) · 1.95 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* Secure project-local temporary directory utilities.
*
* All temporary files are created under `<packageRoot>/.tmp/` which is
* `.gitignore`d. This avoids writing to the OS temp directory
* (`os.tmpdir()` / `/tmp`), which is world-readable and triggers
* CWE-377 / CWE-378 (js/insecure-temporary-file).
*/
import { mkdirSync, mkdtempSync } from 'fs';
import { join } from 'path';
import { getPackageRootDir } from './package-paths';
/**
* Base directory for all project-local temporary data.
*
* Resolution order:
* 1. `CODEQL_MCP_TMP_DIR` environment variable — for read-only package root
* scenarios (e.g., npm global installs where the package directory is not
* writable).
* 2. `<packageRoot>/.tmp` — default; excluded from version control.
*/
const PROJECT_TMP_BASE = process.env.CODEQL_MCP_TMP_DIR || join(getPackageRootDir(), '.tmp');
/**
* Return the project-local `.tmp` base directory, creating it if needed.
*/
export function getProjectTmpBase(): string {
mkdirSync(PROJECT_TMP_BASE, { recursive: true });
return PROJECT_TMP_BASE;
}
/**
* Create a unique temporary directory under the project `.tmp` root.
*
* Works identically to `fs.mkdtempSync(os.tmpdir(), prefix)` but is
* scoped to the repository.
*
* @param prefix - Directory name prefix (e.g. `'codeql-external-'`).
* @returns Absolute path to the newly created directory.
*/
export function createProjectTempDir(prefix: string): string {
const base = getProjectTmpBase();
return mkdtempSync(join(base, prefix));
}
/**
* Return a deterministic subdirectory under `.tmp/<name>`, creating it
* if it does not already exist.
*
* Useful for well-known scratch areas such as `query-logs` or `quickeval`.
*
* @param name - Subdirectory name (e.g. `'query-logs'`).
* @returns Absolute path to the subdirectory.
*/
export function getProjectTmpDir(name: string): string {
const dir = join(getProjectTmpBase(), name);
mkdirSync(dir, { recursive: true });
return dir;
}