Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(tool/move): update files pointing to the moved document(s) #8493

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 47 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,44 @@ 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.replace(
new RegExp(oldSlug, "g"),
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