From 7b1063657160a58cb60a06e71ddf817cb34d9c54 Mon Sep 17 00:00:00 2001 From: "Queen Vinyl Da.i'gyu-Kazotetsu" Date: Thu, 8 Aug 2024 06:45:36 -0700 Subject: [PATCH] feat(cli): Fix recursive document deletion (#9486) * feat(cli): Add non-recursive document deletion * nonRecursive -> recursive --------- Co-authored-by: Claas Augner --- content/document.ts | 44 +++++++++++++++++++++++++++++--------------- tool/cli.ts | 4 ++-- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/content/document.ts b/content/document.ts index 2e8e0c24d634..3ebb11d99120 100644 --- a/content/document.ts +++ b/content/document.ts @@ -634,7 +634,10 @@ export async function remove( if (children.length > 0 && redirect && !recursive) { throw new Error("unable to remove and redirect a document with children"); } - const docs = [slug, ...children.map(({ metadata }) => metadata.slug)]; + const docs = [ + slug, + ...(recursive ? children.map(({ metadata }) => metadata.slug) : []), + ]; if (dry) { if (redirectUrl) { @@ -644,24 +647,35 @@ export async function remove( } const removed = []; - for (const { metadata } of children) { - const slug = metadata.slug; - await updateWikiHistory( - path.join(root, metadata.locale.toLowerCase()), - slug - ); - removed.push(buildURL(locale, slug)); + if (recursive) { + for (const { metadata } of children) { + const slug = metadata.slug; + await updateWikiHistory( + path.join(root, metadata.locale.toLowerCase()), + slug + ); + removed.push(buildURL(locale, slug)); + } } - execGit(["rm", "-r", path.dirname(fileInfo.path)], { cwd: root }); + if (recursive) { + execGit(["rm", "-r", path.dirname(fileInfo.path)], { cwd: root }); + } else { + execGit(["rm", fileInfo.path], { cwd: root }); + } + + console.log("Removed files"); if (redirectUrl) { - Redirect.add(locale, [ - [url, redirectUrl], - ...children.map( - ({ url: childUrl }) => [childUrl, redirectUrl] as [string, string] - ), - ]); + Redirect.add(locale, [[url, redirectUrl]]); + + if (recursive) { + Redirect.add(locale, [ + ...children.map( + ({ url: childUrl }) => [childUrl, redirectUrl] as [string, string] + ), + ]); + } } else { Redirect.remove(locale, [url, ...removed]); } diff --git a/tool/cli.ts b/tool/cli.ts index 43a95ac67cf6..1aa503075871 100644 --- a/tool/cli.ts +++ b/tool/cli.ts @@ -323,10 +323,10 @@ program default: DEFAULT_LOCALE, validator: [...VALID_LOCALES.values()], }) - .option("-r, --recursive", "Delete content recursively", { default: false }) + .option("-r, --recursive", "Delete children", { default: false }) .option( "--redirect ", - "Redirect document (and its children, if --recursive is true) to the URL " + "Redirect document, and its children (if --recursive is true), to the URL " ) .option("-y, --yes", "Assume yes", { default: false }) .action(