Browse Source

CLI

contingency-plan
Rich-Harris 10 years ago
parent
commit
8f12b7da36
  1. 45
      bin/handleError.js
  2. 27
      bin/help.md
  3. 29
      bin/index.js
  4. 59
      bin/runRollup.js
  5. 13
      bin/showHelp.js

45
bin/handleError.js

@ -0,0 +1,45 @@
var chalk = require( 'chalk' );
var handlers = {
MISSING_INPUT_OPTION: function () {
console.error( chalk.red( 'You must specify an --input (-i) option' ) );
},
MISSING_OUTPUT_OPTION: function () {
console.error( chalk.red( 'You must specify an --output (-o) option when creating a file with a sourcemap' ) );
},
MISSING_NAME: function ( err ) {
console.error( chalk.red( 'You must supply a name for UMD exports (e.g. `--name myModule`)' ) );
},
PARSE_ERROR: function ( err ) {
console.error( chalk.red( 'Error parsing ' + err.file + ': ' + err.message ) );
},
ONE_AT_A_TIME: function ( err ) {
console.error( chalk.red( 'rollup can only bundle one file at a time' ) );
},
DUPLICATE_IMPORT_OPTIONS: function ( err ) {
console.error( chalk.red( 'use --input, or pass input path as argument' ) );
}
};
module.exports = function handleError ( err ) {
var handler;
if ( handler = handlers[ err && err.code ] ) {
handler( err );
} else {
console.error( chalk.red( err.message || err ) );
if ( err.stack ) {
console.error( chalk.grey( err.stack ) );
}
}
console.error( 'Type ' + chalk.cyan( 'rollup --help' ) + ' for help, or visit https://github.com/rollup/rollup/wiki' );
process.exit( 1 );
};

27
bin/help.md

@ -0,0 +1,27 @@
rollup version <%= version %>
=====================================
Usage: rollup [options] <entry file>
Basic options:
-v, --version Show version number
-h, --help Show this help message
-i, --input Input (alternative to <entry file>)
-o, --output <output> Output (if absent, prints to stdout)
-f, --format [umd] Type of output (amd, cjs, es6, iife, umd)
-n, --name Name for UMD export
-u, --id ID for AMD module (default is anonymous)
-m, --sourcemap Generate sourcemap (`-m inline` for inline map)
Example:
rollup --format=cjs --output=bundle.js -- src/main.js
Notes:
* When piping to stdout, only inline sourcemaps are permitted
For more information visit https://github.com/rollup/rollup/wiki

29
bin/index.js

@ -0,0 +1,29 @@
#!/usr/bin/env node
var minimist = require( 'minimist' ),
command;
command = minimist( process.argv.slice( 2 ), {
alias: {
i: 'input',
o: 'output',
v: 'version',
h: 'help',
f: 'format',
m: 'sourcemap',
n: 'name',
u: 'id'
}
});
if ( command.help || ( process.argv.length <= 2 && process.stdin.isTTY ) ) {
require( './showHelp' )();
}
else if ( command.version ) {
console.log( 'rollup version ' + require( '../package.json' ).version );
}
else {
require( './runRollup' )( command );
}

59
bin/runRollup.js

@ -0,0 +1,59 @@
require( 'source-map-support' ).install();
var path = require( 'path' );
var sander = require( 'sander' );
var Promise = sander.Promise;
var handleError = require( './handleError' );
var rollup = require( '../' );
module.exports = function ( options ) {
if ( options._.length > 1 ) {
handleError({ code: 'ONE_AT_A_TIME' });
}
if ( options._.length === 1 ) {
if ( options.input ) {
handleError({ code: 'DUPLICATE_IMPORT_OPTIONS' });
}
options.input = options._[0];
}
try {
bundle( options ).catch( handleError );
} catch ( err ) {
handleError( err );
}
};
function bundle ( options, method ) {
var bundleOptions, file;
if ( !options.input ) {
handleError({ code: 'MISSING_INPUT_OPTION' });
}
return rollup.rollup( options.input ).then( function ( bundle ) {
var generateOptions = {
format: options.format
};
if ( options.output ) {
return bundle.write( options.output, generateOptions );
}
if ( options.sourcemap && options.sourcemap !== 'inline' ) {
handleError({ code: 'MISSING_OUTPUT_OPTION' });
}
var result = bundle.generate( generateOptions );
var code = result.code;
if ( options.sourcemap === 'inline' ) {
code += '\n//# sourceMappingURL=' + map.toUrl();
}
process.stdout.write( code );
});
}

13
bin/showHelp.js

@ -0,0 +1,13 @@
var fs = require( 'fs' );
var path = require( 'path' );
module.exports = function () {
fs.readFile( path.join( __dirname, 'help.md' ), function ( err, result ) {
var help;
if ( err ) throw err;
help = result.toString().replace( '<%= version %>', require( '../package.json' ).version );
console.log( '\n' + help + '\n' );
});
};
Loading…
Cancel
Save