From d61df97d8d7ce4e6e522fd506a12390dd81f34ef Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Wed, 8 Jun 2016 10:56:49 -0400 Subject: [PATCH] auto-install rollup-watch if requested --- bin/getRollupWatch.js | 43 +++++++++++++++++++++++++++++++++++++++++++ bin/handleError.js | 8 ++++++++ bin/runRollup.js | 37 ++++++++++++++++++++++++++++--------- 3 files changed, 79 insertions(+), 9 deletions(-) create mode 100644 bin/getRollupWatch.js diff --git a/bin/getRollupWatch.js b/bin/getRollupWatch.js new file mode 100644 index 0000000..d830023 --- /dev/null +++ b/bin/getRollupWatch.js @@ -0,0 +1,43 @@ +var chalk = require( 'chalk' ); +var relative = require( 'require-relative' ); + +module.exports = function getRollupWatch () { + try { + watch = relative( 'rollup-watch', process.cwd() ); + return Promise.resolve( watch ); + } catch ( err ) { + if ( err.code === 'MODULE_NOT_FOUND' ) { + return installRollupWatch().then( () => { + return relative( 'rollup-watch', process.cwd() ); + }); + } else { + return Promise.reject( err ); + } + } +}; + +function installRollupWatch () { + console.error( 'rollup --watch depends on the rollup-watch package, which could not be found. You can install it globally with ' + chalk.cyan( 'npm install -g rollup-watch' ) + '. Do this now? ' + chalk.green.bold( '[y/n]' ) ); + + process.stdin.setEncoding( 'utf8' ); + + return new Promise( ( fulfil, reject ) => { + process.stdin.on( 'readable', function () { + var chunk = process.stdin.read(); + if ( !chunk ) return; + + if ( /^y(?:es)?$/i.test( chunk ) ) { + console.error( 'installing rollup-watch...' ); + child = require( 'child_process' ).exec( 'npm install --global rollup-watch', err => { + if ( err ) { + reject( err ); + } else { + fulfil(); + } + }); + } else { + reject( new Error( 'aborted' ) ); + } + }); + }); +} diff --git a/bin/handleError.js b/bin/handleError.js index b7cd224..33ee847 100644 --- a/bin/handleError.js +++ b/bin/handleError.js @@ -27,6 +27,14 @@ var handlers = { DUPLICATE_IMPORT_OPTIONS: function ( err ) { console.error( chalk.red( 'use --input, or pass input path as argument' ) ); + }, + + ROLLUP_WATCH_NOT_INSTALLED: function ( err ) { + console.error( chalk.red( 'failed to find or install rollup-watch' ) ); + }, + + WATCHER_MISSING_INPUT_OR_OUTPUT: function ( err ) { + console.error( chalk.red( 'must specify --input and --output when using rollup --watch' ) ); } }; diff --git a/bin/runRollup.js b/bin/runRollup.js index 46ac35c..91a3df4 100644 --- a/bin/runRollup.js +++ b/bin/runRollup.js @@ -1,8 +1,9 @@ require( 'source-map-support' ).install(); var path = require( 'path' ); -var relative = require( 'require-relative' ); var handleError = require( './handleError' ); +var getRollupWatch = require( './getRollupWatch' ); +var chalk = require( 'chalk' ); var rollup = require( '../' ); // log to stderr to keep `rollup main.js > bundle.js` from breaking @@ -129,16 +130,34 @@ function execute ( options, command ) { try { if ( command.watch ) { - var watch; - - try { - watch = relative( 'rollup-watch', process.cwd() ); - } catch ( err ) { - // TODO offer to install rollup-watch - throw err; + if ( !options.entry || !options.dest ) { + handleError({ code: 'WATCHER_MISSING_INPUT_OR_OUTPUT' }); } - watch( options ); + getRollupWatch() + .then( watch => { + const watcher = watch( rollup, options ); + + watcher.on( 'event', event => { + switch ( event.code ) { + case 'STARTING': + console.error( 'checking rollup-watch version...' ); + break; + + case 'BUILD_START': + console.error( 'bundling...' ); + break; + + case 'BUILD_END': + console.error( 'bundled in ' + event.duration + 'ms. Watching for changes...' ); + break; + + default: + console.error( 'unknown event', event ); + } + }); + }) + .catch( handleError ); } else { bundle( options ).catch( handleError ); }