Skip to content

Commit 51b43ff

Browse files
Constantin Krügeruniqueck
authored andcommitted
Fix: find all files in a directory (#14)
1 parent 1b708a3 commit 51b43ff

File tree

3 files changed

+56
-13
lines changed

3 files changed

+56
-13
lines changed

lib/allChecksRunner.js

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
const fs = require('fs')
21
const HtmlPage = require('./html/htmlPage')
32
const SinglePageResult = require('./singlePageResult')
43
const Logger = require('./logging/LoggingFacacde')
@@ -12,23 +11,16 @@ class AllChecksRunner {
1211
async performAllChecks () {
1312
const resultsForAllPages = []
1413

15-
fs.readdir(this.config.sourceDir, { recursive: this.config.recursive, withFileTypes: true }, (err, files) => {
16-
if (!err) {
17-
files.filter((file) => {
18-
return file.isFile() && file.name.toLowerCase().endsWith('.html')
19-
}).forEach((file) => {
20-
const singlePageResult = this.performCheckForOneFile(file)
21-
resultsForAllPages.push(singlePageResult)
22-
})
23-
} else {
24-
console.log(err)
25-
}
14+
const files = require('./utils').findFiles(this.config.sourceDir, this.config.recursive, this.config.extension)
15+
files.forEach((file) => {
16+
const singlePageResult = this.performCheckForOneFile(file)
17+
resultsForAllPages.push(singlePageResult)
2618
})
2719
return resultsForAllPages
2820
}
2921

3022
performCheckForOneFile (file) {
31-
const htmlPage = new HtmlPage({ filePath: file.parentPath, fileName: file.name })
23+
const htmlPage = new HtmlPage({ filePath: file.filePath, fileName: file.fileName })
3224
const singlePageResult = new SinglePageResult(htmlPage)
3325

3426
const { checker = [] } = this.config

lib/utils.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,24 @@
33
module.exports = {
44
cwd: function cwd () {
55
return process.cwd()
6+
},
7+
findFiles: function (/* string */ sourceDir, /* boolean */ recursive, /* string[] */ fileExtensions) {
8+
const fs = require('fs')
9+
const path = require('path')
10+
const files = fs.readdirSync(sourceDir, { recursive, withFileTypes: false })
11+
return files.filter((file) => {
12+
let fileExt = path.extname(file).toLowerCase()
13+
if (fileExt) {
14+
fileExt = fileExt.substring(1)
15+
}
16+
return fileExtensions.includes(fileExt)
17+
}).map((file) => {
18+
return {
19+
filePath: path.join(sourceDir, path.dirname(file)),
20+
fileName: path.basename(file)
21+
}
22+
}
23+
)
624
}
25+
726
}

test/node-unit/utils.spec.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/* global describe it */
2+
'use strict'
3+
4+
const { findFiles, cwd } = require('../../lib/utils')
5+
const chai = require('chai')
6+
const path = require('path')
7+
const expect = chai.expect
8+
9+
describe('findFiles', () => {
10+
it('non recursive', () => {
11+
const files = findFiles(`${cwd()}/test/fixtures`, false, ['html'])
12+
expect(files).to.have.lengthOf(1)
13+
expect(files).has.deep.members([
14+
{ filePath: path.join(cwd(), 'test', 'fixtures'), fileName: 'file-to-test.html' }
15+
])
16+
})
17+
it('recursive', () => {
18+
const files = findFiles(`${cwd()}/test/fixtures`, true, ['html'])
19+
expect(files).to.have.lengthOf(5)
20+
expect(files).has.deep.members([
21+
{ filePath: path.join(cwd(), 'test', 'fixtures'), fileName: 'file-to-test.html' },
22+
{ filePath: path.join(cwd(), 'test', 'fixtures', 'html-files'), fileName: 'brokenHttpLinksChecker_127_0_0_2.html' },
23+
{ filePath: path.join(cwd(), 'test', 'fixtures', 'html-files'), fileName: 'brokenHttpLinksChecker_github.com.html' },
24+
{ filePath: path.join(cwd(), 'test', 'fixtures', 'html-files'), fileName: 'brokenHttpLinksChecker_localhost.html' },
25+
{ filePath: path.join(cwd(), 'test', 'fixtures', 'sub_dr'), fileName: 'brokenHttpLinksChecker.html' }
26+
])
27+
})
28+
it('empty extensions', () => {
29+
const files = findFiles(`${cwd()}/test/fixtures`, true, [])
30+
expect(files).to.have.lengthOf(0)
31+
})
32+
})

0 commit comments

Comments
 (0)