Files
stack/packages/project/src/paths.mjs
T
jason.woltjeandClaude Opus 5.5 56639c99b0 project: init cleans up after a failure and refuses a nested records repo (row 153, #1647)
Row 153 round 1, Rocko's candidate on 686fc1cb6. Filbert approved
(comment 28727). projectRepo lives in packages/project/src/paths.mjs, so
business.mjs is unchanged (sage-0469). Sage's landing gate ran on this
commit.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-15 19:32:58 -05:00

45 lines
1.9 KiB
JavaScript

import { lstatSync } from 'node:fs';
import { dirname, isAbsolute, join, normalize, relative, sep } from 'node:path';
export class ProjectError extends Error {
constructor(code, message) { super(`${code}: ${message}`); this.code = code; }
}
export const ID = /^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/;
export const checkedId = value => {
if (typeof value !== 'string' || !ID.test(value)) throw new ProjectError('project-id', 'invalid id');
return value;
};
export function absolute(path) {
if (typeof path !== 'string' || !isAbsolute(path) || normalize(path) !== path || path.includes('\0'))
throw new ProjectError('project-path', 'expected normalized absolute path');
return path;
}
export function noLinks(path) {
for (let part = path; ; part = dirname(part)) {
try {
const st = lstatSync(part);
if (!st.isDirectory() || st.isSymbolicLink()) throw new ProjectError('project-path', 'path ancestor must be a directory, never a link');
} catch (e) { if (e.code !== 'ENOENT') throw e; }
if (part === dirname(part)) break;
}
}
export function projectRepo({ business, project, dataRoot }) {
checkedId(business?.id); checkedId(project);
if (!Object.hasOwn(business?.projects ?? {}, project))
throw new ProjectError('project-not-declared', `business does not declare ${project}`);
absolute(dataRoot);
const entry = business.projects[project];
const repo = absolute(entry.repo ?? join(dataRoot, 'projects', business.id, project));
const contains = (base, child) => {
const rel = relative(base, child);
return rel === '' || (rel !== '..' && !rel.startsWith('..' + sep) && !isAbsolute(rel));
};
for (const project of Object.values(business.projects)) {
const code = absolute(project.root);
if (contains(code, repo) || contains(repo, code))
throw new ProjectError('project-path', 'record repository and code checkout overlap');
}
noLinks(repo);
return repo;
}