1 | #!/usr/bin/env node
|
2 |
|
3 | 'use strict'
|
4 |
|
5 | const requiredNodeEngineMinimum = parseInt(require('./package.json').engines.node.match(/^>=(\d+)\./)[1], 10)
|
6 | const currentNodeEngine = parseInt(process.version.match(/^v(\d+)\./)[1], 10)
|
7 |
|
8 | if (currentNodeEngine < requiredNodeEngineMinimum) {
|
9 | console.error('dependency-check: Node ' + requiredNodeEngineMinimum + ' or greater is required. `dependency-check` did not run.')
|
10 | process.exit(0)
|
11 | }
|
12 |
|
13 | const check = require('./')
|
14 |
|
15 | const args = require('minimist')(process.argv.slice(2), {
|
16 | default: {
|
17 | missing: false,
|
18 | unused: false,
|
19 | dev: true,
|
20 | 'default-entries': true,
|
21 | verbose: false
|
22 | },
|
23 | boolean: ['missing', 'unused', 'dev', 'version', 'ignore', 'default-entries', 'verbose'],
|
24 | alias: {
|
25 | 'ignore-module': 'i',
|
26 | extensions: 'e'
|
27 | }
|
28 | })
|
29 |
|
30 | if (args.version) {
|
31 | console.log(require('./package.json').version)
|
32 | process.exit(1)
|
33 | }
|
34 |
|
35 | if (args.help || args._.length === 0) {
|
36 | console.log('\nUsage: dependency-check <path to entry file, package.json or module folder> <additional entry paths to add> <options>')
|
37 | console.log('\nEntry paths supports globbing for easy adding of eg. entire folders.')
|
38 | console.log('\nOptions:')
|
39 | console.log('--missing Only check to make sure that all modules in your code are listed in your package.json')
|
40 | console.log('--unused Only check which modules listed in your package.json *are not* used in your code')
|
41 | console.log("--no-dev Won't tell you about devDependencies that are missing or unused")
|
42 | console.log("--no-peer Won't tell you about peerDependencies that are missing or unused")
|
43 | console.log("--ignore-module, -i Won't tell you about these module names when missing or unused. Supports globbing")
|
44 | console.log("--no-default-entries Won't parse your main and bin entries from package.json even when a package.json or module folder has been defined")
|
45 | console.log('--detective Requireable path containing an alternative implementation of the detective module that supports alternate syntaxes')
|
46 | console.log("--extensions, -e List of file extensions with detective to use when resolving require paths. Eg. 'js,jsx:detective-es6'")
|
47 | console.log('--version Show current version')
|
48 | console.log('--ignore To always exit with code 0 pass --ignore')
|
49 | console.log('--verbose Enable logging of eg. success message')
|
50 | console.log('')
|
51 |
|
52 | process.exit(1)
|
53 | }
|
54 |
|
55 | function extensions (arg) {
|
56 | if (!arg) return undefined
|
57 | const extensions = {}
|
58 |
|
59 | function add (value) {
|
60 | const parts = value.trim().split(':', 2)
|
61 |
|
62 | parts[0].split(',').forEach(function (ext) {
|
63 | extensions[ext.charAt(0) === '.' ? ext : '.' + ext] = parts[1]
|
64 | })
|
65 | }
|
66 |
|
67 | if (typeof arg === 'string') {
|
68 | add(arg)
|
69 | } else {
|
70 | arg.forEach(add)
|
71 | }
|
72 |
|
73 | return extensions
|
74 | }
|
75 |
|
76 | check({
|
77 | path: args._.shift(),
|
78 | entries: args._,
|
79 | noDefaultEntries: !args['default-entries'],
|
80 | extensions: extensions(args.e),
|
81 | detective: args.detective
|
82 | })
|
83 | .then(data => {
|
84 | const pkg = data.package
|
85 | const deps = data.used
|
86 | let failed = 0
|
87 | const options = {
|
88 | excludeDev: args.dev === false,
|
89 | excludePeer: args.peer === false,
|
90 | ignore: [].concat(args.i || [])
|
91 | }
|
92 |
|
93 | const runAllTests = !args.extra && !args.missing
|
94 |
|
95 | if (runAllTests || args.unused) {
|
96 | const extras = check.extra(pkg, deps, options)
|
97 | failed += extras.length
|
98 | if (extras.length) {
|
99 | console.error('Fail! Modules in package.json not used in code: ' + extras.join(', '))
|
100 | } else if (args.verbose) {
|
101 | console.log('Success! All dependencies in package.json are used in the code')
|
102 | }
|
103 | }
|
104 | if (runAllTests || args.missing) {
|
105 | const optionsForMissingCheck = runAllTests
|
106 | ? Object.assign({}, options, {
|
107 | excludeDev: false,
|
108 | excludePeer: false
|
109 | })
|
110 | : options
|
111 |
|
112 | const missing = check.missing(pkg, deps, optionsForMissingCheck)
|
113 |
|
114 | failed += missing.length
|
115 |
|
116 | if (missing.length) {
|
117 | console.error('Fail! Dependencies not listed in package.json: ' + missing.join(', '))
|
118 | } else if (args.verbose) {
|
119 | console.log('Success! All dependencies used in the code are listed in package.json')
|
120 | }
|
121 | }
|
122 | process.exit(args.ignore || !failed ? 0 : 1)
|
123 | })
|
124 | .catch(err => {
|
125 | console.error(err.message)
|
126 | process.exit(1)
|
127 | })
|