diff --git a/src/lib/audit-logger.js b/src/lib/audit-logger.js index c2f4d28b..578fd583 100644 --- a/src/lib/audit-logger.js +++ b/src/lib/audit-logger.js @@ -101,7 +101,7 @@ function getFilesCountWithExtension (directory) { return log } - const files = fs.readdirSync(directory) + const files = fs.readdirSync(directory, { recursive: true }) if (files.length === 0) { this.log(chalk.red(chalk.bold(`Error: No files found in directory ${directory}.`))) @@ -109,7 +109,6 @@ function getFilesCountWithExtension (directory) { } const fileTypeCounts = {} - files.forEach(file => { const ext = path.extname(file).toLowerCase() || 'no extension' if (fileTypeCounts[ext]) { @@ -122,17 +121,32 @@ function getFilesCountWithExtension (directory) { Object.keys(fileTypeCounts).forEach(ext => { const count = fileTypeCounts[ext] let description - - if (ext === '.js') description = 'Javascript file(s)' - else if (ext === '.css') description = 'CSS file(s)' - else if (ext === '.html') description = 'HTML page(s)' - else if (['.png', '.jpg', '.jpeg', '.gif', '.svg', '.webp'].includes(ext)) description = 'image(s)' - else if (ext === 'no extension') description = 'file(s) without extension' - else description = `${ext} file(s)` - + switch (ext) { + case '.js': + description = 'Javascript file(s)' + break + case '.css': + description = 'CSS file(s)' + break + case '.html': + description = 'HTML page(s)' + break + case '.png': + case '.jpg': + case '.jpeg': + case '.gif': + case '.svg': + case '.webp': + description = `${ext} image(s)` + break + case 'no extension': + description = 'file(s) without extension' + break + default: + description = `${ext} file(s)` + } log.push(`${count} ${description}\n`) }) - return log } diff --git a/test/commands/lib/audit-logger.test.js b/test/commands/lib/audit-logger.test.js index 9847b959..33342099 100644 --- a/test/commands/lib/audit-logger.test.js +++ b/test/commands/lib/audit-logger.test.js @@ -259,7 +259,7 @@ describe('getFilesCountWithExtension', () => { const result = auditLogger.getFilesCountWithExtension.call({ log: mockLog }, directory) - expect(fs.readdirSync).toHaveBeenCalledWith(directory) + expect(fs.readdirSync).toHaveBeenCalledWith(directory, { recursive: true }) expect(mockLog).toHaveBeenCalledWith( 'Error: No files found in directory __fixtures__/app/web-src.' ) @@ -268,15 +268,17 @@ describe('getFilesCountWithExtension', () => { it('should return a count of different file types', () => { fs.existsSync.mockReturnValue(true) - fs.readdirSync.mockReturnValue(['index.html', 'script.js', 'styles.css', 'image.png', 'readme']) + fs.readdirSync.mockReturnValue(['index.html', 'script.js', 'styles.css', 'image.png', 'image.jpg', 'readme']) const result = auditLogger.getFilesCountWithExtension.call({ log: mockLog }, directory) - + // this really should be 2 image(s) but there is a side effect in the code that makes it split by ext + // and this makes more sense than seeing 1 image(s), 1 image(s) expect(result).toEqual([ '1 HTML page(s)\n', '1 Javascript file(s)\n', '1 CSS file(s)\n', - '1 image(s)\n', + '1 .png image(s)\n', + '1 .jpg image(s)\n', '1 file(s) without extension\n' ]) })