-
Notifications
You must be signed in to change notification settings - Fork 295
Expand file tree
/
Copy pathpathHelper.js
More file actions
36 lines (30 loc) · 878 Bytes
/
pathHelper.js
File metadata and controls
36 lines (30 loc) · 878 Bytes
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
import path from "path";
import { fileURLToPath } from "url";
import fs from "fs";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
/**
* PROJECT_ROOT resolution:
* 1. root environment variable (if set and exists)
* 2. default: drpy-node-mcp/utils -> drpy-node
*/
function findProjectRoot() {
// Check environment variable
if (process.env.ROOT) {
const envRoot = path.resolve(process.env.ROOT);
if (fs.existsSync(envRoot)) {
return envRoot;
}
}
// Default
const defaultRoot = path.resolve(__dirname, "..", "..");
return defaultRoot;
}
export const PROJECT_ROOT = findProjectRoot();
console.warn(`Using PROJECT_ROOT: ${PROJECT_ROOT}`);
export function resolvePath(p) {
return path.resolve(PROJECT_ROOT, p);
}
export function isSafePath(p) {
const resolved = resolvePath(p);
return resolved.startsWith(PROJECT_ROOT);
}