npm and error messages

This commit is contained in:
2018-10-27 03:51:47 -05:00
parent 692ab70565
commit 025a403027
29601 changed files with 2759363 additions and 14 deletions

View File

@@ -0,0 +1,43 @@
'use strict';
const concat = require('../utils').concat;
const formatTitle = require('../utils/colors').formatTitle;
function displayError(severity, error) {
const baseError = formatTitle(severity, severity);
return concat(
`${baseError} ${removeLoaders(error.file)}`,
'',
error.message,
(error.origin ? error.origin : undefined),
'',
error.infos
);
}
function removeLoaders(file) {
if (!file) {
return "";
}
const split = file.split('!');
const filePath = split[split.length - 1];
return `in ${filePath}`;
}
function isDefaultError(error) {
return !error.type;
}
/**
* Format errors without a type
*/
function format(errors, type) {
return errors
.filter(isDefaultError)
.reduce((accum, error) => (
accum.concat(displayError(type, error))
), []);
}
module.exports = format;

View File

@@ -0,0 +1,31 @@
'use strict';
const concat = require('../utils').concat;
const chalk = require('chalk');
const infos = [
'You may use special comments to disable some warnings.',
'Use ' + chalk.yellow('// eslint-disable-next-line') + ' to ignore the next line.',
'Use ' + chalk.yellow('/* eslint-disable */') + ' to ignore all warnings in a file.'
];
function displayError(error) {
return [error.message, '']
}
function format(errors, type) {
const lintErrors = errors.filter(e => e.type === 'lint-error');
if (lintErrors.length > 0) {
const flatten = (accum, curr) => accum.concat(curr);
return concat(
lintErrors
.map(error => displayError(error))
.reduce(flatten, []),
infos
)
}
return [];
}
module.exports = format;

View File

@@ -0,0 +1,92 @@
'use strict';
const concat = require('../utils').concat;
function isRelative (module) {
return module.startsWith('./') || module.startsWith('../');
}
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 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 `To install them, you can run: npm install --save ${moduleNames.join(' ')}`;
}
function dependenciesNotFound (dependencies) {
if (dependencies.length === 0) return;
return concat(
dependencies.length === 1 ? 'This dependency was not found:' : 'These dependencies were not found:',
'',
dependencies.map(formatGroup),
'',
forgetToInstall(dependencies)
);
}
function relativeModulesNotFound (modules) {
if (modules.length === 0) return;
return concat(
modules.length === 1 ? 'This relative module was not found:' : 'These relative modules were not found:',
'',
modules.map(formatGroup)
);
}
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 formatErrors (errors) {
if (errors.length === 0) {
return [];
}
const groups = groupModules(errors);
const dependencies = groups.filter(group => !group.relative);
const relativeModules = groups.filter(group => group.relative);
return concat(
dependenciesNotFound(dependencies),
dependencies.length && relativeModules.length ? ['', ''] : null,
relativeModulesNotFound(relativeModules)
);
}
function format (errors) {
return formatErrors(errors.filter((e) => (
e.type === 'module-not-found'
)));
}
module.exports = format;