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 save JSON report to file #400

Closed
wants to merge 1 commit into from
Closed
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 @@ -65,6 +65,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 @@ -150,6 +151,12 @@ Install packages using `--save-exact`, meaning exact versions will be saved in p

Applies to both `dependencies` and `devDependencies`.

#### `--json`

Save Json report to file.

`$ 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
8 changes: 7 additions & 1 deletion 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 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 @@ -73,6 +75,7 @@ const cli = meow({
],
string: [
'ignore',
'json',
'specials'
]
});
Expand All @@ -86,6 +89,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 @@ -118,7 +122,9 @@ Promise.resolve()
if (options.update) {
return interactiveUpdate(currentState);
}

if (options.json) {
return jsonOutput(currentState, options.json);
}
return staticOutput(currentState);
})
.catch(err => {
Expand Down
14 changes: 14 additions & 0 deletions lib/out/json-output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

const chalk = require('chalk');
const _ = require('lodash');
const fs = require("fs");

function outputJSON(currentState, filename) {
const packages = currentState.get('packages');
fs.writeFileSync(filename, JSON.stringify(packages, null, 4));
console.log(`Report saved to ${filename}`)
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: '',
specials: '',
debug: false,
emoji: true,
Expand Down