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

Add paths for missing dependencies #433

Merged
merged 2 commits into from Oct 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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