Skip to content

Commit

Permalink
Add paths for missing dependencies (#433)
Browse files Browse the repository at this point in the history
* Add paths for missing dependencies

Closes #428

Co-authored-by: Bernie @twk-b

* Add test for missing dependencies
  • Loading branch information
rumpl committed Oct 31, 2019
1 parent f7240a1 commit 9743d20
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
11 changes: 8 additions & 3 deletions src/cli.js
Expand Up @@ -47,15 +47,20 @@ function prettify(caption, deps) {
return list.length ? [caption].concat(list) : [];
}

function print(result, log, json) {
function mapMissing(missing, rootDir) {
return lodash.map(missing, (foundInFiles, key) =>
`${key}: ${lodash.replace(lodash.first(foundInFiles), rootDir, '.')}`);
}

function print(result, log, json, rootDir) {
if (json) {
log(JSON.stringify(result, (key, value) => (lodash.isError(value) ? value.stack : value)));
} else if (noIssue(result)) {
log('No depcheck issue');
} else {
const deps = prettify('Unused dependencies', result.dependencies);
const devDeps = prettify('Unused devDependencies', result.devDependencies);
const missing = prettify('Missing dependencies', Object.keys(result.missing));
const missing = prettify('Missing dependencies', mapMissing(result.missing, rootDir));
const content = deps.concat(devDeps, missing).join('\n');
log(content);
}
Expand Down Expand Up @@ -119,7 +124,7 @@ export default function cli(args, log, error, exit) {
specials: getSpecials(opt.argv.specials),
skipMissing: opt.argv.skipMissing,
}))
.then((result) => print(result, log, opt.argv.json))
.then((result) => print(result, log, opt.argv.json, rootDir))
.then((result) => exit((opt.argv.json || noIssue(result)) ? 0 : -1))
.catch((errorMessage) => {
error(errorMessage);
Expand Down
15 changes: 13 additions & 2 deletions test/cli.js
Expand Up @@ -131,7 +131,7 @@ describe('depcheck command line', () => {
exitCode.should.equal(0);
}));

it('should output unused dependencies when happen', () =>
it('should output unused dependencies', () =>
testCli(makeArgv('bad', {}))
.then(({ logs, error, exitCode }) => {
logs.should.have.length(2);
Expand All @@ -142,7 +142,7 @@ describe('depcheck command line', () => {
exitCode.should.equal(-1);
}));

it('should output unused devDependencies when happen', () =>
it('should output unused devDependencies', () =>
testCli(makeArgv('dev', {}))
.then(({ logs, error, exitCode }) => {
logs.should.have.length(2);
Expand All @@ -153,6 +153,17 @@ describe('depcheck command line', () => {
exitCode.should.equal(-1);
}));

it('should output missing dependencies', () =>
testCli(makeArgv('missing', {}))
.then(({ logs, error, exitCode }) => {
logs.should.have.length(2);
logs[0].should.equal('Missing dependencies');
logs[1].should.containEql('missing-dep');

error.should.be.empty();
exitCode.should.equal(-1);
}));

it('should recognize JSX file even only pass jsx parser and require detector', () =>
testCli(makeArgv('jsx', {
argv: ['--parsers="*.jsx:jsx"', '--dectors=requireCallExpression'],
Expand Down

0 comments on commit 9743d20

Please sign in to comment.