Skip to content

Commit 1b0d116

Browse files
committed
Log errors to .json continuously instead of waiting for the scan to complete
1 parent 55545e0 commit 1b0d116

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

scripts/checkDeadLinks.js

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,29 @@ async function checkLink(link) {
4949
}
5050
}
5151

52+
/**
53+
* Append dead link information to the JSON file.
54+
* @param {object} deadLink - The dead link object to append.
55+
*/
56+
async function appendDeadLinkToFile(deadLink) {
57+
const outputPath = path.resolve('dead_links.json');
58+
try {
59+
const data = await fs.readJson(outputPath).catch(() => []);
60+
data.push(deadLink);
61+
await fs.writeJson(outputPath, data, { spaces: 4 });
62+
} catch (error) {
63+
console.error(`❌ Failed to append dead link to JSON: ${error.message}`);
64+
}
65+
}
66+
5267
/**
5368
* Process a single file to extract and check links.
5469
* @param {string} filePath - The path to the file.
5570
* @param {string[]} excludePatterns - Patterns to exclude from link checking.
5671
* @param {object[]} deadLinks - Array to collect dead link information.
5772
* @param {object} counters - Counters to keep track of total and dead links.
5873
*/
59-
async function processFile(filePath, excludePatterns, deadLinks, counters) {
74+
async function processFile(filePath, excludePatterns, counters) {
6075
try {
6176
const content = await fs.readFile(filePath, 'utf-8');
6277
const links = extractLinks(content);
@@ -78,13 +93,14 @@ async function processFile(filePath, excludePatterns, deadLinks, counters) {
7893
if (result.error) {
7994
const errorDescription = getErrorDescription(result.status_code);
8095
console.log(`🔴 [${result.status_code || 'N/A'}] ${link} in file: ${relativeFilePath}`);
81-
deadLinks.push({
96+
const deadLink = {
8297
link: result.link,
8398
status_code: result.status_code,
8499
file: relativeFilePath,
85100
error: result.error,
86101
error_description: errorDescription
87-
});
102+
};
103+
await appendDeadLinkToFile(deadLink);
88104
counters.deadLinks += 1;
89105
} else {
90106
console.log(`✅ [OK] ${link} in file: ${relativeFilePath}`);
@@ -112,12 +128,9 @@ function getErrorDescription(statusCode) {
112128
504: 'Gateway Timeout - The server was acting as a gateway or proxy and did not receive a response in time.',
113129
405: 'Method Not Allowed - The method specified in the Request-Line is not allowed for the resource.',
114130
429: 'Too Many Requests - The user has sent too many requests in a given amount of time.',
115-
503: 'Service Unavailable - The server is currently unavailable (overloaded or down).',
116-
504: 'Gateway Timeout - The server, while acting as a gateway or proxy, did not receive a timely response from an upstream server it needed to access in order to complete the request.',
117131
505: 'HTTP Version Not Supported - The server does not support, or refuses to support, the major version of HTTP that was used in the request message.',
118132
511: 'Network Authentication Required - The client needs to authenticate to gain network access.',
119-
599: 'Network Connect Timeout Error - Can be cause by a network issue or a server-side issue.',
120-
// Add more descriptions as needed
133+
599: 'Network Connect Timeout Error - Can be caused by a network issue or a server-side issue.',
121134
};
122135

123136
return descriptions[statusCode] || 'Unknown error, look up status code for details';
@@ -162,12 +175,14 @@ async function main() {
162175
const excludeFiles = new Set(argv['exclude-files'].map(file => path.resolve(file)));
163176
const excludePatterns = argv['exclude'];
164177

165-
let deadLinks = [];
166178
let counters = {
167179
totalLinks: 0,
168180
deadLinks: 0
169181
};
170182

183+
// Initialize/clear dead_links.json file
184+
await fs.writeJson(path.resolve('dead_links.json'), []);
185+
171186
// Define file patterns to search
172187
const filePatterns = ['**/*.mdx', '**/*.json', '**/*.md'];
173188

@@ -185,26 +200,14 @@ async function main() {
185200
continue;
186201
}
187202

188-
await processFile(file, excludePatterns, deadLinks, counters);
203+
await processFile(file, excludePatterns, counters);
189204
}
190205
}
191206
}
192207

193-
// Write dead links to JSON file
194-
if (deadLinks.length > 0) {
195-
const outputPath = path.resolve('dead_links.json');
196-
try {
197-
await fs.writeJson(outputPath, deadLinks, { spaces: 4 });
198-
console.log(`\n🔗 Dead links have been written to ${outputPath}`);
199-
} catch (error) {
200-
console.error(`❌ Failed to write dead links to JSON: ${error.message}`);
201-
}
202-
} else {
203-
console.log('\n🎉 No dead links found.');
204-
}
205-
206208
// Print summary
207209
console.log(`\n🔍 Summary: Total links checked: ${counters.totalLinks}, Dead links found: ${counters.deadLinks}`);
210+
console.log('\n✅ Scan completed successfully.');
208211
}
209212

210213
main();

0 commit comments

Comments
 (0)