Skip to content
This repository has been archived by the owner on Jul 27, 2021. It is now read-only.

feat: group module not found errors #31

Merged
merged 1 commit into from
Feb 28, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 50 additions & 11 deletions src/formatters/moduleNotFound.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,72 @@
'use strict';
const concat = require('../utils').concat;

function dependenciesNotFound (count) {
function moduleNotFound (count) {
if (count === 1) {
return 'This dependency was not found in node_modules:';
return 'This module was not found:';
}

return 'These dependencies were not found in node_modules:';
return 'These modules were not found:';
}

function forgetToInstall (count) {
if (count === 1) {
return 'Did you forget to run npm install --save for it?';
function forgetToInstall (missingDependencies) {
const moduleNames = missingDependencies.map(missingDependency => missingDependency.module);

if (missingDependencies.length === 1) {
return `To install it, you can run: npm install --save ${moduleNames.join(' ')}`;
}

return 'Did you forget to run npm install --save for them?';
return `To install them, you can run: npm install --save ${moduleNames.join(' ')}`;
}

function isRelative (module) {
return module.startsWith('./');
}

function groupModules (errors) {
const missingModule = new Map();

errors.forEach((error) => {
if (!missingModule.has(error.module)) {
missingModule.set(error.module, [])
}
missingModule.get(error.module).push(error);
});

return Array.from(missingModule.keys()).map(module => ({
module: module,
relative: isRelative(module),
errors: missingModule.get(module),
}));
}

function formatFileList (files) {
const length = files.length;
if (!length) return '';
return ` in ${files[0]}${files[1] ? `, ${files[1]}` : ''}${length > 2 ? ` and ${length - 2} other${length === 3 ? '' : 's'}` : ''}`;
}

function formatGroup (group) {
const files = group.errors.map(e => e.file).filter(Boolean);
return `* ${group.module}${formatFileList(files)}`;
}

function formatErrors (errors) {
if (errors.length === 0) {
return [];
}

const groups = groupModules(errors);
const missingDependencies = groups.filter(group => !group.relative);

return concat(
dependenciesNotFound(errors.length),
'',
errors.map(e =>`* ${e.module}${e.file ? ` in ${e.file}` : ''}`),
moduleNotFound(errors.length),
'',
forgetToInstall(errors.length)
groups.map(formatGroup),
missingDependencies.length === 0 ? undefined : [
'',
forgetToInstall(missingDependencies)
]
);
}

Expand Down
8 changes: 4 additions & 4 deletions test/integration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ it('integration : module-errors', async() => {
expect(logs).toEqual([
'ERROR Failed to compile with 2 errors',
'',
'These dependencies were not found in node_modules:',
'These modules were not found:',
'',
'* ./non-existing in ./test/fixtures/module-errors/index.js',
'* not-found in ./test/fixtures/module-errors/index.js',
'',
'Did you forget to run npm install --save for them?'
'To install it, you can run: npm install --save not-found'
]);
});

Expand Down Expand Up @@ -123,11 +123,11 @@ it('integration : webpack multi compiler : module-errors', async() => {
expect(logs).toEqual([
'ERROR Failed to compile with 2 errors',
'',
'These dependencies were not found in node_modules:',
'These modules were not found:',
'',
'* ./non-existing in ./test/fixtures/multi-compiler-module-errors/index.js',
'* not-found in ./test/fixtures/multi-compiler-module-errors/index2.js',
'',
'Did you forget to run npm install --save for them?'
'To install it, you can run: npm install --save not-found'
]);
});
53 changes: 49 additions & 4 deletions test/unit/formatters/moduleNotFound.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,69 @@ const expect = require('expect');
it('Formats module-not-found errors', () => {
const error = { type: 'module-not-found', module: 'redux' };
expect(moduleNotFound([error])).toEqual([
'This dependency was not found in node_modules:',
'This module was not found:',
'',
'* redux',
'',
'Did you forget to run npm install --save for it?'
'To install it, you can run: npm install --save redux'
]);
});

it('Groups all module-not-found into one', () => {
const reduxError = { type: 'module-not-found', module: 'redux' };
const reactError = { type: 'module-not-found', module: 'react' };
expect(moduleNotFound([reduxError, reactError])).toEqual([
'These dependencies were not found in node_modules:',
'These modules were not found:',
'',
'* redux',
'* react',
'',
'Did you forget to run npm install --save for them?'
'To install them, you can run: npm install --save redux react'
]);
});

it('Groups same module in module-not-found with 2 files', () => {
const reduxError = { type: 'module-not-found', module: 'redux' };
const reactError1 = { type: 'module-not-found', module: 'react', file: './src/file1.js' };
const reactError2 = { type: 'module-not-found', module: 'react', file: './src/file2.js' };
expect(moduleNotFound([reduxError, reactError1, reactError2])).toEqual([
'These modules were not found:',
'',
'* redux',
'* react in ./src/file1.js, ./src/file2.js',
'',
'To install them, you can run: npm install --save redux react'
]);
});

it('Groups same module in module-not-found with 3 files', () => {
const reduxError = { type: 'module-not-found', module: 'redux' };
const reactError1 = { type: 'module-not-found', module: 'react', file: './src/file1.js' };
const reactError2 = { type: 'module-not-found', module: 'react', file: './src/file2.js' };
const reactError3 = { type: 'module-not-found', module: 'react', file: './src/file3.js' };
expect(moduleNotFound([reduxError, reactError1, reactError2, reactError3])).toEqual([
'These modules were not found:',
'',
'* redux',
'* react in ./src/file1.js, ./src/file2.js and 1 other',
'',
'To install them, you can run: npm install --save redux react'
]);
});

it('Groups same module in module-not-found with 4 files', () => {
const reduxError = { type: 'module-not-found', module: 'redux' };
const reactError1 = { type: 'module-not-found', module: 'react', file: './src/file1.js' };
const reactError2 = { type: 'module-not-found', module: 'react', file: './src/file2.js' };
const reactError3 = { type: 'module-not-found', module: 'react', file: './src/file3.js' };
const reactError4 = { type: 'module-not-found', module: 'react', file: './src/file4.js' };
expect(moduleNotFound([reduxError, reactError1, reactError2, reactError3, reactError4])).toEqual([
'These modules were not found:',
'',
'* redux',
'* react in ./src/file1.js, ./src/file2.js and 2 others',
'',
'To install them, you can run: npm install --save redux react'
]);
});

Expand Down