Skip to content

Commit

Permalink
feat(cli): Fix recursive document deletion (#9486)
Browse files Browse the repository at this point in the history
* feat(cli): Add non-recursive document deletion

* nonRecursive -> recursive

---------

Co-authored-by: Claas Augner <caugner@mozilla.com>
  • Loading branch information
queengooborg and caugner authored Aug 8, 2024
1 parent 9ad8658 commit 7b10636
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
44 changes: 29 additions & 15 deletions content/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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]);
}
Expand Down
4 changes: 2 additions & 2 deletions tool/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>",
"Redirect document (and its children, if --recursive is true) to the URL <redirect>"
"Redirect document, and its children (if --recursive is true), to the URL <redirect>"
)
.option("-y, --yes", "Assume yes", { default: false })
.action(
Expand Down

0 comments on commit 7b10636

Please sign in to comment.