UNPKG

2.18 kBPlain TextView Raw
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 */
7var sysPath = require('path');
8var join = sysPath.join;
9var fs = require('fs');
10var childProcessFork = require('child_process').fork;
11
12var version = process.versions.node;
13if (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
21global[Symbol.for('start-time')] = Date.now();
22
23var files = {old: 'cli.js', new: 'run-cli.js'};
24var 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
40var 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
59var loadGlobalBrunch = function() {
60 loadBrunch(join(fs.realpathSync(__dirname), '..', 'lib'));
61};
62
63var localPath = join(sysPath.resolve('.'), 'node_modules', 'brunch', 'lib', files.old);
64if (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}