Skip to content

Commit

Permalink
feature(tool/move): update files pointing to the moved document(s)
Browse files Browse the repository at this point in the history
  • Loading branch information
OnkarRuikar committed May 8, 2024
1 parent e258f67 commit 6ca379b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
2 changes: 2 additions & 0 deletions content/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ export function saveFile(
"tags",
"original_slug",
"browser-compat",
"status",
"l10n",
];

const saveMetadata = {};
Expand Down
53 changes: 44 additions & 9 deletions tool/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,25 +372,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 {
Expand Down Expand Up @@ -443,8 +445,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);

Check failure on line 458 in tool/cli.ts

View workflow job for this annotation

GitHub Actions / build

Property 'replaceAll' does not exist on type 'string'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2021' or later.

Check failure on line 458 in tool/cli.ts

View workflow job for this annotation

GitHub Actions / lint

Property 'replaceAll' does not exist on type 'string'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2021' or later.
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.")
);
}
}
})
)
Expand Down

0 comments on commit 6ca379b

Please sign in to comment.