diff --git a/README.md b/README.md index 3dfc813c..9770a660 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,7 @@ Options -d, --dev-only Look at devDependencies only (skip dependencies). -i, --ignore Ignore dependencies based on succeeding glob. -E, --save-exact Save exact version (x.y.z) instead of caret (^x.y.z) in package.json. + --json Save Json report to file --specials List of depcheck specials to include in check for unused dependencies. --no-color Force or disable color output. --no-emoji Remove emoji support. No emoji in default in CI environments. @@ -149,6 +150,12 @@ Install packages using `--save-exact`, meaning exact versions will be saved in p Applies to both `dependencies` and `devDependencies`. +#### `--json` + +Print out the Json report. + +`$ npm-check --json > /home/user/report.json` will save JSON report to `/home/user/report.json`. + #### `--specials` Check special (e.g. config) files when looking for unused dependencies. diff --git a/lib/cli.js b/lib/cli.js index 908ed75d..fbea5737 100755 --- a/lib/cli.js +++ b/lib/cli.js @@ -8,6 +8,7 @@ const createCallsiteRecord = require('callsite-record'); const pkg = require('../package.json'); const npmCheck = require('./index'); const staticOutput = require('./out/static-output'); +const jsonOutput = require('./out/json-output'); const interactiveUpdate = require('./out/interactive-update'); const updateAll = require('./out/update-all'); const debug = require('./state/debug'); @@ -33,6 +34,7 @@ const cli = meow(` -d, --dev-only Look at devDependencies only (skip dependencies). -i, --ignore Ignore dependencies based on succeeding glob. -E, --save-exact Save exact version (x.y.z) instead of caret (^x.y.z) in package.json. + --json Print Json report. --specials List of depcheck specials to include in check for unused dependencies. --no-color Force or disable color output. --no-emoji Remove emoji support. No emoji in default in CI environments. @@ -77,6 +79,9 @@ const cli = meow(` type: 'string', alias: 'i' }, + json: { + type: 'boolean' + }, specials: { type: 'string' }, @@ -108,6 +113,7 @@ const options = { ignoreDev: cli.flags.production, devOnly: cli.flags.devOnly, saveExact: cli.flags.saveExact, + json: cli.flags.json, specials: cli.flags.specials, emoji: cli.flags.emoji, installer: process.env.NPM_CHECK_INSTALLER || 'auto', @@ -142,6 +148,10 @@ Promise.resolve() return interactiveUpdate(currentState); } + if (options.json) { + return jsonOutput(currentState); + } + return staticOutput(currentState); }) .catch(error => { diff --git a/lib/out/json-output b/lib/out/json-output new file mode 100644 index 00000000..a8005e6c --- /dev/null +++ b/lib/out/json-output @@ -0,0 +1,13 @@ +'use strict'; + +const chalk = require('chalk'); +const _ = require('lodash'); +const fs = require("fs"); + +function outputJSON(currentState, filename) { + const packages = currentState.get('packages'); + console.log(JSON.stringify(packages, null, 4)); + process.exitCode = 0; +} + +module.exports = outputJSON; \ No newline at end of file diff --git a/lib/state/state.js b/lib/state/state.js index d4bb3d72..8100a02d 100644 --- a/lib/state/state.js +++ b/lib/state/state.js @@ -14,6 +14,7 @@ const defaultOptions = { devOnly: false, forceColor: false, saveExact: false, + json: false, specials: '', debug: false, emoji: true,