1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 | var version = process.versions.node;
|
6 | if (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 |
|
19 | var program = require('commander');
|
20 | var logger = require('loggy');
|
21 | var commands = require('.');
|
22 |
|
23 | program
|
24 | .version(require('../package.json').version, '-v, --version')
|
25 | .usage('[command] [options]');
|
26 |
|
27 | program.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 |
|
36 | program.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 |
|
46 | program.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 |
|
60 | var 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 |
|
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 |
|
82 |
|
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 |
|
95 | exports.run = function() {
|
96 | var args = process.argv.slice();
|
97 | var command = args[2];
|
98 |
|
99 |
|
100 |
|
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 | };
|