diff --git a/bin/index.js b/bin/index.js index 1656c1e..4635aaa 100755 --- a/bin/index.js +++ b/bin/index.js @@ -17,4 +17,8 @@ if (path === "help") { process.exit(0); } -scanLinks(path); +scanLinks(path).then(() => { + process.exit(0); +}).catch(() => { + process.exit(1); +}); diff --git a/bin/scan/scanLinks.js b/bin/scan/scanLinks.js index 6964ddb..e7e0250 100755 --- a/bin/scan/scanLinks.js +++ b/bin/scan/scanLinks.js @@ -6,7 +6,7 @@ const help = require("../help.js"); const fs = require('fs'); // path is -function links(path) { +async function scanLinks(path) { // Error: path does not exist if (!fs.existsSync(path)) { console.error('Error:', path, 'does not exist'); @@ -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; diff --git a/package.json b/package.json index 01e988a..e69bb78 100644 --- a/package.json +++ b/package.json @@ -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" },