Merge pull request #128 from gribnoysup/feature/prettify-error-logging

More helpful rollup errors
This commit is contained in:
Mateusz Burzyński 2018-05-11 17:13:16 +02:00 committed by GitHub
commit 047d640070
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 14 deletions

View File

@ -1,22 +1,42 @@
#!/usr/bin/env node
import chalk from 'chalk';
import microbundle from '.';
import prog from './prog';
import { stdout, stderr } from './utils';
const run = opts => {
microbundle(opts)
.then( output => {
if (output!=null) process.stdout.write(output + '\n');
.then(output => {
if (output!=null) stdout(output);
if (!opts.watch) process.exit(0);
})
.catch(err => {
process.stderr.write(String(err.error || err) + '\n');
if (typeof(err.code) === 'string') {
process.stderr.write('error ' + err.code);
process.exit(1);
process.exitCode = (typeof err.code === 'number' && err.code) || 1;
const error = err.error || err;
const description = `${error.name ? error.name + ': ' : ''}${error.message || error}`;
const message = error.plugin
? `(${error.plugin} plugin) ${description}`
: description;
stderr(chalk.bold.red(message));
if (error.loc) {
stderr();
stderr(`at ${error.loc.file}:${error.loc.line}:${error.loc.column}`);
}
else {
process.exit(err.code || 1);
if (error.frame) {
stderr();
stderr(chalk.dim(error.frame));
}
else if (err.stack) {
const headlessStack = error.stack.replace(message, '');
stderr(chalk.dim(headlessStack));
}
stderr();
process.exit();
});
};

View File

@ -19,7 +19,7 @@ import prettyBytes from 'pretty-bytes';
import shebangPlugin from 'rollup-plugin-preserve-shebang';
import typescript from 'rollup-plugin-typescript2';
import flow from './lib/flow-plugin';
import { readFile, isDir, isFile } from './utils';
import { readFile, isDir, isFile, stdout, stderr } from './utils';
import camelCase from 'camelcase';
const removeScope = name => name.replace(/^@.*\//, '');
@ -39,9 +39,9 @@ export default async function microbundle(options) {
options.pkg = JSON.parse(await readFile(resolve(cwd, 'package.json'), 'utf8'));
}
catch (err) {
process.stderr.write(chalk.yellow(`${chalk.yellow.inverse('WARN')} no package.json found. Assuming a pkg.name of "${basename(options.cwd)}".`) + '\n');
stderr(chalk.yellow(`${chalk.yellow.inverse('WARN')} no package.json found. Assuming a pkg.name of "${basename(options.cwd)}".`));
let msg = String(err.message || err);
if (!msg.match(/ENOENT/)) console.warn(` ${chalk.red.dim(msg)}`);
if (!msg.match(/ENOENT/)) stderr(` ${chalk.red.dim(msg)}`);
options.pkg = {};
hasPackageJson = false;
}
@ -49,7 +49,7 @@ export default async function microbundle(options) {
if (!options.pkg.name) {
options.pkg.name = basename(options.cwd);
if (hasPackageJson) {
process.stderr.write(chalk.yellow(`${chalk.yellow.inverse('WARN')} missing package.json "name" field. Assuming "${options.pkg.name}".`) + '\n');
stderr(chalk.yellow(`${chalk.yellow.inverse('WARN')} missing package.json "name" field. Assuming "${options.pkg.name}".`));
}
}
@ -104,7 +104,7 @@ export default async function microbundle(options) {
if (options.watch) {
const onBuild = options.onBuild;
return new Promise((resolve, reject) => {
process.stdout.write(chalk.blue(`Watching source, compiling to ${relative(cwd, dirname(options.output))}:\n`));
stdout(chalk.blue(`Watching source, compiling to ${relative(cwd, dirname(options.output))}:`));
steps.map(options => {
watch(Object.assign({
output: options.outputOptions,
@ -115,7 +115,7 @@ export default async function microbundle(options) {
}
if (e.code === 'END') {
getSizeInfo(options._code, options.outputOptions.file).then(text => {
process.stdout.write(`Wrote ${text.trim()}\n`);
stdout(`Wrote ${text.trim()}`);
});
if (typeof onBuild === 'function') {
onBuild(e);

View File

@ -6,3 +6,5 @@ export const readFile = promisify(fs.readFile);
export const stat = promisify(fs.stat);
export const isDir = name => stat(name).then( stats => stats.isDirectory() ).catch( () => false );
export const isFile = name => stat(name).then( stats => stats.isFile() ).catch( () => false );
export const stdout = console.log.bind(console); // eslint-disable-line no-console
export const stderr = console.error.bind(console);