From e8a0506344d2d8966f7d459d58126c986e0be984 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Wed, 13 Jan 2021 11:46:02 +0100 Subject: [PATCH 1/2] scripts/verify-links: refactor to not use any dependencies --- scripts/verify-links.js | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/scripts/verify-links.js b/scripts/verify-links.js index 75e7a8c1d0..5ad14ead4d 100755 --- a/scripts/verify-links.js +++ b/scripts/verify-links.js @@ -18,8 +18,27 @@ /* eslint-disable import/no-extraneous-dependencies */ const { resolve: resolvePath, join: joinPath, dirname } = require('path'); -const fs = require('fs-extra'); -const recursive = require('recursive-readdir'); +const fs = require('fs').promises; +const { existsSync } = require('fs'); + +const IGNORED_DIRS = ['node_modules', 'dist', 'bin', '.git']; + +async function listFiles(dir) { + const files = await fs.readdir(dir); + const paths = await Promise.all( + files + .filter(file => !IGNORED_DIRS.includes(file)) + .map(async file => { + const path = joinPath(dir, file); + + if ((await fs.stat(path)).isDirectory()) { + return listFiles(path); + } + return path; + }), + ); + return paths.flat(); +} const projectRoot = resolvePath(__dirname, '..'); @@ -65,7 +84,7 @@ async function verifyUrl(basePath, absUrl, docPages) { } const staticPath = resolvePath(projectRoot, 'microsite/static', `.${url}`); - if (await fs.pathExists(staticPath)) { + if (existsSync(staticPath)) { return undefined; } @@ -82,8 +101,7 @@ async function verifyUrl(basePath, absUrl, docPages) { return { url, basePath, problem: 'out-of-docs' }; } - const exists = await fs.pathExists(path); - if (!exists) { + if (!existsSync(path)) { return { url, basePath, problem: 'missing' }; } @@ -110,7 +128,7 @@ async function verifyFile(filePath, docPages) { // It is used to validate microsite links from outside /docs/, as those // are not transformed from the markdown file representation by docusaurus. async function findExternalDocsLinks(dir) { - const allFiles = await recursive(dir); + const allFiles = await listFiles(dir); const mdFiles = allFiles.filter(p => p.endsWith('.md')); const paths = new Map(); @@ -138,14 +156,15 @@ async function findExternalDocsLinks(dir) { async function main() { process.chdir(projectRoot); - const files = await recursive('.', ['node_modules', 'dist', 'bin']); + const files = await listFiles('.'); const mdFiles = files.filter(f => f.endsWith('.md')); const badUrls = []; const docPages = await findExternalDocsLinks('docs'); + const docPageSet = new Set(docPages.values()); for (const mdFile of mdFiles) { - const badFileUrls = await verifyFile(mdFile, new Set(docPages.values())); + const badFileUrls = await verifyFile(mdFile, docPageSet); badUrls.push(...badFileUrls); } From 9595e2e166553e72eb3b1714b16e55b4777c96c2 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Wed, 13 Jan 2021 11:47:51 +0100 Subject: [PATCH 2/2] workflows: run link verification as part of microsite CI --- .github/workflows/microsite-build-check.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/microsite-build-check.yml b/.github/workflows/microsite-build-check.yml index 118ba942bb..45182229c9 100644 --- a/.github/workflows/microsite-build-check.yml +++ b/.github/workflows/microsite-build-check.yml @@ -27,6 +27,9 @@ jobs: with: node-version: ${{ matrix.node-version }} + - name: verify doc links + run: node scripts/verify-links.js + # Skip caching of microsite dependencies, it keeps the global cache size # smaller, which make Windows builds a lot faster for the rest of the project. - name: yarn install