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

feat: add cli flag --json #160

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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