From 33ed201b7d46d3ada21e5135c2fccf360ba338e2 Mon Sep 17 00:00:00 2001 From: Jasper De Moor Date: Tue, 19 Dec 2017 09:04:48 +0100 Subject: [PATCH] Run cli.js through babel (#246) * run prettier * fix * use same src folder as everything else --- packages/core/parcel/bin/cli.js | 109 ++------------------------------ packages/core/parcel/src/cli.js | 104 ++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+), 104 deletions(-) mode change 100755 => 100644 packages/core/parcel/bin/cli.js create mode 100755 packages/core/parcel/src/cli.js diff --git a/packages/core/parcel/bin/cli.js b/packages/core/parcel/bin/cli.js old mode 100755 new mode 100644 index ea71832a42b..523bcad4f02 --- a/packages/core/parcel/bin/cli.js +++ b/packages/core/parcel/bin/cli.js @@ -1,104 +1,5 @@ -#!/usr/bin/env node - -require('v8-compile-cache'); -const chalk = require('chalk'); -const program = require('commander'); -const version = require('../package.json').version; - -program.version(version); - -program - .command('serve [input]') - .description('starts a development server') - .option('-p, --port ', 'set the port to serve on. defaults to 1234') - .option('-o, --open', 'automatically open in default browser') - .option( - '-d, --out-dir ', - 'set the output directory. defaults to "dist"' - ) - .option( - '--public-url ', - 'set the public URL to serve on. defaults to the same as the --out-dir option' - ) - .option('--no-hmr', 'disable hot module replacement') - .option('--no-cache', 'disable the filesystem cache') - .option('-V, --version', 'output the version number') - .action(bundle); - -program - .command('watch [input]') - .description('starts the bundler in watch mode') - .option( - '-d, --out-dir ', - 'set the output directory. defaults to "dist"' - ) - .option( - '--public-url ', - 'set the public URL to serve on. defaults to the same as the --out-dir option' - ) - .option('--no-hmr', 'disable hot module replacement') - .option('--no-cache', 'disable the filesystem cache') - .action(bundle); - -program - .command('build [input]') - .description('bundles for production') - .option( - '-d, --out-dir ', - 'set the output directory. defaults to "dist"' - ) - .option( - '--public-url ', - 'set the public URL to serve on. defaults to the same as the --out-dir option' - ) - .option('--no-minify', 'disable minification') - .option('--no-cache', 'disable the filesystem cache') - .action(bundle); - -program - .command('help [command]') - .description('display help information for a command') - .action(function(command) { - let cmd = program.commands.find(c => c.name() === command) || program; - cmd.help(); - }); - -program.on('--help', function() { - console.log(''); - console.log( - ' Run `' + - chalk.bold('parcel help ') + - '` for more information on specific commands' - ); - console.log(''); -}); - -// Make serve the default command -var args = process.argv; -if (!args[2] || !program.commands.some(c => c.name() === args[2])) { - args.splice(2, 0, 'serve'); -} - -program.parse(args); - -async function bundle(main, command) { - // Require bundler here so the help command is fast - const Bundler = require('../'); - - if (command.name() === 'build') { - process.env.NODE_ENV = 'production'; - } else { - process.env.NODE_ENV = process.env.NODE_ENV || 'development'; - } - - const bundler = new Bundler(main, command); - - if (command.name() === 'serve') { - const server = await bundler.serve(command.port || 1234); - if (command.open) { - require('opn')(`http://localhost:${server.address().port}`); - } - } else { - bundler.bundle(); - } -} +// Node 8 supports native async functions - no need to use compiled code! +module.exports = + parseInt(process.versions.node, 10) < 8 + ? require('../lib/cli') + : require('../src/cli'); diff --git a/packages/core/parcel/src/cli.js b/packages/core/parcel/src/cli.js new file mode 100755 index 00000000000..ea71832a42b --- /dev/null +++ b/packages/core/parcel/src/cli.js @@ -0,0 +1,104 @@ +#!/usr/bin/env node + +require('v8-compile-cache'); +const chalk = require('chalk'); +const program = require('commander'); +const version = require('../package.json').version; + +program.version(version); + +program + .command('serve [input]') + .description('starts a development server') + .option('-p, --port ', 'set the port to serve on. defaults to 1234') + .option('-o, --open', 'automatically open in default browser') + .option( + '-d, --out-dir ', + 'set the output directory. defaults to "dist"' + ) + .option( + '--public-url ', + 'set the public URL to serve on. defaults to the same as the --out-dir option' + ) + .option('--no-hmr', 'disable hot module replacement') + .option('--no-cache', 'disable the filesystem cache') + .option('-V, --version', 'output the version number') + .action(bundle); + +program + .command('watch [input]') + .description('starts the bundler in watch mode') + .option( + '-d, --out-dir ', + 'set the output directory. defaults to "dist"' + ) + .option( + '--public-url ', + 'set the public URL to serve on. defaults to the same as the --out-dir option' + ) + .option('--no-hmr', 'disable hot module replacement') + .option('--no-cache', 'disable the filesystem cache') + .action(bundle); + +program + .command('build [input]') + .description('bundles for production') + .option( + '-d, --out-dir ', + 'set the output directory. defaults to "dist"' + ) + .option( + '--public-url ', + 'set the public URL to serve on. defaults to the same as the --out-dir option' + ) + .option('--no-minify', 'disable minification') + .option('--no-cache', 'disable the filesystem cache') + .action(bundle); + +program + .command('help [command]') + .description('display help information for a command') + .action(function(command) { + let cmd = program.commands.find(c => c.name() === command) || program; + cmd.help(); + }); + +program.on('--help', function() { + console.log(''); + console.log( + ' Run `' + + chalk.bold('parcel help ') + + '` for more information on specific commands' + ); + console.log(''); +}); + +// Make serve the default command +var args = process.argv; +if (!args[2] || !program.commands.some(c => c.name() === args[2])) { + args.splice(2, 0, 'serve'); +} + +program.parse(args); + +async function bundle(main, command) { + // Require bundler here so the help command is fast + const Bundler = require('../'); + + if (command.name() === 'build') { + process.env.NODE_ENV = 'production'; + } else { + process.env.NODE_ENV = process.env.NODE_ENV || 'development'; + } + + const bundler = new Bundler(main, command); + + if (command.name() === 'serve') { + const server = await bundler.serve(command.port || 1234); + if (command.open) { + require('opn')(`http://localhost:${server.address().port}`); + } + } else { + bundler.bundle(); + } +}