#!/usr/bin/env node 'use strict'; // Not using ES6 in that file since we want it to "launch" on older nodes. /* eslint-disable prefer-arrow-callback, prefer-template */ /* eslint-env es6:false */ var sysPath = require('path'); var join = sysPath.join; var fs = require('fs'); var childProcessFork = require('child_process').fork; var version = process.versions.node; if (parseInt(version) < 4) { console.error( 'Error: Brunch 2+ requires Node.js v4 or higher (you have v' + version + ') ' + 'Upgrade Node.js or use older Brunch (not recommended): npm i -g brunch@1' ); process.exit(1); } global[Symbol.for('start-time')] = Date.now(); var files = {old: 'cli.js', new: 'run-cli.js'}; var runSeparateWatchProcess = function(cliPath) { process.env.BRUNCH_FORKED_PROCESS = 'true'; var args = process.env.BRUNCH_DEVTOOLS ? ['--inspect', '--debug-brk'] : process.execArgv; var proc = childProcessFork(cliPath, process.argv.slice(2), {execArgv: args}); proc.on('message', function(message) { if (message === 'reload') { proc.kill(); runSeparateWatchProcess(cliPath); } }); process.on('SIGTERM', function() { proc.kill(); }); }; var loadBrunch = function(libPath) { var cmd = process.argv[2]; var runCli = join(libPath, files.new); // This approach is only needed for watch command. if ((cmd === 'w' || cmd === 'watch') && fs.existsSync(runCli)) { try { runSeparateWatchProcess(runCli); return; } catch (e) { // empty } } // This is needed to support cases when the local Brunch is of an older version // and does not have a run-cli.js file. require(join(libPath, files.old)).run(); }; var loadGlobalBrunch = function() { loadBrunch(join(fs.realpathSync(__dirname), '..', 'lib')); }; var localPath = join(sysPath.resolve('.'), 'node_modules', 'brunch', 'lib', files.old); if (fs.existsSync(localPath)) { try { loadBrunch(sysPath.dirname(localPath)); } catch (error) { console.error( 'Brunch: Local install exists, but failed to load it. ' + 'Continuing with global install:', error ); loadGlobalBrunch(); } } else { loadGlobalBrunch(); }