Skip to content

Commit

Permalink
Added "apply migration" step to the link checker tool
Browse files Browse the repository at this point in the history
  • Loading branch information
skoszuta committed Dec 13, 2024
1 parent 88833c3 commit 5fbe95f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
6 changes: 6 additions & 0 deletions tools/links-check/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as fs from "node:fs";
import * as path from "node:path";
import ignore from "ignore";
import fg from "fast-glob";
import { promptUserForMigration, performMigrations } from "./migrate";

main().catch((err: unknown) => {
console.error(`🚨 ${err}`);
Expand Down Expand Up @@ -95,4 +96,9 @@ async function main() {
}

printReport(report);

const applyMigrations = await promptUserForMigration();
if (applyMigrations) {
await performMigrations(report, commonPath);
}
}
35 changes: 35 additions & 0 deletions tools/links-check/migrate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type { Report } from "./report";
import * as readline from "node:readline";
import * as fs from "node:fs";
import * as path from "node:path";

export async function promptUserForMigration(): Promise<boolean> {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});

return new Promise((resolve) => {
rl.question("Would you like to apply suggested migrations? (yes/no): ", (answer) => {
rl.close();
resolve(answer.toLowerCase() === "yes");
});
});
}

export async function performMigrations(report: Report, commonPath: string) {
for (const [filePath, links] of report.outdated) {
const absoluteFilePath = path.resolve(commonPath, filePath);
const fileContent = await fs.promises.readFile(absoluteFilePath, "utf-8");
let updatedContent = fileContent;

for (const link of links) {
if (link.updated && link.migrated) {
updatedContent = updatedContent.replace(link.url, link.updated);
}
}

await fs.promises.writeFile(absoluteFilePath, updatedContent, "utf-8");
console.info(`Updated links in file: ${absoluteFilePath}`);
}
}

0 comments on commit 5fbe95f

Please sign in to comment.