Skip to content

Commit 2473ae3

Browse files
authored
fix: print useful dep-check message (#1248)
Format the dep-check error message to make it easier to read.
1 parent c761502 commit 2473ae3

File tree

9 files changed

+85
-22
lines changed

9 files changed

+85
-22
lines changed

.aegir.js

+21
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,26 @@
22
export default {
33
docs: {
44
entryPoint: 'utils'
5+
},
6+
dependencyCheck: {
7+
ignore: [
8+
'@typescript-eslint/eslint-plugin',
9+
'buffer',
10+
'c8',
11+
'conventional-changelog-conventionalcommits',
12+
'electron-mocha-main',
13+
'mocha',
14+
'npm-package-json-lint',
15+
'nyc',
16+
'path',
17+
'playwright-test',
18+
'react-native-test-runner',
19+
'semantic-release',
20+
'semantic-release-monorepo',
21+
'source-map-support',
22+
'typedoc-plugin-mdn-links',
23+
'typedoc-plugin-missing-exports',
24+
'electron'
25+
]
526
}
627
}

package.json

+2-5
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@
212212
"lint": "node src/index.js lint",
213213
"test": "node src/index.js test",
214214
"docs": "node src/index.js docs",
215-
"dep-check": "node src/index.js dep-check --unused false",
215+
"dep-check": "node src/index.js dep-check",
216216
"doc-check": "node src/index.js doc-check",
217217
"test:node": "node src/index.js test -t node --cov",
218218
"test:chrome": "node src/index.js test -t browser --cov",
@@ -237,7 +237,6 @@
237237
"@types/chai-subset": "^1.3.3",
238238
"@types/mocha": "^10.0.0",
239239
"@types/node": "^18.11.15",
240-
"@types/sinon": "^10.0.0",
241240
"@typescript-eslint/eslint-plugin": "^5.18.0",
242241
"buffer": "^6.0.3",
243242
"bytes": "^3.1.0",
@@ -335,9 +334,7 @@
335334
"@types/semver": "^7.3.4",
336335
"@types/update-notifier": "^6.0.1",
337336
"@types/yargs": "^17.0.0",
338-
"electron": "^24.1.2",
339-
"sinon": "^15.0.0",
340-
"util": "^0.12.4"
337+
"electron": "^24.1.2"
341338
},
342339
"browser": {
343340
"fs": false,

src/dependency-check.js

+40-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
/* eslint-disable no-console */
2+
13
import { cwd } from 'process'
24
import depcheck from 'depcheck'
5+
import kleur from 'kleur'
36
import Listr from 'listr'
47

58
const ignoredDevDependencies = [
@@ -37,13 +40,43 @@ const tasks = new Listr(
3740
},
3841
ignoreMatches: ignoredDevDependencies.concat(ctx.fileConfig.dependencyCheck.ignore).concat(ctx.ignore)
3942
})
40-
if (Object.keys(result.missing).length > 0 || (ctx.unused && (result.dependencies.length > 0 || result.devDependencies.length > 0))) {
41-
throw new Error(
42-
'Some dependencies are missing or unused.\n' +
43-
'Missing: \n' + Object.entries(result.missing).map(([dep, path]) => dep + ': ' + path).join('\n') +
44-
'\nUnused production dependencies: \n' + result.dependencies.join('\n') + '\n' +
45-
'Unused dev dependencies: \n' + result.devDependencies.join('\n')
46-
)
43+
44+
if (Object.keys(result.missing).length > 0 || result.dependencies.length > 0 || result.devDependencies.length > 0) {
45+
if (Object.keys(result.missing).length > 0) {
46+
console.error('')
47+
console.error('Missing dependencies:')
48+
console.error('')
49+
50+
Object.entries(result.missing).forEach(([dep, path]) => {
51+
console.error(kleur.red(dep))
52+
console.error(' ', kleur.gray(path.join('\n ')))
53+
})
54+
}
55+
56+
if (result.dependencies.length > 0) {
57+
console.error('')
58+
console.error('Unused production dependencies:')
59+
console.error('')
60+
61+
result.dependencies.forEach(dep => {
62+
console.error(kleur.yellow(dep))
63+
})
64+
}
65+
66+
if (result.devDependencies.length > 0) {
67+
console.error('')
68+
console.error('Unused dev dependencies:')
69+
console.error('')
70+
71+
result.devDependencies.forEach(dep => {
72+
console.error(kleur.yellow(dep))
73+
})
74+
}
75+
76+
// necessary because otherwise listr removes the last line of output
77+
console.error(' ')
78+
79+
throw new Error('Some dependencies are missing or unused')
4780
}
4881
}
4982
}

test/dependency-check.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,23 @@ describe('dependency check', () => {
4343

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

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

6060
it('should pass when there are no missing deps in an ts project', async () => {
6161
await expect(
62-
execa(bin, ['dependency-check', '-u', 'false'], {
62+
execa(bin, ['dependency-check'], {
6363
cwd: path.join(__dirname, 'fixtures/dependency-check/ts-pass')
6464
})
6565
).to.eventually.be.fulfilled()
@@ -68,11 +68,9 @@ describe('dependency check', () => {
6868
it('should check unused', async () => {
6969
await expect(
7070
execa(bin, ['dependency-check'], {
71-
cwd: path.join(__dirname, 'fixtures/dependency-check/pass')
71+
cwd: path.join(__dirname, 'fixtures/dependency-check/fail-unused')
7272
})
73-
).to.eventually.be.rejected.with.property('message').that.include(
74-
'Unused production dependencies: \npico'
75-
)
73+
).to.eventually.be.rejectedWith('pico')
7674
})
7775

7876
/**
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
// @ts-nocheck
21
/* eslint-disable no-unused-vars */
3-
import { execa } from 'execa'
2+
// @ts-ignore
3+
const pico = require('pico')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/* eslint-disable no-unused-vars */
2+
// @ts-expect-error unused
3+
import { execa } from 'execa'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "dep-check-fail",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"dependencies": {
6+
"execa": "1.0.0",
7+
"pico": "1.0.0"
8+
}
9+
}

test/fixtures/dependency-check/pass/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33

44
// @ts-ignore
55
import { execa } from 'execa'
6+
// @ts-ignore
7+
import pico from 'pico'
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
/* eslint-disable @typescript-eslint/no-unused-vars */
22
// @ts-expect-error unused
3-
import { execa } from 'execa'
3+
import pico from 'pico'

0 commit comments

Comments
 (0)