From 4ec62abb4fec461bef63fb1c3aaafe57889d5d33 Mon Sep 17 00:00:00 2001 From: isaac hershenson Date: Thu, 29 Aug 2024 08:31:13 -0700 Subject: [PATCH] x --- .github/workflows/link_check.yml | 1 + scripts/check-links.cjs | 54 +++++++------------------------- 2 files changed, 13 insertions(+), 42 deletions(-) diff --git a/.github/workflows/link_check.yml b/.github/workflows/link_check.yml index 4a910fff8..c42b5a6e0 100644 --- a/.github/workflows/link_check.yml +++ b/.github/workflows/link_check.yml @@ -45,6 +45,7 @@ jobs: - name: Install dependencies run: | yarn install --frozen-lockfile + npm install linkinator - name: Get changed files id: changed-files diff --git a/scripts/check-links.cjs b/scripts/check-links.cjs index f4f8e06c0..1a6f9d698 100644 --- a/scripts/check-links.cjs +++ b/scripts/check-links.cjs @@ -1,60 +1,30 @@ -const fs = require('fs'); -const path = require('path'); -const { LinkChecker } = require('linkinator'); +const { execSync } = require('child_process'); const ignorePatterns = [ 'https://(api|web)\\.smith\\.langchain\\.com/.*', 'https://x\\.com/.*' ]; -async function findIpynbFiles(dir) { - const files = await fs.promises.readdir(dir); - let results = []; - for (const file of files) { - const filePath = path.join(dir, file); - const stat = await fs.promises.stat(filePath); - if (stat.isDirectory()) { - results = results.concat(await findIpynbFiles(filePath)); - } else if (path.extname(file) === '.ipynb') { - results.push(filePath); - } - } - return results; -} +function checkLinks() { + const changedFiles = process.env.CHANGED_FILES ? process.env.CHANGED_FILES.split(' ') : []; + const ipynbFiles = changedFiles.filter(file => file.endsWith('.ipynb')); -async function checkLinks() { - const ipynbFiles = await findIpynbFiles('.'); - console.log('Found .ipynb files:', ipynbFiles); + console.log('Changed .ipynb files:', ipynbFiles); - const checker = new LinkChecker(); - - checker.on('link', (result) => { - console.log(`${result.status} ${result.url}`); - }); + if (ipynbFiles.length === 0) { + console.log('No .ipynb files were changed. Skipping link check.'); + return; + } for (const file of ipynbFiles) { console.log(`Checking links in ${file}`); try { - const result = await checker.check({ - path: file, - recurse: false, - linksToSkip: ignorePatterns, - }); - - if (result.passed) { - console.log(`All links in ${file} are valid.`); - } else { - console.error(`Broken links found in ${file}.`); - process.exitCode = 1; - } + execSync(`npx linkinator ${file} ${ignorePatterns.map(pattern => `--skip "${pattern}"`).join(' ')}`, { stdio: 'inherit' }); } catch (error) { console.error(`Error checking links in ${file}:`, error); - process.exitCode = 1; + process.exit(1); } } } -checkLinks().catch(error => { - console.error('An error occurred:', error); - process.exitCode = 1; -}); \ No newline at end of file +checkLinks(); \ No newline at end of file