// `archilyzer reports delete [--iteration ] [--apply] [--force] [--json]` // (release 27 follow-up) — delete an article, or one of its iterations, through // the registry (lib/report/delete.ts). // // DRY RUN BY DEFAULT: it prints the plan — every path that would go, with its // size, what is kept and why, and what refuses — and writes nothing. `--apply` // deletes. `--force` (an article only) unpublishes it and drops a site page // that names it with no report of its own, first; it never overrides a running // iteration. // // The projects root is settings.generation.projectsRoot (`~` expanded; blank = // ~/reports/articles). umtool's media root, when UMTOOL_MEDIA_DIR is set here, // is where a tiered project's `out/` link points (its reports root REPORTS_DIR, // else ~/reports, as umtool/lib/paths.mjs reads them): such a link's target // goes with the project. Without it, the target is kept and named. // // Exit 0 when the plan is clean (and applied, with --apply); 1 when it is // refused, or a path could not be removed; 2 for usage, or no such article or // iteration. import { projectsRootFrom } from "../lib/projectsRoot.mjs"; import os from "node:os"; import path from "node:path"; import type { GenerationSettings } from "../lib/generationSettings"; import type { Paths } from "../lib/paths"; import { deleteArticle, deleteBlocked, deleteIteration, DeleteRefusedError, formatDeletePlan, planArticleDelete, planIterationDelete, deleteMediaFromEnv, type DeleteMedia, type DeletePaths, type DeletePlan, type DeleteResult, } from "../lib/report/delete"; import { ArticleRegistryError } from "../lib/report/registry"; export { deleteMediaFromEnv as mediaFromEnv }; type Out = { log: (s: string) => void; error: (s: string) => void }; export type DeleteMainOptions = { article: string; iteration?: number; apply?: boolean; force?: boolean; json?: boolean; projectsRoot: string; media?: DeleteMedia | null; paths: DeletePaths; }; export async function deleteMain(o: DeleteMainOptions, out: Out = console): Promise { const opts = { projectsRoot: o.projectsRoot, media: o.media ?? null, paths: o.paths, force: o.force === true }; const name = o.iteration !== undefined ? `"${o.article}" i${o.iteration}` : `"${o.article}"`; let plan: DeletePlan; try { plan = o.iteration !== undefined ? await planIterationDelete(o.article, o.iteration, opts) : await planArticleDelete(o.article, opts); } catch (err) { if (err instanceof ArticleRegistryError) { out.error(`reports delete: ${err.message}`); return 2; } throw err; } const blocked = deleteBlocked(plan, opts.force); if (!o.apply || blocked) { if (o.json) out.log(JSON.stringify({ applied: false, blocked, plan }, null, 2)); else { for (const l of formatDeletePlan(plan, { force: opts.force })) out.log(l); out.log(blocked ? `nothing deleted: ${name} is refused` : `dry run: nothing deleted — run again with --apply to delete ${name}`); } return blocked ? 1 : 0; } let result: DeleteResult; try { result = o.iteration !== undefined ? await deleteIteration(o.article, o.iteration, opts) : await deleteArticle(o.article, opts); } catch (err) { if (err instanceof DeleteRefusedError) { // Something changed between the plan and the lock. if (o.json) out.log(JSON.stringify({ applied: false, blocked: true, plan: err.plan }, null, 2)); else for (const l of formatDeletePlan(err.plan, { force: opts.force })) out.log(l); out.error(`reports delete: ${err.message}`); return 1; } throw err; } if (o.json) out.log(JSON.stringify({ applied: true, ...result }, null, 2)); else { for (const l of formatDeletePlan(result.plan, { force: opts.force })) out.log(l); for (const p of result.removed) out.log(`removed ${p}`); for (const f of result.failed) out.error(`NOT removed ${f.path}: ${f.error}`); out.log(result.failed.length ? `${name}: deleted, ${result.failed.length} path(s) left (above)` : `${name}: deleted`); } return result.failed.length ? 1 : 0; } const expandHome = (p: string) => (p === "~" ? os.homedir() : p.startsWith("~/") ? path.join(os.homedir(), p.slice(2)) : p); // settings.generation.projectsRoot, `~` expanded; blank = ~/reports/articles // (controller/reportDraft.ts projectsRootOf, which a bin does not import). export const projectsRootOf = (gen: Pick): string => projectsRootFrom(gen.projectsRoot); // The CLI row's options, from the flags and the settings. export async function deleteRow( article: string, flags: { iteration?: string; apply?: boolean; force?: boolean; json?: boolean }, ): Promise { let iteration: number | undefined; if (flags.iteration !== undefined) { iteration = Number(flags.iteration); if (!Number.isInteger(iteration) || iteration < 1) { console.error(`reports delete: --iteration takes an iteration number (1, 2, …), not "${flags.iteration}"`); return 2; } if (flags.force) { console.error("reports delete: --force is for an article (unpublish, site pages); an iteration's refusals have no override"); return 2; } } const { getPaths } = await import("../lib/paths"); const { getSettings } = await import("../lib/settings"); const paths: Paths = getPaths(); return deleteMain({ article, ...(iteration !== undefined ? { iteration } : {}), apply: flags.apply === true, force: flags.force === true, json: flags.json === true, projectsRoot: projectsRootOf(getSettings().generation), media: deleteMediaFromEnv(), paths, }); }