From b260348df9e11b54337e50ba4925b8377fa52add Mon Sep 17 00:00:00 2001 From: OnkarRuikar <87750369+OnkarRuikar@users.noreply.github.com> Date: Fri, 24 Mar 2023 20:06:02 +0530 Subject: [PATCH] feature(tool/move): update files pointing to the moved document(s) --- content/document.ts | 2 ++ kumascript/index.ts | 1 + tool/cli.ts | 53 +++++++++++++++++++++++++++++++++++++-------- 3 files changed, 47 insertions(+), 9 deletions(-) diff --git a/content/document.ts b/content/document.ts index 18cd235ec63a..d72114ffdeea 100644 --- a/content/document.ts +++ b/content/document.ts @@ -116,6 +116,8 @@ export function saveFile( "translation_of_original", "original_slug", "browser-compat", + "status", + "l10n", ]; const saveMetadata = {}; diff --git a/kumascript/index.ts b/kumascript/index.ts index 9e6cbb7c6a63..ff00fa115979 100644 --- a/kumascript/index.ts +++ b/kumascript/index.ts @@ -94,6 +94,7 @@ export async function render( ...metadata, url, tags: metadata.tags || [], + status: metadata.status || [], selective_mode, interactive_examples: { base_url: INTERACTIVE_EXAMPLES_BASE_URL, diff --git a/tool/cli.ts b/tool/cli.ts index ead3e0622a6a..13de3b39efed 100644 --- a/tool/cli.ts +++ b/tool/cli.ts @@ -388,25 +388,27 @@ program // find references to the deleted document in content console.log("Checking references..."); - const referringFiles = []; + const referringFiles = new Set(); const allDocs = await Document.findAll(); for (const document of allDocs.iterDocs()) { const rawBody = document.rawBody; for (const deleted of deletedDocs) { const url = `/${locale}/docs/${deleted}`; if (rawBody.includes(url)) { - referringFiles.push(`${document.url}`); + referringFiles.add(`${document.url}`); } } } - if (referringFiles.length) { + if (referringFiles.size) { console.warn( chalk.yellow( - `\n${referringFiles.length} files are referring to the deleted document. ` + - `Please update the following files to remove the links:\n\t${referringFiles.join( - "\n\t" - )}` + `\n${referringFiles.size} files are referring to the deleted document(s). ` + + `Please update the following files to remove the links:\n\t${[ + ...referringFiles, + ] + .sort() + .join("\n\t")}` ) ); } else { @@ -459,8 +461,41 @@ program default: true, }); if (run) { - const moved = await Document.move(oldSlug, newSlug, locale); - console.log(chalk.green(`Moved ${moved.length} documents.`)); + const movedDocs = await Document.move(oldSlug, newSlug, locale); + console.log(chalk.green(`Moved ${movedDocs.length} documents.`)); + + // find and update references to the moved document + console.log("\nUpdating references..."); + const updatedFiles = new Set(); + const allDocs = await Document.findAll(); + for (const document of allDocs.iterDocs()) { + const rawBody = document.rawBody; + for (const [oldSlug, newSlug] of movedDocs) { + const updatedBody = rawBody.replaceAll(oldSlug, newSlug); + if (rawBody !== updatedBody) { + Document.saveFile( + document.fileInfo.path, + updatedBody, + document.metadata, + document.metadata.frontMatterKeys + ); + updatedFiles.add(document.url); + } + } + } + + if (updatedFiles.size) { + console.warn( + chalk.green( + `${updatedFiles.size} files referring to the moved document(s) have been updated:` + + `\n\t${[...updatedFiles.values()].sort().join("\n\t")}` + ) + ); + } else { + console.log( + chalk.green("\nNo file is referring to the moved document.") + ); + } } }) )