Skip to content

Commit

Permalink
Merge pull request #679 from webdevium/main
Browse files Browse the repository at this point in the history
cli: --oneline option
  • Loading branch information
rumpl committed Nov 10, 2021
2 parents e770d0f + 80c9052 commit a687b10
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ All of the arguments are optional:

`--json`: Output results in JSON. When not specified, depcheck outputs in human friendly format.

`--oneline`: Output results as space separated string. Useful for copy/paste.

`--ignores`: A comma separated array containing package names to ignore. It can be glob expressions. Example, `--ignores="eslint,babel-*"`.

`--ignore-dirs`: DEPRECATED, use ignore-patterns instead. A comma separated array containing directory names to ignore. Example, `--ignore-dirs=dist,coverage`.
Expand Down
31 changes: 23 additions & 8 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,27 @@ function noIssue(result) {
);
}

function prettify(caption, deps) {
function prettify(caption, deps, oneline) {
if (oneline) {
return deps.length ? [caption, deps.join(' ')] : [];
}
const list = deps.map((dep) => `* ${dep}`);
return list.length ? [caption].concat(list) : [];
}

function mapMissing(missing, rootDir) {
function mapMissing(missing, rootDir, oneline) {
if (oneline) {
return lodash.keys(missing);
}
return lodash.map(
missing,
(foundInFiles, key) =>
`${key}: ${lodash.replace(lodash.first(foundInFiles), rootDir, '.')}`,
);
}

function print(result, log, json, rootDir) {
if (json) {
function print(result, log, opt, rootDir) {
if (opt.json) {
log(
JSON.stringify(result, (key, value) =>
lodash.isError(value) ? value.stack : value,
Expand All @@ -83,11 +89,20 @@ function print(result, log, json, rootDir) {
} else if (noIssue(result)) {
log('No depcheck issue');
} else {
const deps = prettify('Unused dependencies', result.dependencies);
const devDeps = prettify('Unused devDependencies', result.devDependencies);
const deps = prettify(
'Unused dependencies',
result.dependencies,
opt.oneline,
);
const devDeps = prettify(
'Unused devDependencies',
result.devDependencies,
opt.oneline,
);
const missing = prettify(
'Missing dependencies',
mapMissing(result.missing, rootDir),
mapMissing(result.missing, rootDir, opt.oneline),
opt.oneline,
);
const content = deps.concat(devDeps, missing).join('\n');
log(content);
Expand Down Expand Up @@ -117,7 +132,7 @@ export default async function cli(args, log, error, exit) {
specials: getSpecials(opt.specials),
skipMissing: opt.skipMissing,
});
print(depcheckResult, log, opt.json, rootDir);
print(depcheckResult, log, opt, rootDir);
exit(noIssue(depcheckResult) ? 0 : -1);
} catch (err) {
error(err);
Expand Down
1 change: 1 addition & 0 deletions src/utils/configuration-reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export function getCliArgs(args, version) {
.describe('ignore-bin-package', 'Ignore package with bin entry')
.describe('skip-missing', 'Skip calculation of missing dependencies')
.describe('json', 'Output results to JSON')
.describe('oneline', 'Output results as space separated string')
.describe('ignores', 'Comma separated package list to ignore')
.describe(
'ignore-dirs',
Expand Down

0 comments on commit a687b10

Please sign in to comment.