UNPKG

4.52 kBJavaScriptView Raw
1'use strict';
2
3/* eslint-disable prefer-arrow-callback, prefer-template */
4/* eslint-env es6:false */
5var version = process.versions.node;
6if (parseInt(version) < 4) {
7 console.error(
8 'Your global Brunch installation is trying to load local Brunch v2+, ' +
9 'which only supports Node.js v4 or higher (you have ' + version + ').\n' +
10 'You have two choices:\n' +
11 ' a) Update Node.js to v4+. Then update global Brunch: ' +
12 '`npm un -g brunch && npm i -g brunch`\n' +
13 ' b) Adjust package.json to use Brunch 1.x (outdated, not recommended).'
14 );
15 process.exit(1);
16}
17
18// The files use ES2015, which cannot be used with old Brunches.
19var program = require('commander');
20var logger = require('loggy');
21var commands = require('.');
22
23program
24 .version(require('../package.json').version, '-v, --version')
25 .usage('[command] [options]');
26
27program.command('new [path]')
28 .alias('n')
29 .description('Create new Brunch project in path.')
30 .option('-s, --skeleton [name]', 'skeleton name or URL from brunch.io/skeletons')
31 .on('--help', function() {
32 require('init-skeleton').printBanner('brunch new -s');
33 })
34 .action(commands.new);
35
36program.command('build [path]')
37 .alias('b')
38 .description('Build a Brunch project.')
39 .option('-e, --env [setting]', 'specify a set of override settings to apply')
40 .option('-p, --production', 'same as `--env production`')
41 .option('-d, --debug [pattern]', 'print verbose debug output to stdout')
42 .option('-j, --jobs [num]', 'parallelize the build')
43 .option('-c, --config [path]', 'specify a path to Brunch config file')
44 .action(commands.build);
45
46program.command('watch [path]')
47 .alias('w')
48 .description('Watch Brunch directory and rebuild if something changed.')
49 .option('-e, --env [setting]', 'specify a set of override settings to apply')
50 .option('-p, --production', 'same as `--env production`')
51 .option('-s, --server', 'run a simple http server for the public dir on localhost')
52 .option('-n, --network', 'if `server` was given, allow access from the network')
53 .option('-P, --port [port]', 'if `server` was given, listen on this port')
54 .option('-d, --debug [pattern]', 'print verbose debug output to stdout')
55 .option('-j, --jobs [num]', 'parallelize the build')
56 .option('-c, --config [path]', 'specify a path to Brunch config file')
57 .option('--stdin', 'listen to stdin and exit when stdin closes')
58 .action(commands.watch);
59
60var checkForRemovedOptions = function(args, command) {
61 var hasArg = function(deprs) {
62 return deprs.some(function(arg) {
63 return args.includes(arg);
64 });
65 };
66 var hasCommand = function(valid) {
67 return valid.includes(command);
68 };
69 // Deprecations
70 if (hasArg(['-o', '--optimize'])) {
71 return '--optimize has been removed. Use `-p / --production`';
72 }
73 if (hasCommand(['g', 'd', 'generate', 'destroy'])) {
74 return '`brunch generate / destroy` command has been removed.\n\nUse scaffolt (https://github.com/paulmillr/scaffolt)\nsuccessor or similar:\n npm install -g scaffolt\n scaffolt <type> <name> [options]\n scaffolt <type> <name> [options] --revert';
75 }
76 if (hasCommand(['t', 'test'])) {
77 return '`brunch test` command has been removed.\n\nUse mocha-phantomjs (http://metaskills.net/mocha-phantomjs/)\nsuccessor or similar:\n npm install -g mocha-phantomjs\n mocha-phantomjs [options] <your-html-file-or-url>';
78 }
79 var pIndex = args.indexOf('-p');
80 if (pIndex !== -1) {
81 // if -p is followed by a number, the user probably wants to specify the port
82 // the new option name for port is -P
83 var port = +args[pIndex + 1];
84 if (Number.isInteger(port)) {
85 var parsed = args.slice(2).map(function(arg) {
86 return arg === '-p' ? '-P' : arg;
87 });
88 var corrected = ['brunch'].concat(parsed).join(' ');
89 return 'The `-p` option is no longer used to specify the port. Use `-P` instead, e.g. `' + corrected + '`';
90 }
91 }
92};
93
94// The fn would be executed every time user run `bin/brunch`.
95exports.run = function() {
96 var args = process.argv.slice();
97 var command = args[2];
98
99 // Need this since `brunch` binary will fork and run `run-cli`,
100 // but we still want to see `brunch` in help.
101 args[1] = 'brunch';
102
103 var error = checkForRemovedOptions(args, command);
104 if (error) {
105 logger.error(error);
106 return;
107 }
108
109 program.parse(args);
110
111 var validCommand = program.commands.some(function(cmd) {
112 return cmd.name() === command || cmd.alias() === command;
113 });
114
115 if (!validCommand) program.help();
116};