From 8f78f5a15a8a6d41ca4253a4404481cb45c18f85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Merzough=20M=C3=BCnker?= Date: Sun, 11 Apr 2021 17:40:51 +0200 Subject: [PATCH 1/3] feat: add cli flag --json MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If the --json flag is used the missing and unused dependencies are printed as json object Signed-off-by: Merzough Münker --- cli.js | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/cli.js b/cli.js index bcf8dfc..55aec13 100755 --- a/cli.js +++ b/cli.js @@ -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', } }) @@ -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('') @@ -129,33 +132,43 @@ check({ const runAllTests = !args.extra && !args.missing + /** @type {string[]} */ + let extras = [] + /** @type {string[]} */ + 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 ? Object.assign({}, options, { - excludeDev: false, - excludePeer: false - }) + excludeDev: false, + excludePeer: false + }) : 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 })) + } else { + 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.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) }) From 4a3b826281f66af86d7f9a3bc0dfc571c278744f Mon Sep 17 00:00:00 2001 From: Pelle Wessman Date: Wed, 30 Jun 2021 11:09:30 +0200 Subject: [PATCH 2/3] Only log the options selected, including in JSON --- cli.js | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/cli.js b/cli.js index 55aec13..39b7b1a 100755 --- a/cli.js +++ b/cli.js @@ -132,10 +132,10 @@ check({ const runAllTests = !args.extra && !args.missing - /** @type {string[]} */ - let extras = [] - /** @type {string[]} */ - let result = [] + /** @type {string[]|undefined} */ + let extras + /** @type {string[]|undefined} */ + let result if (runAllTests || args.unused) { extras = extra(pkg, deps, options) @@ -144,9 +144,9 @@ check({ if (runAllTests || args.missing) { const optionsForMissingCheck = runAllTests ? Object.assign({}, options, { - excludeDev: false, - excludePeer: false - }) + excludeDev: false, + excludePeer: false + }) : options result = missing(pkg, deps, optionsForMissingCheck) @@ -156,12 +156,18 @@ check({ if (args.json) { console.log(JSON.stringify({ missing: result, unused: extras })) - } else { + // 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) { From 627d79bf62f85d0f5d76c3318ca9944d4c7ad859 Mon Sep 17 00:00:00 2001 From: Pelle Wessman Date: Wed, 30 Jun 2021 11:09:48 +0200 Subject: [PATCH 3/3] Add .npmrc that disables lock file --- .npmrc | 1 + 1 file changed, 1 insertion(+) create mode 100644 .npmrc diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..43c97e7 --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +package-lock=false