1
- import path from 'path'
2
- import { execa } from 'execa'
3
- import merge from 'merge-options'
4
- import { pkg } from './utils.js'
5
- import { fileURLToPath } from 'url'
6
1
import Listr from 'listr'
7
-
8
- const __dirname = path . dirname ( fileURLToPath ( import . meta . url ) )
2
+ import depcheck from 'depcheck'
3
+ import { cwd } from 'process'
9
4
10
5
/**
11
6
* @typedef {import("listr").ListrTaskWrapper } Task
@@ -22,67 +17,23 @@ const tasks = new Listr(
22
17
* @param {Task } task
23
18
*/
24
19
task : async ( ctx , task ) => {
25
- const forwardOptions = ctx [ '--' ] ? ctx [ '--' ] : [ ]
26
- const input = ctx . input . length > 0 ? ctx . input : ctx . fileConfig . dependencyCheck . input
27
- const ignore = ctx . ignore
28
- . concat ( ctx . fileConfig . dependencyCheck . ignore )
29
- . reduce ( ( acc , i ) => acc . concat ( '-i' , i ) , /** @type {string[] } */ ( [ ] ) )
30
-
31
- const args = [ ...input , '--missing' , ...ignore ]
32
-
33
- if ( pkg . type === 'module' ) {
34
- // use detective-es6 for js, regular detective for cjs
35
- args . push (
36
- '--extensions' , 'cjs:detective-cjs' ,
37
- '--extensions' , 'js:detective-es6'
38
- )
39
- }
40
-
41
- await execa (
42
- 'dependency-check' ,
43
- [ ...args , ...forwardOptions ] ,
44
- merge (
45
- {
46
- localDir : path . join ( __dirname , '..' ) ,
47
- preferLocal : true
48
- }
49
- )
50
- )
51
- }
52
- } ,
53
- {
54
- title : 'dependency-check (production only)' ,
55
- /**
56
- * @param {GlobalOptions & DependencyCheckOptions } ctx
57
- * @param {Task } task
58
- */
59
- task : async ( ctx , task ) => {
60
- const forwardOptions = ctx [ '--' ] ? ctx [ '--' ] : [ ]
61
- const input = ctx . input . length > 0 ? ctx . input : ctx . fileConfig . dependencyCheck . productionInput
62
- const ignore = ctx . ignore
63
- . concat ( ctx . fileConfig . dependencyCheck . ignore )
64
- . reduce ( ( acc , i ) => acc . concat ( '-i' , i ) , /** @type {string[] } */ ( [ ] ) )
65
-
66
- const args = [ ...input , '--missing' , '--no-dev' , ...ignore ]
67
-
68
- if ( pkg . type === 'module' ) {
69
- // use detective-es6 for js, regular detective for cjs
70
- args . push (
71
- '--extensions' , 'cjs:detective-cjs' ,
72
- '--extensions' , 'js:detective-es6'
20
+ const result = await depcheck ( cwd ( ) , {
21
+ parsers : {
22
+ '**/*.js' : depcheck . parser . es6 ,
23
+ '**/*.ts' : depcheck . parser . typescript ,
24
+ '**/*.cjs' : depcheck . parser . es6 ,
25
+ '**/*.mjs' : depcheck . parser . es6
26
+ } ,
27
+ ignoreMatches : [ 'eslint*' , '@types/*' , '@semantic-release/*' ] . concat ( ctx . fileConfig . dependencyCheck . ignore ) . concat ( ctx . ignore )
28
+ } )
29
+ if ( Object . keys ( result . missing ) . length > 0 || ( ctx . unused && ( result . dependencies . length > 0 || result . devDependencies . length > 0 ) ) ) {
30
+ throw new Error (
31
+ 'Some dependencies are missing or unused.\n' +
32
+ 'Missing: \n' + Object . entries ( result . missing ) . map ( ( [ dep , path ] ) => dep + ': ' + path ) . join ( '\n' ) +
33
+ '\nUnused production dependencies: \n' + result . dependencies . join ( '\n' ) + '\n' +
34
+ 'Unused dev dependencies: \n' + result . devDependencies . join ( '\n' )
73
35
)
74
36
}
75
-
76
- await execa (
77
- 'dependency-check' ,
78
- [ ...args , ...forwardOptions ] ,
79
- merge (
80
- {
81
- localDir : path . join ( __dirname , '..' ) ,
82
- preferLocal : true
83
- }
84
- )
85
- )
86
37
}
87
38
}
88
39
]
0 commit comments