-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
13 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}); | ||
checkLinks(); |