From 18e4804a516272ccc93039decf8549932a4a3009 Mon Sep 17 00:00:00 2001 From: Kamran Ahmed Date: Mon, 9 Jan 2023 15:35:45 +0400 Subject: [PATCH] Add command to collect links from roadmaps --- bin/roadmap-links.cjs | 44 +++++++++++++++++++++++++++++++++++++++++++ package.json | 3 ++- 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 bin/roadmap-links.cjs diff --git a/bin/roadmap-links.cjs b/bin/roadmap-links.cjs new file mode 100644 index 000000000..b4ed4630f --- /dev/null +++ b/bin/roadmap-links.cjs @@ -0,0 +1,44 @@ +const fs = require('fs'); +const path = require('path'); + +const roadmapId = process.argv[2]; +if (!roadmapId) { + console.error('Error: roadmapId is required'); +} + +const fullPath = path.join(__dirname, `../src/roadmaps/${roadmapId}`); +if (!fs.existsSync(fullPath)) { + console.error(`Error: path not found: ${fullPath}!`); + process.exit(1); +} + +function readFiles(folderPath) { + const stats = fs.lstatSync(folderPath); + + if (stats.isFile()) { + return [folderPath]; + } + + const folderContent = fs.readdirSync(folderPath); + let files = []; + + for (const file of folderContent) { + const filePath = path.join(folderPath, file); + + files = [...files, ...readFiles(filePath)]; + } + + return files; +} + +const files = readFiles(fullPath); +let allLinks = []; + +files.forEach((file) => { + const fileContent = fs.readFileSync(file, 'utf-8'); + const matches = [...fileContent.matchAll(/\[[^\]]+]\((https?:\/\/[^)]+)\)/g)]; + + allLinks = [...allLinks, ...matches.map((match) => match[1])]; +}); + +allLinks.map((link) => console.log(link)); diff --git a/package.json b/package.json index c22e3dfaa..bc3476203 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,8 @@ "deploy": "NODE_DEBUG=gh-pages gh-pages -d dist -t", "sync-content": "sh ./bin/sync-content.sh", "compress:jsons": "node bin/compress-jsons.cjs", - "upgrade": "ncu -u" + "upgrade": "ncu -u", + "roadmap-links": "node bin/roadmap-links.cjs" }, "dependencies": { "@astrojs/sitemap": "^1.0.0",