Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implemented ability to print JSON report to console #532

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
10 changes: 10 additions & 0 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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.
Expand Down Expand Up @@ -77,6 +79,9 @@ const cli = meow(`
type: 'string',
alias: 'i'
},
json: {
type: 'boolean'
},
specials: {
type: 'string'
},
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -142,6 +148,10 @@ Promise.resolve()
return interactiveUpdate(currentState);
}

if (options.json) {
return jsonOutput(currentState);
}

return staticOutput(currentState);
})
.catch(error => {
Expand Down
13 changes: 13 additions & 0 deletions lib/out/json-output
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions lib/state/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const defaultOptions = {
devOnly: false,
forceColor: false,
saveExact: false,
json: false,
specials: '',
debug: false,
emoji: true,
Expand Down