Files
hermes-mobile/apps/companion/src/config/paths.ts
T
2026-07-09 04:43:00 +00:00

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));
}