1 | #!/usr/bin/env node
|
2 | 'use strict';
|
3 |
|
4 | // Not using ES6 in that file since we want it to "launch" on older nodes.
|
5 | /* eslint-disable prefer-arrow-callback, prefer-template */
|
6 | /* eslint-env es6:false */
|
7 | var sysPath = require('path');
|
8 | var join = sysPath.join;
|
9 | var fs = require('fs');
|
10 | var childProcessFork = require('child_process').fork;
|
11 |
|
12 | var version = process.versions.node;
|
13 | if (parseInt(version) < 4) {
|
14 | console.error(
|
15 | 'Error: Brunch 2+ requires Node.js v4 or higher (you have v' + version + ') ' +
|
16 | 'Upgrade Node.js or use older Brunch (not recommended): npm i -g brunch@1'
|
17 | );
|
18 | process.exit(1);
|
19 | }
|
20 |
|
21 | global[Symbol.for('start-time')] = Date.now();
|
22 |
|
23 | var files = {old: 'cli.js', new: 'run-cli.js'};
|
24 | var runSeparateWatchProcess = function(cliPath) {
|
25 | process.env.BRUNCH_FORKED_PROCESS = 'true';
|
26 | var args = process.env.BRUNCH_DEVTOOLS ? ['--inspect', '--debug-brk'] : process.execArgv;
|
27 | var proc = childProcessFork(cliPath, process.argv.slice(2), {execArgv: args});
|
28 | proc.on('message', function(message) {
|
29 | if (message === 'reload') {
|
30 | proc.kill();
|
31 | runSeparateWatchProcess(cliPath);
|
32 | }
|
33 | });
|
34 |
|
35 | process.on('SIGTERM', function() {
|
36 | proc.kill();
|
37 | });
|
38 | };
|
39 |
|
40 | var loadBrunch = function(libPath) {
|
41 | var cmd = process.argv[2];
|
42 | var runCli = join(libPath, files.new);
|
43 |
|
44 | // This approach is only needed for watch command.
|
45 | if ((cmd === 'w' || cmd === 'watch') && fs.existsSync(runCli)) {
|
46 | try {
|
47 | runSeparateWatchProcess(runCli);
|
48 | return;
|
49 | } catch (e) {
|
50 | // empty
|
51 | }
|
52 | }
|
53 |
|
54 | // This is needed to support cases when the local Brunch is of an older version
|
55 | // and does not have a run-cli.js file.
|
56 | require(join(libPath, files.old)).run();
|
57 | };
|
58 |
|
59 | var loadGlobalBrunch = function() {
|
60 | loadBrunch(join(fs.realpathSync(__dirname), '..', 'lib'));
|
61 | };
|
62 |
|
63 | var localPath = join(sysPath.resolve('.'), 'node_modules', 'brunch', 'lib', files.old);
|
64 | if (fs.existsSync(localPath)) {
|
65 | try {
|
66 | loadBrunch(sysPath.dirname(localPath));
|
67 | } catch (error) {
|
68 | console.error(
|
69 | 'Brunch: Local install exists, but failed to load it. ' +
|
70 | 'Continuing with global install:', error
|
71 | );
|
72 | loadGlobalBrunch();
|
73 | }
|
74 | } else {
|
75 | loadGlobalBrunch();
|
76 | }
|