Skip to content

Commit 571e143

Browse files
Merge pull request #19 from justindhillon/performance-improvements
Performance Improvements
2 parents 6812299 + 8f7349c commit 571e143

File tree

4 files changed

+24
-14
lines changed

4 files changed

+24
-14
lines changed

bin/scan/scanLinks.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const help = require("../help.js");
66
const fs = require('fs');
77

88
// path is <file/directory path>
9-
async function links(path) {
9+
function links(path) {
1010
// Error: path does not exist
1111
if (!fs.existsSync(path)) {
1212
console.error('Error:', path, 'does not exist');
@@ -26,7 +26,7 @@ async function links(path) {
2626
const lastSlashIndex = removeSlash.lastIndexOf('/');
2727
const fluff = removeSlash.substring(0, lastSlashIndex + 1);
2828

29-
filePaths.forEach((filePath) => {
29+
filePaths.forEach(async (filePath) => {
3030
// gets content of path
3131
const fileContent = readFile(filePath);
3232

@@ -36,7 +36,7 @@ async function links(path) {
3636
// if any broken links are found, it writes
3737
// them to an "output" folder
3838
if (links !== null) {
39-
writeBrokenLinks(links, filePath, fluff);
39+
await writeBrokenLinks(links, filePath, fluff);
4040
}
4141
});
4242

bin/scan/writeBrokenLinks.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ function isLocalhostUrl(url) {
1111
// path is <file/directory path>
1212
// Writes data to identical path in "output"
1313
function writeToFile(data, PATH, fluff) {
14-
console.log(fluff, PATH);
1514
PATH = PATH.replace(fluff, '');
1615
PATH = "output/" + PATH;
1716
const directoryPath = path.dirname(PATH);
@@ -38,18 +37,29 @@ function writeToFile(data, PATH, fluff) {
3837
// in an "output" folder
3938
async function writeBrokenLinks(links, PATH, fluff) {
4039
for (const link of links) {
41-
await new Promise(r => setTimeout(r, 2000));
4240
if (isLocalhostUrl(link)) { continue }
43-
linkCheck(link, function (err, result) {
44-
if (err) {
45-
console.error('Error: failed to validate', link);
46-
process.exit(1);
47-
}
41+
42+
try {
43+
const result = await new Promise((resolve, reject) => {
44+
linkCheck(link, function (err, result) {
45+
if (err) {
46+
reject(err);
47+
} else {
48+
resolve(result);
49+
}
50+
});
51+
});
52+
4853
if (result.status === "dead") {
4954
writeToFile(link, PATH, fluff);
5055
}
51-
});
56+
} catch (err) {
57+
console.error('Error: failed to validate', link, err);
58+
// Handle the error as needed, maybe continue to the next link instead of exiting
59+
}
5260
}
61+
62+
return;
5363
};
5464

5565
module.exports = writeBrokenLinks;

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "link-inspector",
3-
"version": "1.0.5",
3+
"version": "1.1.0",
44
"description": "A npx package that automatically scans files and directories broken links.",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)