Skip to content

Commit

Permalink
x
Browse files Browse the repository at this point in the history
  • Loading branch information
isahers1 committed Aug 29, 2024
1 parent c1a468c commit 4ec62ab
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 42 deletions.
1 change: 1 addition & 0 deletions .github/workflows/link_check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ jobs:
- name: Install dependencies
run: |
yarn install --frozen-lockfile
npm install linkinator
- name: Get changed files
id: changed-files
Expand Down
54 changes: 12 additions & 42 deletions scripts/check-links.cjs
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();

0 comments on commit 4ec62ab

Please sign in to comment.