Skip to content

Commit

Permalink
Merge pull request #20 from justindhillon/performance-improvements
Browse files Browse the repository at this point in the history
Performance Improvements
  • Loading branch information
justindhillon authored Nov 22, 2023
2 parents 8859707 + 8e9fa0c commit 89c4f6b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
6 changes: 5 additions & 1 deletion bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ if (path === "help") {
process.exit(0);
}

scanLinks(path);
scanLinks(path).then(() => {
process.exit(0);
}).catch(() => {
process.exit(1);
});
38 changes: 24 additions & 14 deletions bin/scan/scanLinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const help = require("../help.js");
const fs = require('fs');

// path is <file/directory path>
function links(path) {
async function scanLinks(path) {
// Error: path does not exist
if (!fs.existsSync(path)) {
console.error('Error:', path, 'does not exist');
Expand All @@ -26,21 +26,31 @@ function links(path) {
const lastSlashIndex = removeSlash.lastIndexOf('/');
const fluff = removeSlash.substring(0, lastSlashIndex + 1);

filePaths.forEach(async (filePath) => {
// gets content of path
const fileContent = readFile(filePath);

// gets array of links from fileContent
const links = getLinks(fileContent);
console.log("If nothing is output below, no broken links where found");

// if any broken links are found, it writes
// them to an "output" folder
if (links !== null) {
await writeBrokenLinks(links, filePath, fluff);
}
let promises = filePaths.map(filePath => {
return new Promise(async (resolve, reject) => {
try {
const fileContent = readFile(filePath);
const links = getLinks(fileContent);

if (links !== null) {
await writeBrokenLinks(links, filePath, fluff);
}
resolve();
} catch (error) {
reject(error);
}
});
});

console.log("If nothing is output below, no broken links where found");
await Promise.all(promises)
.then(() => {
console.log("Finished!");
})
.catch(error => {
console.error("An error occurred:", error);
});
}

module.exports = links;
module.exports = scanLinks;
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
"version": "1.1.0",
"description": "A npx package that automatically scans files and directories broken links.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"bin": {
"link-inspector": "bin/index.js"
},
Expand Down

0 comments on commit 89c4f6b

Please sign in to comment.