Skip to content

Commit 89c4f6b

Browse files
Merge pull request #20 from justindhillon/performance-improvements
Performance Improvements
2 parents 8859707 + 8e9fa0c commit 89c4f6b

File tree

3 files changed

+29
-18
lines changed

3 files changed

+29
-18
lines changed

bin/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,8 @@ if (path === "help") {
1717
process.exit(0);
1818
}
1919

20-
scanLinks(path);
20+
scanLinks(path).then(() => {
21+
process.exit(0);
22+
}).catch(() => {
23+
process.exit(1);
24+
});

bin/scan/scanLinks.js

Lines changed: 24 additions & 14 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-
function links(path) {
9+
async function scanLinks(path) {
1010
// Error: path does not exist
1111
if (!fs.existsSync(path)) {
1212
console.error('Error:', path, 'does not exist');
@@ -26,21 +26,31 @@ function links(path) {
2626
const lastSlashIndex = removeSlash.lastIndexOf('/');
2727
const fluff = removeSlash.substring(0, lastSlashIndex + 1);
2828

29-
filePaths.forEach(async (filePath) => {
30-
// gets content of path
31-
const fileContent = readFile(filePath);
32-
33-
// gets array of links from fileContent
34-
const links = getLinks(fileContent);
29+
console.log("If nothing is output below, no broken links where found");
3530

36-
// if any broken links are found, it writes
37-
// them to an "output" folder
38-
if (links !== null) {
39-
await writeBrokenLinks(links, filePath, fluff);
40-
}
31+
let promises = filePaths.map(filePath => {
32+
return new Promise(async (resolve, reject) => {
33+
try {
34+
const fileContent = readFile(filePath);
35+
const links = getLinks(fileContent);
36+
37+
if (links !== null) {
38+
await writeBrokenLinks(links, filePath, fluff);
39+
}
40+
resolve();
41+
} catch (error) {
42+
reject(error);
43+
}
44+
});
4145
});
4246

43-
console.log("If nothing is output below, no broken links where found");
47+
await Promise.all(promises)
48+
.then(() => {
49+
console.log("Finished!");
50+
})
51+
.catch(error => {
52+
console.error("An error occurred:", error);
53+
});
4454
}
4555

46-
module.exports = links;
56+
module.exports = scanLinks;

package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
"version": "1.1.0",
44
"description": "A npx package that automatically scans files and directories broken links.",
55
"main": "index.js",
6-
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
8-
},
96
"bin": {
107
"link-inspector": "bin/index.js"
118
},

0 commit comments

Comments
 (0)