{"file":"client/src/export.ts","version":7,"total":145,"from":1,"to":145,"lines":[{"n":1,"text":"// abso export <dir> [--version N]: one published version of this folder's","atom":"a0","author":"init","version":0},{"n":2,"text":"// space, written to a folder of its own, for a build that doesn't move while","atom":"a0","author":"init","version":0},{"n":3,"text":"// shared does (#37, Playtester 1). The folder gets .avcs-export.json naming","atom":"a0","author":"init","version":0},{"n":4,"text":"// the version, to stamp the build with. Exporting again replaces the folder,","atom":"a0","author":"init","version":0},{"n":5,"text":"// keeping node_modules (and what --keep names) so a build needs no new install.","atom":"a0","author":"init","version":0},{"n":6,"text":"//","atom":"a0","author":"init","version":0},{"n":7,"text":"// The version is rebuilt as `abso run --at` rebuilds it (at.ts), from the","atom":"a0","author":"init","version":0},{"n":8,"text":"// known copies and one compare; the server's listing of that version (#37,","atom":"a0","author":"init","version":0},{"n":9,"text":"// Engine 522be7f) then checks that no file is left out.","atom":"a0","author":"init","version":0},{"n":10,"text":"","atom":"a0","author":"init","version":0},{"n":11,"text":"import { chmodSync, existsSync, mkdirSync, readdirSync, rmSync, statSync, writeFileSync } from \"node:fs\";","atom":"a0","author":"init","version":0},{"n":12,"text":"import { dirname, join, resolve } from \"node:path\";","atom":"a0","author":"init","version":0},{"n":13,"text":"import type { Where } from \"./api.ts\";","atom":"a0","author":"init","version":0},{"n":14,"text":"import { type Parsed, str, UsageError } from \"./args.ts\";","atom":"a0","author":"init","version":0},{"n":15,"text":"import { globTest, snapshotAt, snapshotEntries } from \"./at.ts\";","atom":"a0","author":"init","version":0},{"n":16,"text":"import { type Env, type Output, open, plural } from \"./commands.ts\";","atom":"a0","author":"init","version":0},{"n":17,"text":"import { ignoredHere } from \"./fetch.ts\";","atom":"a0","author":"init","version":0},{"n":18,"text":"import { loadIgnore, relativeTo } from \"./folder.ts\";","atom":"a0","author":"init","version":0},{"n":19,"text":"import { type Refused, unsafePath } from \"./safety.ts\";","atom":"a0","author":"init","version":0},{"n":20,"text":"","atom":"a0","author":"init","version":0},{"n":21,"text":"/** The file an export leaves in its folder: what it holds, and the sign that the folder is one export may replace. */","atom":"a0","author":"init","version":0},{"n":22,"text":"export const EXPORT_STAMP = \".avcs-export.json\";","atom":"a0","author":"init","version":0},{"n":23,"text":"","atom":"a0","author":"init","version":0},{"n":24,"text":"/** Where an export may go: outside the abso folder, or in a part of it abso leaves out. Empty, or an earlier export. */","atom":"a0","author":"init","version":0},{"n":25,"text":"function checkTarget(root: string, target: string): void {","atom":"a0","author":"init","version":0},{"n":26,"text":"\tconst inside = relativeTo(root, target);","atom":"a0","author":"init","version":0},{"n":27,"text":"\tif (inside === undefined && (target === root || relativeTo(target, root) !== undefined)) {","atom":"a0","author":"init","version":0},{"n":28,"text":"\t\tthrow new UsageError(`${target} holds this abso folder; export to a folder of its own`);","atom":"a0","author":"init","version":0},{"n":29,"text":"\t}","atom":"a0","author":"init","version":0},{"n":30,"text":"\tif (inside !== undefined && !ignoredHere(loadIgnore(root), inside, true)) {","atom":"a0","author":"init","version":0},{"n":31,"text":"\t\tthrow new UsageError(","atom":"a0","author":"init","version":0},{"n":32,"text":"\t\t\t`${inside} is inside this abso folder, so abso would record the export as your edits. Export outside the folder, or into a folder .avcsignore leaves out`,","atom":"a0","author":"init","version":0},{"n":33,"text":"\t\t);","atom":"a0","author":"init","version":0},{"n":34,"text":"\t}","atom":"a0","author":"init","version":0},{"n":35,"text":"\tif (!existsSync(target)) return;","atom":"a0","author":"init","version":0},{"n":36,"text":"\tif (!statSync(target).isDirectory()) throw new UsageError(`${target} is a file; export to a folder`);","atom":"a0","author":"init","version":0},{"n":37,"text":"\tif (readdirSync(target).length > 0 && !existsSync(join(target, EXPORT_STAMP))) {","atom":"a0","author":"init","version":0},{"n":38,"text":"\t\tthrow new UsageError(","atom":"a0","author":"init","version":0},{"n":39,"text":"\t\t\t`${target} has files in it; export to an empty folder (an export replaces only a folder an earlier export made)`,","atom":"a0","author":"init","version":0},{"n":40,"text":"\t\t);","atom":"a0","author":"init","version":0},{"n":41,"text":"\t}","atom":"a0","author":"init","version":0},{"n":42,"text":"}","atom":"a0","author":"init","version":0},{"n":43,"text":"","atom":"a0","author":"init","version":0},{"n":44,"text":"/**","atom":"a0","author":"init","version":0},{"n":45,"text":" * Empties an earlier export for the next, except what `keep` names: node_modules","atom":"a0","author":"init","version":0},{"n":46,"text":" * always (abso never exports it, and an install takes a while), and --keep's","atom":"a0","author":"init","version":0},{"n":47,"text":" * globs. Returns the kept paths, the top-most of each.","atom":"a0","author":"init","version":0},{"n":48,"text":" */","atom":"a0","author":"init","version":0},{"n":49,"text":"function clearExport(dir: string, keep: (rel: string) => boolean, rel = \"\"): string[] {","atom":"a0","author":"init","version":0},{"n":50,"text":"\tconst kept: string[] = [];","atom":"a0","author":"init","version":0},{"n":51,"text":"\tfor (const e of readdirSync(join(dir, ...rel.split(\"/\").filter(Boolean)), { withFileTypes: true })) {","atom":"a0","author":"init","version":0},{"n":52,"text":"\t\tconst path = rel === \"\" ? e.name : `${rel}/${e.name}`;","atom":"a0","author":"init","version":0},{"n":53,"text":"\t\tconst full = join(dir, ...path.split(\"/\"));","atom":"a0","author":"init","version":0},{"n":54,"text":"\t\tif (e.name === \"node_modules\" || keep(path)) {","atom":"a0","author":"init","version":0},{"n":55,"text":"\t\t\tkept.push(path);","atom":"a0","author":"init","version":0},{"n":56,"text":"\t\t\tcontinue;","atom":"a0","author":"init","version":0},{"n":57,"text":"\t\t}","atom":"a0","author":"init","version":0},{"n":58,"text":"\t\tif (e.isDirectory() && !e.isSymbolicLink()) {","atom":"a0","author":"init","version":0},{"n":59,"text":"\t\t\tconst inner = clearExport(dir, keep, path);","atom":"a0","author":"init","version":0},{"n":60,"text":"\t\t\tkept.push(...inner);","atom":"a0","author":"init","version":0},{"n":61,"text":"\t\t\tif (inner.length > 0) continue;","atom":"a0","author":"init","version":0},{"n":62,"text":"\t\t}","atom":"a0","author":"init","version":0},{"n":63,"text":"\t\trmSync(full, { recursive: true, force: true });","atom":"a0","author":"init","version":0},{"n":64,"text":"\t}","atom":"a0","author":"init","version":0},{"n":65,"text":"\treturn kept;","atom":"a0","author":"init","version":0},{"n":66,"text":"}","atom":"a0","author":"init","version":0},{"n":67,"text":"","atom":"a0","author":"init","version":0},{"n":68,"text":"export async function runExport(args: Parsed, env: Env): Promise<Output> {","atom":"a0","author":"init","version":0},{"n":69,"text":"\tif (args.positionals.length !== 1)","atom":"a0","author":"init","version":0},{"n":70,"text":"\t\tthrow new UsageError(\"export takes the folder to write: abso export <dir> [--version <N>]\");","atom":"a0","author":"init","version":0},{"n":71,"text":"\tconst { root, config, api } = open(env);","atom":"a0","author":"init","version":0},{"n":72,"text":"\tconst target = resolve(env.cwd, args.positionals[0]!);","atom":"a0","author":"init","version":0},{"n":73,"text":"\tcheckTarget(root, target);","atom":"a0","author":"init","version":0},{"n":74,"text":"\tconst rawVersion = str(args, \"version\");","atom":"a0","author":"init","version":0},{"n":75,"text":"\tif (rawVersion !== undefined && !/^\\d+$/.test(rawVersion))","atom":"a0","author":"init","version":0},{"n":76,"text":"\t\tthrow new UsageError(`--version is a version number (got ${JSON.stringify(rawVersion)})`);","atom":"a0","author":"init","version":0},{"n":77,"text":"\tconst diff = await api.diff();","atom":"a0","author":"init","version":0},{"n":78,"text":"\tconst where: Where = { draft: diff.draft };","atom":"a0","author":"init","version":0},{"n":79,"text":"\t// The listing names the version (the newest by default) and every file it holds; a server from before it answers 404.","atom":"a0","author":"init","version":0},{"n":80,"text":"\tconst listing = await api","atom":"a0","author":"init","version":0},{"n":81,"text":"\t\t.filesAt(diff.draft, rawVersion === undefined ? undefined : Number(rawVersion))","atom":"a0","author":"init","version":0},{"n":82,"text":"\t\t.catch(() => undefined);","atom":"a0","author":"init","version":0},{"n":83,"text":"\tconst version =","atom":"a0","author":"init","version":0},{"n":84,"text":"\t\trawVersion !== undefined ? Number(rawVersion) : (listing?.version ?? (await api.log(where, 1)).version);","atom":"a0","author":"init","version":0},{"n":85,"text":"\tconst snap = await snapshotAt(root, api, version);","atom":"a0","author":"init","version":0},{"n":86,"text":"\tconst skipped: Refused[] = [];","atom":"a0","author":"init","version":0},{"n":87,"text":"\tif (listing !== undefined) {","atom":"a0","author":"init","version":0},{"n":88,"text":"\t\tconst holds = new Set(listing.files);","atom":"a0","author":"init","version":0},{"n":89,"text":"\t\tfor (const rel of [...snap.files.keys()]) if (!holds.has(rel)) snap.files.delete(rel);","atom":"a0","author":"init","version":0},{"n":90,"text":"\t\t// Files the known copies lack (a folder that missed some, #37): asked for one by one.","atom":"a0","author":"init","version":0},{"n":91,"text":"\t\tfor (const rel of listing.files) {","atom":"a0","author":"init","version":0},{"n":92,"text":"\t\t\tif (snap.files.has(rel)) continue;","atom":"a0","author":"init","version":0},{"n":93,"text":"\t\t\tconst file = await api.fileAt(rel, version, where);","atom":"a0","author":"init","version":0},{"n":94,"text":"\t\t\tif (file.text !== null) snap.files.set(rel, { text: file.text });","atom":"a0","author":"init","version":0},{"n":95,"text":"\t\t\telse","atom":"a0","author":"init","version":0},{"n":96,"text":"\t\t\t\tskipped.push({","atom":"a0","author":"init","version":0},{"n":97,"text":"\t\t\t\t\tpath: rel,","atom":"a0","author":"init","version":0},{"n":98,"text":"\t\t\t\t\twhy: \"a binary file this folder has no copy of; abso sync --full, then export again\",","atom":"a0","author":"init","version":0},{"n":99,"text":"\t\t\t\t});","atom":"a0","author":"init","version":0},{"n":100,"text":"\t\t}","atom":"a0","author":"init","version":0},{"n":101,"text":"\t}","atom":"a0","author":"init","version":0},{"n":102,"text":"\tconst entries = await snapshotEntries(api, snap);","atom":"a0","author":"init","version":0},{"n":103,"text":"\t// Built before anything is removed, so a failed export leaves an earlier one whole.","atom":"a0","author":"init","version":0},{"n":104,"text":"\tconst keepGlob = str(args, \"keep\");","atom":"a0","author":"init","version":0},{"n":105,"text":"\tconst kept = existsSync(target) ? clearExport(target, keepGlob === undefined ? () => false : globTest(keepGlob)) : [];","atom":"a0","author":"init","version":0},{"n":106,"text":"\tmkdirSync(target, { recursive: true });","atom":"a0","author":"init","version":0},{"n":107,"text":"\tlet written = 0;","atom":"a0","author":"init","version":0},{"n":108,"text":"\tfor (const e of entries) {","atom":"a0","author":"init","version":0},{"n":109,"text":"\t\tconst why = unsafePath(e.path) ?? (e.link !== undefined ? \"it is a link; export makes none\" : undefined);","atom":"a0","author":"init","version":0},{"n":110,"text":"\t\tif (why !== undefined) {","atom":"a0","author":"init","version":0},{"n":111,"text":"\t\t\tskipped.push({ path: e.path, why });","atom":"a0","author":"init","version":0},{"n":112,"text":"\t\t\tcontinue;","atom":"a0","author":"init","version":0},{"n":113,"text":"\t\t}","atom":"a0","author":"init","version":0},{"n":114,"text":"\t\tconst path = join(target, ...e.path.split(\"/\"));","atom":"a0","author":"init","version":0},{"n":115,"text":"\t\tmkdirSync(dirname(path), { recursive: true });","atom":"a0","author":"init","version":0},{"n":116,"text":"\t\twriteFileSync(path, e.data);","atom":"a0","author":"init","version":0},{"n":117,"text":"\t\tif (e.mode !== undefined) {","atom":"a0","author":"init","version":0},{"n":118,"text":"\t\t\ttry {","atom":"a0","author":"init","version":0},{"n":119,"text":"\t\t\t\tchmodSync(path, e.mode);","atom":"a0","author":"init","version":0},{"n":120,"text":"\t\t\t} catch {","atom":"a0","author":"init","version":0},{"n":121,"text":"\t\t\t\t// The bit is a convenience here.","atom":"a0","author":"init","version":0},{"n":122,"text":"\t\t\t}","atom":"a0","author":"init","version":0},{"n":123,"text":"\t\t}","atom":"a0","author":"init","version":0},{"n":124,"text":"\t\twritten++;","atom":"a0","author":"init","version":0},{"n":125,"text":"\t}","atom":"a0","author":"init","version":0},{"n":126,"text":"\tconst space = diff.draft === null ? \"shared\" : `draft ${diff.draft}`;","atom":"a0","author":"init","version":0},{"n":127,"text":"\tconst stamp = {","atom":"a0","author":"init","version":0},{"n":128,"text":"\t\tproject: config.project,","atom":"a0","author":"init","version":0},{"n":129,"text":"\t\tspace: diff.draft ?? \"shared\",","atom":"a0","author":"init","version":0},{"n":130,"text":"\t\tversion,","atom":"a0","author":"init","version":0},{"n":131,"text":"\t\tfiles: written,","atom":"a0","author":"init","version":0},{"n":132,"text":"\t\texportedAt: new Date().toISOString(),","atom":"a0","author":"init","version":0},{"n":133,"text":"\t};","atom":"a0","author":"init","version":0},{"n":134,"text":"\twriteFileSync(join(target, EXPORT_STAMP), `${JSON.stringify(stamp, null, 2)}\\n`);","atom":"a0","author":"init","version":0},{"n":135,"text":"\treturn {","atom":"a0","author":"init","version":0},{"n":136,"text":"\t\ttext: [","atom":"a0","author":"init","version":0},{"n":137,"text":"\t\t\t`Exported version ${version} of ${space} to ${target}: ${plural(written, \"file\")}. ${EXPORT_STAMP} there names the version; abso export to the same folder replaces it.`,","atom":"a0","author":"init","version":0},{"n":138,"text":"\t\t\t...(kept.length > 0 ? [`Kept from the earlier export: ${kept.join(\", \")}.`] : []),","atom":"a0","author":"init","version":0},{"n":139,"text":"\t\t\t...(skipped.length > 0","atom":"a0","author":"init","version":0},{"n":140,"text":"\t\t\t\t? [`Not exported (${skipped.length}):`, ...skipped.map((s) => `  ${s.path}: ${s.why}`)]","atom":"a0","author":"init","version":0},{"n":141,"text":"\t\t\t\t: []),","atom":"a0","author":"init","version":0},{"n":142,"text":"\t\t].join(\"\\n\"),","atom":"a0","author":"init","version":0},{"n":143,"text":"\t\tjson: { ...stamp, dir: target, kept, skipped },","atom":"a0","author":"init","version":0},{"n":144,"text":"\t};","atom":"a0","author":"init","version":0},{"n":145,"text":"}","atom":"a0","author":"init","version":0}],"atoms":[{"atom":"a0","author":"init","message":"Start the project","time":1791506895507,"timeIso":"2026-10-09T00:48:15.507Z","publish":{"draft":null,"version":0,"agent":"init","message":"Start the project","time":1791506895507,"timeIso":"2026-10-09T00:48:15.507Z"}}]}