Skip to content

Commit

Permalink
audit all files uploaded (recursive), tweak info output (#828)
Browse files Browse the repository at this point in the history
Co-authored-by: Amulya Kashyap <[email protected]>
  • Loading branch information
purplecabbage and Amulya Kashyap authored Jan 6, 2025
1 parent 88c2bc7 commit 58739ea
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
36 changes: 25 additions & 11 deletions src/lib/audit-logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,14 @@ 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}.`)))
return log
}

const fileTypeCounts = {}

files.forEach(file => {
const ext = path.extname(file).toLowerCase() || 'no extension'
if (fileTypeCounts[ext]) {
Expand All @@ -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
}

Expand Down
10 changes: 6 additions & 4 deletions test/commands/lib/audit-logger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
)
Expand All @@ -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'
])
})
Expand Down

0 comments on commit 58739ea

Please sign in to comment.