Skip to content

Commit 6848a38

Browse files
GAumalaromaindso
authored andcommitted
Support node scripts in BROWSER (facebook#1590)
* Support node scripts in BROWSER Modify OpenBrowser.js to run node scripts specified with the BROWSER environment variable . If the value of the BROWSER environment variable ends with '.js' a child process is spawned to execute the script with node.js. Any arguments passed to npm start are also passed to this script, as well as the url where the app is served. The command executed in the child process is: node <pathToScript> [OPTIONS] <url> Update User Guide. * Tweak code style * Pin dep * Comment out 0.10 docs
1 parent 8d4d728 commit 6848a38

File tree

3 files changed

+65
-7
lines changed

3 files changed

+65
-7
lines changed

packages/react-dev-utils/openBrowser.js

+63-6
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,61 @@
99

1010
'use strict';
1111

12+
var chalk = require('chalk');
1213
var execSync = require('child_process').execSync;
14+
var spawn = require('cross-spawn');
1315
var opn = require('opn');
1416

1517
// https://github.com/sindresorhus/opn#app
1618
var OSX_CHROME = 'google chrome';
1719

18-
function openBrowser(url) {
20+
const Actions = Object.freeze({
21+
NONE: 0,
22+
BROWSER: 1,
23+
SCRIPT: 2,
24+
});
25+
26+
function getBrowserEnv() {
1927
// Attempt to honor this environment variable.
2028
// It is specific to the operating system.
2129
// See https://github.com/sindresorhus/opn#app for documentation.
22-
var browser = process.env.BROWSER;
23-
24-
// Special case: BROWSER="none" will prevent opening completely.
25-
if (browser === 'none') {
26-
return false;
30+
const value = process.env.BROWSER;
31+
let action;
32+
if (!value) {
33+
// Default.
34+
action = Actions.BROWSER;
35+
} else if (value.toLowerCase().endsWith('.js')) {
36+
action = Actions.SCRIPT;
37+
} else if (value.toLowerCase() === 'none') {
38+
action = Actions.NONE;
39+
} else {
40+
action = Actions.BROWSER;
2741
}
42+
return { action, value };
43+
}
44+
45+
function executeNodeScript(scriptPath, url) {
46+
const extraArgs = process.argv.slice(2);
47+
const child = spawn('node', [scriptPath, ...extraArgs, url], {
48+
stdio: 'inherit',
49+
});
50+
child.on('close', code => {
51+
if (code !== 0) {
52+
console.log();
53+
console.log(
54+
chalk.red(
55+
'The script specified as BROWSER environment variable failed.'
56+
)
57+
);
58+
console.log(chalk.cyan(scriptPath) + ' exited with code ' + code + '.');
59+
console.log();
60+
return;
61+
}
62+
});
63+
return true;
64+
}
2865

66+
function startBrowserProcess(browser, url) {
2967
// If we're on OS X, the user hasn't specifically
3068
// requested a different browser, we can try opening
3169
// Chrome with AppleScript. This lets us reuse an
@@ -67,4 +105,23 @@ function openBrowser(url) {
67105
}
68106
}
69107

108+
/**
109+
* Reads the BROWSER evironment variable and decides what to do with it. Returns
110+
* true if it opened a browser or ran a node.js script, otherwise false.
111+
*/
112+
function openBrowser(url) {
113+
const { action, value } = getBrowserEnv();
114+
switch (action) {
115+
case Actions.NONE:
116+
// Special case: BROWSER="none" will prevent opening completely.
117+
return false;
118+
case Actions.SCRIPT:
119+
return executeNodeScript(value, url);
120+
case Actions.BROWSER:
121+
return startBrowserProcess(value, url);
122+
default:
123+
throw new Error('Not implemented.');
124+
}
125+
}
126+
70127
module.exports = openBrowser;

packages/react-dev-utils/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"anser": "1.1.0",
3333
"babel-code-frame": "6.20.0",
3434
"chalk": "1.1.3",
35+
"cross-spawn": "4.0.2",
3536
"escape-string-regexp": "1.0.5",
3637
"filesize": "3.3.0",
3738
"gzip-size": "3.0.0",

packages/react-scripts/template/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1560,7 +1560,7 @@ You can adjust various development and production settings by setting environmen
15601560
15611561
Variable | Development | Production | Usage
15621562
:--- | :---: | :---: | :---
1563-
BROWSER | :white_check_mark: | :x: | By default, Create React App will open the default system browser, favoring Chrome on macOS. Specify a [browser](https://github.com/sindresorhus/opn#app) to override this behavior, or set it to `none` to disable it completely.
1563+
BROWSER | :white_check_mark: | :x: | By default, Create React App will open the default system browser, favoring Chrome on macOS. Specify a [browser](https://github.com/sindresorhus/opn#app) to override this behavior, or set it to `none` to disable it completely. <!-- TODO: enable with 0.10: If you need to customize the way the browser is launched, you can specify a node script instead. Any arguments passed to `npm start` will also be passed to this script, and the url where your app is served will be the last argument. Your script's file name must have the `.js` extension. -->
15641564
HOST | :white_check_mark: | :x: | By default, the development web server binds to `localhost`. You may use this variable to specify a different host.
15651565
PORT | :white_check_mark: | :x: | By default, the development web server will attempt to listen on port 3000 or prompt you to attempt the next available port. You may use this variable to specify a different port.
15661566
HTTPS | :white_check_mark: | :x: | When set to `true`, Create React App will run the development server in `https` mode.

0 commit comments

Comments
 (0)