Files
sprites/tools/deploy/path.ts
Christopher Monsanto 73f5954407 Adopt the house import style
Node builtins come in as node:-prefixed namespace imports;
multi-symbol internal imports use short namespace aliases (artifact,
cas, subst, db, api, executor). Single-symbol pulls, types, and the
buildfile DSL names stay braced, matching the sanctioned exceptions.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-21 01:13:14 -04:00

47 lines
1.2 KiB
TypeScript

import * as pathlib from 'node:path';
// Slight variation of pathlib parse, less fields, different ext handling
export type Path = {dir: string, name: string, ext: string | null};
export function parse(s: string): Path {
let {dir, name, ext: dotext} = pathlib.parse(s);
let ext;
if (dotext === '') {
ext = null;
} else {
ext = dotext.slice(1);
}
return {dir, name, ext};
}
export function format({dir, name, ext}: Path) {
let dotext = ext === null ? '' : `.${ext}`;
return pathlib.format({dir, name, ext: dotext});
}
export function join(s: string, {dir, name, ext}: Path): Path {
return {dir: pathlib.join(s, dir), name, ext};
}
export type Delta = Partial<Path>;
export function update({dir, name, ext}: Path, delta: Delta): Path {
return {
dir: delta.dir ?? dir,
name: delta.name ?? name,
ext: delta.ext === undefined ? ext : delta.ext
};
}
// Convenience function
export type PathLike = Path | string;
export function path(p: PathLike, delta?: Delta): Path {
let parsed = typeof p === 'string' ? parse(p) : p;
if (delta !== undefined)
parsed = update(parsed, delta);
return parsed;
}