Browse Source

more consistent error logging in CLI

gh-1187
Rich Harris 8 years ago
parent
commit
35785fff12
  1. 65
      bin/src/handleError.js
  2. 47
      bin/src/logging.js
  3. 91
      bin/src/runRollup.js

65
bin/src/handleError.js

@ -1,65 +0,0 @@
import chalk from 'chalk';
function stderr ( msg ) {
console.error( msg ); // eslint-disable-line no-console
}
const handlers = {
MISSING_CONFIG: () => {
stderr( chalk.red( 'Config file must export an options object. See https://github.com/rollup/rollup/wiki/Command-Line-Interface#using-a-config-file' ) );
},
MISSING_EXTERNAL_CONFIG: err => {
stderr( chalk.red( `Could not resolve config file ${err.config}` ) );
},
MISSING_INPUT_OPTION: () => {
stderr( chalk.red( 'You must specify an --input (-i) option' ) );
},
MISSING_OUTPUT_OPTION: () => {
stderr( chalk.red( 'You must specify an --output (-o) option when creating a file with a sourcemap' ) );
},
MISSING_NAME: () => {
stderr( chalk.red( 'You must supply a name for UMD exports (e.g. `--name myModule`)' ) );
},
PARSE_ERROR: err => {
stderr( chalk.red( `Error parsing ${err.file}: ${err.message}` ) );
},
ONE_AT_A_TIME: () => {
stderr( chalk.red( 'rollup can only bundle one file at a time' ) );
},
DUPLICATE_IMPORT_OPTIONS: () => {
stderr( chalk.red( 'use --input, or pass input path as argument' ) );
},
ROLLUP_WATCH_NOT_INSTALLED: () => {
stderr( chalk.red( 'rollup --watch depends on the rollup-watch package, which could not be found. Install it with ' ) + chalk.cyan( 'npm install -D rollup-watch' ) );
},
WATCHER_MISSING_INPUT_OR_OUTPUT: () => {
stderr( chalk.red( 'must specify --input and --output when using rollup --watch' ) );
}
};
export default function handleError ( err, recover ) {
const handler = handlers[ err && err.code ];
if ( handler ) {
handler( err );
} else {
stderr( chalk.red( err.message || err ) );
if ( err.stack ) {
stderr( chalk.grey( err.stack ) );
}
}
stderr( `Type ${chalk.cyan( 'rollup --help' )} for help, or visit https://github.com/rollup/rollup/wiki` );
if ( !recover ) process.exit( 1 );
}

47
bin/src/logging.js

@ -0,0 +1,47 @@
import chalk from 'chalk';
import relativeId from '../../src/utils/relativeId.js';
if ( !process.stderr.isTTY ) chalk.enabled = false;
const warnSymbol = process.stderr.isTTY ? `⚠️ ` : `Warning: `;
const errorSymbol = process.stderr.isTTY ? `🚨 ` : `Error: `;
// log to stderr to keep `rollup main.js > bundle.js` from breaking
export const stderr = console.error.bind( console ); // eslint-disable-line no-console
export function handleWarning ( warning ) {
stderr( `${warnSymbol}${chalk.bold( warning.message )}` );
if ( warning.url ) {
stderr( chalk.cyan( warning.url ) );
}
if ( warning.loc ) {
stderr( `${relativeId( warning.loc.file )} (${warning.loc.line}:${warning.loc.column})` );
}
if ( warning.frame ) {
stderr( chalk.dim( warning.frame ) );
}
stderr( '' );
}
export function handleError ( err, recover ) {
stderr( `${errorSymbol}${chalk.bold( err.message )}` );
if ( err.url ) {
stderr( chalk.cyan( err.url ) );
}
if ( err.loc ) {
stderr( `${relativeId( err.loc.file )} (${err.loc.line}:${err.loc.column})` );
}
if ( err.frame ) {
stderr( chalk.dim( err.frame ) );
}
stderr( '' );
if ( !recover ) process.exit( 1 );
}

91
bin/src/runRollup.js

@ -2,27 +2,26 @@ import { realpathSync } from 'fs';
import * as rollup from 'rollup'; import * as rollup from 'rollup';
import relative from 'require-relative'; import relative from 'require-relative';
import chalk from 'chalk'; import chalk from 'chalk';
import handleError from './handleError'; import { handleWarning, handleError, stderr } from './logging.js';
import relativeId from '../../src/utils/relativeId.js';
import SOURCEMAPPING_URL from './sourceMappingUrl.js'; import SOURCEMAPPING_URL from './sourceMappingUrl.js';
import { install as installSourcemapSupport } from 'source-map-support'; import { install as installSourcemapSupport } from 'source-map-support';
installSourcemapSupport(); installSourcemapSupport();
if ( !process.stderr.isTTY ) chalk.enabled = false;
const warnSymbol = process.stderr.isTTY ? `⚠️ ` : `Warning: `;
// stderr to stderr to keep `rollup main.js > bundle.js` from breaking
const stderr = console.error.bind( console ); // eslint-disable-line no-console
export default function runRollup ( command ) { export default function runRollup ( command ) {
if ( command._.length > 1 ) { if ( command._.length > 1 ) {
handleError({ code: 'ONE_AT_A_TIME' }); handleError({
code: 'ONE_AT_A_TIME',
message: 'rollup can only bundle one file at a time'
});
} }
if ( command._.length === 1 ) { if ( command._.length === 1 ) {
if ( command.input ) { if ( command.input ) {
handleError({ code: 'DUPLICATE_IMPORT_OPTIONS' }); handleError({
code: 'DUPLICATE_IMPORT_OPTIONS',
message: 'use --input, or pass input path as argument'
});
} }
command.input = command._[0]; command.input = command._[0];
@ -51,7 +50,10 @@ export default function runRollup ( command ) {
config = relative.resolve( pkgName, process.cwd() ); config = relative.resolve( pkgName, process.cwd() );
} catch ( err ) { } catch ( err ) {
if ( err.code === 'MODULE_NOT_FOUND' ) { if ( err.code === 'MODULE_NOT_FOUND' ) {
handleError({ code: 'MISSING_EXTERNAL_CONFIG', config }); handleError({
code: 'MISSING_EXTERNAL_CONFIG',
message: `Could not resolve config file ${config}`
});
} }
throw err; throw err;
@ -64,11 +66,12 @@ export default function runRollup ( command ) {
rollup.rollup({ rollup.rollup({
entry: config, entry: config,
onwarn: message => { onwarn: warning => {
if ( message.code === 'UNRESOLVED_IMPORT' ) return; if ( warning.code === 'UNRESOLVED_IMPORT' ) return;
stderr( message.toString() ); handleWarning( warning );
} }
}).then( bundle => { })
.then( bundle => {
const { code } = bundle.generate({ const { code } = bundle.generate({
format: 'cjs' format: 'cjs'
}); });
@ -83,18 +86,18 @@ export default function runRollup ( command ) {
} }
}; };
try {
const options = require( config ); const options = require( config );
if ( Object.keys( options ).length === 0 ) { if ( Object.keys( options ).length === 0 ) {
handleError({ code: 'MISSING_CONFIG' }); handleError({
code: 'MISSING_CONFIG',
message: 'Config file must export an options object',
url: 'https://github.com/rollup/rollup/wiki/Command-Line-Interface#using-a-config-file'
});
} }
execute( options, command ); execute( options, command );
require.extensions[ '.js' ] = defaultLoader; require.extensions[ '.js' ] = defaultLoader;
} catch ( err ) {
handleError( err );
}
}) })
.catch( stderr ); .catch( handleError );
} else { } else {
execute( {}, command ); execute( {}, command );
} }
@ -157,21 +160,7 @@ function execute ( options, command ) {
if ( seen.has( str ) ) return; if ( seen.has( str ) ) return;
seen.add( str ); seen.add( str );
stderr( `${warnSymbol}${chalk.bold( warning.message )}` ); handleWarning( warning );
if ( warning.url ) {
stderr( chalk.cyan( warning.url ) );
}
if ( warning.loc ) {
stderr( `${relativeId( warning.loc.file )} (${warning.loc.line}:${warning.loc.column})` );
}
if ( warning.frame ) {
stderr( chalk.dim( warning.frame ) );
}
stderr( '' );
}; };
} }
@ -184,10 +173,12 @@ function execute ( options, command ) {
} }
}); });
try {
if ( command.watch ) { if ( command.watch ) {
if ( !options.entry || ( !options.dest && !options.targets ) ) { if ( !options.entry || ( !options.dest && !options.targets ) ) {
handleError({ code: 'WATCHER_MISSING_INPUT_OR_OUTPUT' }); handleError({
code: 'WATCHER_MISSING_INPUT_OR_OUTPUT',
message: 'must specify --input and --output when using rollup --watch'
});
} }
try { try {
@ -218,7 +209,10 @@ function execute ( options, command ) {
}); });
} catch ( err ) { } catch ( err ) {
if ( err.code === 'MODULE_NOT_FOUND' ) { if ( err.code === 'MODULE_NOT_FOUND' ) {
err.code = 'ROLLUP_WATCH_NOT_INSTALLED'; handleError({
code: 'ROLLUP_WATCH_NOT_INSTALLED',
message: 'rollup --watch depends on the rollup-watch package, which could not be found. Install it with npm install -D rollup-watch'
});
} }
handleError( err ); handleError( err );
@ -226,9 +220,6 @@ function execute ( options, command ) {
} else { } else {
bundle( options ).catch( handleError ); bundle( options ).catch( handleError );
} }
} catch ( err ) {
handleError( err );
}
} }
function clone ( object ) { function clone ( object ) {
@ -244,10 +235,14 @@ function assign ( target, source ) {
function bundle ( options ) { function bundle ( options ) {
if ( !options.entry ) { if ( !options.entry ) {
handleError({ code: 'MISSING_INPUT_OPTION' }); handleError({
code: 'MISSING_INPUT_OPTION',
message: 'You must specify an --input (-i) option'
});
} }
return rollup.rollup( options ).then( bundle => { return rollup.rollup( options )
.then( bundle => {
if ( options.dest ) { if ( options.dest ) {
return bundle.write( options ); return bundle.write( options );
} }
@ -263,7 +258,10 @@ function bundle ( options ) {
} }
if ( options.sourceMap && options.sourceMap !== 'inline' ) { if ( options.sourceMap && options.sourceMap !== 'inline' ) {
handleError({ code: 'MISSING_OUTPUT_OPTION' }); handleError({
code: 'MISSING_OUTPUT_OPTION',
message: 'You must specify an --output (-o) option when creating a file with a sourcemap'
});
} }
let { code, map } = bundle.generate( options ); let { code, map } = bundle.generate( options );
@ -273,5 +271,6 @@ function bundle ( options ) {
} }
process.stdout.write( code ); process.stdout.write( code );
}); })
.catch( handleError );
} }

Loading…
Cancel
Save