Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
feat: add cli flag --json (#160)
Browse files Browse the repository at this point in the history
* feat: add cli flag --json

If the --json flag is used the missing and unused dependencies are printed as json object

Signed-off-by: Merzough Münker <mmuenker@digitaix.com>

* Only log the options selected, including  in JSON

* Add .npmrc that disables lock file

Co-authored-by: Pelle Wessman <pelle@kodfabrik.se>
  • Loading branch information
mmuenker and voxpelli authored Jun 30, 2021
1 parent ce1ef3a commit dbbcd6e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
39 changes: 29 additions & 10 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ const args = require('minimist')(process.argv.slice(2), {
unused: false,
dev: true,
'default-entries': true,
verbose: false
verbose: false,
json: false,
},
'boolean': ['missing', 'unused', 'dev', 'version', 'ignore', 'default-entries', 'verbose'],
'boolean': ['missing', 'unused', 'dev', 'version', 'ignore', 'default-entries', 'verbose', 'json'],
alias: {
'ignore-module': 'i',
extensions: 'e'
extensions: 'e',
json: 'j',
}
})

Expand All @@ -53,6 +55,7 @@ if (args.help || args._.length === 0) {
console.log("--extensions, -e List of file extensions with detective to use when resolving require paths. Eg. 'js,jsx:detective-es6'")
console.log('--version Show current version')
console.log('--ignore To always exit with code 0 pass --ignore')
console.log('--json -j Format the output as json object')
console.log('--verbose Enable logging of eg. success message')
console.log('')

Expand Down Expand Up @@ -129,14 +132,14 @@ check({

const runAllTests = !args.extra && !args.missing

/** @type {string[]|undefined} */
let extras
/** @type {string[]|undefined} */
let result

if (runAllTests || args.unused) {
const extras = extra(pkg, deps, options)
extras = extra(pkg, deps, options)
failed += extras.length
if (extras.length) {
console.error('Fail! Modules in package.json not used in code: ' + extras.join(', '))
} else if (args.verbose) {
console.log('Success! All dependencies in package.json are used in the code')
}
}
if (runAllTests || args.missing) {
const optionsForMissingCheck = runAllTests
Expand All @@ -146,16 +149,32 @@ check({
})
: options

const result = missing(pkg, deps, optionsForMissingCheck)
result = missing(pkg, deps, optionsForMissingCheck)

failed += result.length
}

if (args.json) {
console.log(JSON.stringify({ missing: result, unused: extras }))
// eslint-disable-next-line promise/always-return
process.exit(args.ignore || !failed ? 0 : 1)
}

if (extras) {
if (extras.length) {
console.error('Fail! Modules in package.json not used in code: ' + extras.join(', '))
} else if (args.verbose) {
console.log('Success! All dependencies in package.json are used in the code')
}
}
if (result) {
if (result.length) {
console.error('Fail! Dependencies not listed in package.json: ' + result.join(', '))
} else if (args.verbose) {
console.log('Success! All dependencies used in the code are listed in package.json')
}
}

// eslint-disable-next-line promise/always-return
process.exit(args.ignore || !failed ? 0 : 1)
})
Expand Down

0 comments on commit dbbcd6e

Please sign in to comment.