// abso export
[--version N]: one published version of this folder's
// space, written to a folder of its own, for a build that doesn't move while
// shared does (#37). The folder gets .abso-export naming the
// version, to stamp the build with: JSON on one line, in a file with no
// extension, so the exported project's own formatter and linter pass it by.
// Exporting again replaces the folder, keeping node_modules (and what --keep
// names) so a build needs no new install.
//
// The version is rebuilt as `abso run --at` rebuilds it (at.ts), from the
// known copies and one compare; the server's listing of that version (#37)
// then checks that no file is left out.
import { chmodSync, existsSync, mkdirSync, readdirSync, rmSync, statSync, writeFileSync } from "node:fs";
import { dirname, join, resolve } from "node:path";
import type { Where } from "./api.ts";
import { type Parsed, str, UsageError } from "./args.ts";
import { applySide, globTest, snapshotAt, snapshotEntries } from "./at.ts";
import { type Env, EXIT, type Output, open, plural } from "./commands.ts";
import { ignoredHere } from "./fetch.ts";
import { loadIgnore, relativeTo } from "./folder.ts";
import { type Refused, unsafePath } from "./safety.ts";
import { refusalText, versionsText, withoutArg } from "./without.ts";
/** The file an export leaves in its folder: what it holds, and the sign that the folder is one export may replace. */
export const EXPORT_STAMP = ".abso-export";
/** Its earlier names: an export made then still counts as one. A .json name drew the project's formatter. */
const OLD_EXPORT_STAMPS = [".abso-export.json", ".avcs-export.json"];
/** Where an export may go: outside the abso folder, or in a part of it abso leaves out. Empty, or an earlier export. */
function checkTarget(root: string, target: string): void {
const inside = relativeTo(root, target);
if (inside === undefined && (target === root || relativeTo(target, root) !== undefined)) {
throw new UsageError(`${target} holds this abso folder; export to a folder of its own`);
}
if (inside !== undefined && !ignoredHere(loadIgnore(root), inside, true)) {
throw new UsageError(
`${inside} is inside this abso folder, so abso would record the export as your edits. Export outside the folder, or into a folder .absoignore leaves out`,
);
}
if (!existsSync(target)) return;
if (!statSync(target).isDirectory()) throw new UsageError(`${target} is a file; export to a folder`);
if (
readdirSync(target).length > 0 &&
![EXPORT_STAMP, ...OLD_EXPORT_STAMPS].some((n) => existsSync(join(target, n)))
) {
throw new UsageError(
`${target} has files in it; export to an empty folder (an export replaces only a folder an earlier export made)`,
);
}
}
/**
* Empties an earlier export for the next, except what `keep` names: node_modules
* always (abso never exports it, and an install takes a while), and --keep's
* globs. Returns the kept paths, the top-most of each.
*/
function clearExport(dir: string, keep: (rel: string) => boolean, rel = ""): string[] {
const kept: string[] = [];
for (const e of readdirSync(join(dir, ...rel.split("/").filter(Boolean)), { withFileTypes: true })) {
const path = rel === "" ? e.name : `${rel}/${e.name}`;
const full = join(dir, ...path.split("/"));
if (e.name === "node_modules" || keep(path)) {
kept.push(path);
continue;
}
if (e.isDirectory() && !e.isSymbolicLink()) {
const inner = clearExport(dir, keep, path);
kept.push(...inner);
if (inner.length > 0) continue;
}
rmSync(full, { recursive: true, force: true });
}
return kept;
}
export async function runExport(args: Parsed, env: Env): Promise