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 Jul 15, 2023
1 parent 53314f5 commit b260348
Show file tree
Hide file tree
Showing 3 changed files with 47 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 @@ -116,6 +116,8 @@ export function saveFile(
"translation_of_original",
"original_slug",
"browser-compat",
"status",
"l10n",
];

const saveMetadata = {};
Expand Down
1 change: 1 addition & 0 deletions kumascript/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
53 changes: 44 additions & 9 deletions tool/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.")
);
}
}
})
)
Expand Down

0 comments on commit b260348

Please sign in to comment.