Skip to content

Commit

Permalink
fix: print useful dep-check message (#1248)
Browse files Browse the repository at this point in the history
Format the dep-check error message to make it easier to read.
  • Loading branch information
achingbrain committed Apr 27, 2023
1 parent c761502 commit 2473ae3
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 22 deletions.
21 changes: 21 additions & 0 deletions .aegir.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,26 @@
export default {
docs: {
entryPoint: 'utils'
},
dependencyCheck: {
ignore: [
'@typescript-eslint/eslint-plugin',
'buffer',
'c8',
'conventional-changelog-conventionalcommits',
'electron-mocha-main',
'mocha',
'npm-package-json-lint',
'nyc',
'path',
'playwright-test',
'react-native-test-runner',
'semantic-release',
'semantic-release-monorepo',
'source-map-support',
'typedoc-plugin-mdn-links',
'typedoc-plugin-missing-exports',
'electron'
]
}
}
7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@
"lint": "node src/index.js lint",
"test": "node src/index.js test",
"docs": "node src/index.js docs",
"dep-check": "node src/index.js dep-check --unused false",
"dep-check": "node src/index.js dep-check",
"doc-check": "node src/index.js doc-check",
"test:node": "node src/index.js test -t node --cov",
"test:chrome": "node src/index.js test -t browser --cov",
Expand All @@ -237,7 +237,6 @@
"@types/chai-subset": "^1.3.3",
"@types/mocha": "^10.0.0",
"@types/node": "^18.11.15",
"@types/sinon": "^10.0.0",
"@typescript-eslint/eslint-plugin": "^5.18.0",
"buffer": "^6.0.3",
"bytes": "^3.1.0",
Expand Down Expand Up @@ -335,9 +334,7 @@
"@types/semver": "^7.3.4",
"@types/update-notifier": "^6.0.1",
"@types/yargs": "^17.0.0",
"electron": "^24.1.2",
"sinon": "^15.0.0",
"util": "^0.12.4"
"electron": "^24.1.2"
},
"browser": {
"fs": false,
Expand Down
47 changes: 40 additions & 7 deletions src/dependency-check.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/* eslint-disable no-console */

import { cwd } from 'process'
import depcheck from 'depcheck'
import kleur from 'kleur'
import Listr from 'listr'

const ignoredDevDependencies = [
Expand Down Expand Up @@ -37,13 +40,43 @@ const tasks = new Listr(
},
ignoreMatches: ignoredDevDependencies.concat(ctx.fileConfig.dependencyCheck.ignore).concat(ctx.ignore)
})
if (Object.keys(result.missing).length > 0 || (ctx.unused && (result.dependencies.length > 0 || result.devDependencies.length > 0))) {
throw new Error(
'Some dependencies are missing or unused.\n' +
'Missing: \n' + Object.entries(result.missing).map(([dep, path]) => dep + ': ' + path).join('\n') +
'\nUnused production dependencies: \n' + result.dependencies.join('\n') + '\n' +
'Unused dev dependencies: \n' + result.devDependencies.join('\n')
)

if (Object.keys(result.missing).length > 0 || result.dependencies.length > 0 || result.devDependencies.length > 0) {
if (Object.keys(result.missing).length > 0) {
console.error('')
console.error('Missing dependencies:')
console.error('')

Object.entries(result.missing).forEach(([dep, path]) => {
console.error(kleur.red(dep))
console.error(' ', kleur.gray(path.join('\n ')))
})
}

if (result.dependencies.length > 0) {
console.error('')
console.error('Unused production dependencies:')
console.error('')

result.dependencies.forEach(dep => {
console.error(kleur.yellow(dep))
})
}

if (result.devDependencies.length > 0) {
console.error('')
console.error('Unused dev dependencies:')
console.error('')

result.devDependencies.forEach(dep => {
console.error(kleur.yellow(dep))
})
}

// necessary because otherwise listr removes the last line of output
console.error(' ')

throw new Error('Some dependencies are missing or unused')
}
}
}
Expand Down
12 changes: 5 additions & 7 deletions test/dependency-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,23 @@ describe('dependency check', () => {

it('should pass when there are no missing deps', async () => {
await expect(
execa(bin, ['dependency-check', '-u', 'false'], {
execa(bin, ['dependency-check'], {
cwd: path.join(__dirname, 'fixtures/dependency-check/pass')
})
).to.eventually.be.fulfilled()
})

it('should pass when there are no missing deps in an esm project', async () => {
await expect(
execa(bin, ['dependency-check', '-u', 'false'], {
execa(bin, ['dependency-check'], {
cwd: path.join(__dirname, 'fixtures/dependency-check/esm-pass')
})
).to.eventually.be.fulfilled()
})

it('should pass when there are no missing deps in an ts project', async () => {
await expect(
execa(bin, ['dependency-check', '-u', 'false'], {
execa(bin, ['dependency-check'], {
cwd: path.join(__dirname, 'fixtures/dependency-check/ts-pass')
})
).to.eventually.be.fulfilled()
Expand All @@ -68,11 +68,9 @@ describe('dependency check', () => {
it('should check unused', async () => {
await expect(
execa(bin, ['dependency-check'], {
cwd: path.join(__dirname, 'fixtures/dependency-check/pass')
cwd: path.join(__dirname, 'fixtures/dependency-check/fail-unused')
})
).to.eventually.be.rejected.with.property('message').that.include(
'Unused production dependencies: \npico'
)
).to.eventually.be.rejectedWith('pico')
})

/**
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/dependency-check/esm-pass/src/other.cjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// @ts-nocheck
/* eslint-disable no-unused-vars */
import { execa } from 'execa'
// @ts-ignore
const pico = require('pico')
3 changes: 3 additions & 0 deletions test/fixtures/dependency-check/fail-unused/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* eslint-disable no-unused-vars */
// @ts-expect-error unused
import { execa } from 'execa'
9 changes: 9 additions & 0 deletions test/fixtures/dependency-check/fail-unused/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "dep-check-fail",
"version": "1.0.0",
"main": "index.js",
"dependencies": {
"execa": "1.0.0",
"pico": "1.0.0"
}
}
2 changes: 2 additions & 0 deletions test/fixtures/dependency-check/pass/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@

// @ts-ignore
import { execa } from 'execa'
// @ts-ignore
import pico from 'pico'
2 changes: 1 addition & 1 deletion test/fixtures/dependency-check/ts-pass/src/other.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
// @ts-expect-error unused
import { execa } from 'execa'
import pico from 'pico'

0 comments on commit 2473ae3

Please sign in to comment.