@@ -6,7 +6,7 @@ const help = require("../help.js");
6
6
const fs = require ( 'fs' ) ;
7
7
8
8
// path is <file/directory path>
9
- function links ( path ) {
9
+ async function scanLinks ( path ) {
10
10
// Error: path does not exist
11
11
if ( ! fs . existsSync ( path ) ) {
12
12
console . error ( 'Error:' , path , 'does not exist' ) ;
@@ -26,21 +26,31 @@ function links(path) {
26
26
const lastSlashIndex = removeSlash . lastIndexOf ( '/' ) ;
27
27
const fluff = removeSlash . substring ( 0 , lastSlashIndex + 1 ) ;
28
28
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" ) ;
35
30
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
+ } ) ;
41
45
} ) ;
42
46
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
+ } ) ;
44
54
}
45
55
46
- module . exports = links ;
56
+ module . exports = scanLinks ;
0 commit comments