28 lines
1.0 KiB
TypeScript
28 lines
1.0 KiB
TypeScript
import { isAbsolute, relative, resolve, sep } from 'node:path';
|
|
|
|
const ignoredPathSegments = new Set(['.git', '.dev', 'node_modules', 'dist', 'android']);
|
|
|
|
export function toSafeAbsolutePath(workspaceRoot: string, requestedPath = '.'): string {
|
|
const root = resolve(workspaceRoot);
|
|
const target = resolve(root, requestedPath || '.');
|
|
const relativePath = relative(root, target);
|
|
|
|
if (relativePath === '' || (!relativePath.startsWith('..') && !relativePath.includes(`..${sep}`) && !isAbsolute(relativePath))) {
|
|
return target;
|
|
}
|
|
|
|
throw new Error('Path is outside the configured workspace root');
|
|
}
|
|
|
|
export function toWorkspacePath(workspaceRoot: string, absolutePath: string): string {
|
|
const relativePath = relative(resolve(workspaceRoot), resolve(absolutePath));
|
|
return relativePath === '' ? '.' : relativePath.split(sep).join('/');
|
|
}
|
|
|
|
export function isIgnoredWorkspacePath(workspacePath: string): boolean {
|
|
return workspacePath
|
|
.split('/')
|
|
.filter(Boolean)
|
|
.some((segment) => ignoredPathSegments.has(segment));
|
|
}
|