Skip to content
  • Sponsor facebook/create-react-app

  • Notifications You must be signed in to change notification settings
  • Fork 27k
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 04adf67

Browse files
committedMay 9, 2017
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.
1 parent f35593c commit 04adf67

File tree

3 files changed

+70
-7
lines changed

3 files changed

+70
-7
lines changed
 

‎packages/react-dev-utils/openBrowser.js

+68-6
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,61 @@
1010
'use strict';
1111

1212
var execSync = require('child_process').execSync;
13+
var spawn = require('cross-spawn');
1314
var opn = require('opn');
1415

1516
// https://github.com/sindresorhus/opn#app
1617
var OSX_CHROME = 'google chrome';
1718

18-
function openBrowser(url) {
19+
20+
/**
21+
* Use an enum for the possible uses of the BROWSER environment variable
22+
* 'browser': open a browser specified by the user. if value is not specified,
23+
* use the system's default browser.
24+
* 'browser': open a browser.
25+
* 'none': Don't open anything.
26+
* 'script': run a node.js script.
27+
*
28+
*/
29+
const BROWSER_TYPES = Object.freeze({
30+
'browser': 0,
31+
'none': 1,
32+
'script': 2,
33+
})
34+
35+
/**
36+
* Returns an object with the BROWSER environment variable's value and type
37+
*/
38+
function getBrowserEnv() {
1939
// Attempt to honor this environment variable.
2040
// It is specific to the operating system.
2141
// See https://github.com/sindresorhus/opn#app for documentation.
22-
var browser = process.env.BROWSER;
42+
const browserStr = process.env.BROWSER;
43+
const result = { value: browserStr }
44+
if (!browserStr)
45+
result.type = BROWSER_TYPES.browser;
46+
else if (browserStr && browserStr.endsWith('.js'))
47+
result.type = BROWSER_TYPES.script;
48+
else if (browserStr.toLowerCase() === 'none')
49+
result.type = BROWSER_TYPES.none;
50+
else
51+
result.type = BROWSER_TYPES.browser;
52+
return result
53+
}
2354

24-
// Special case: BROWSER="none" will prevent opening completely.
25-
if (browser === 'none') {
26-
return false;
27-
}
55+
/**
56+
* spawns a new child process to execute a node.js script
57+
*/
58+
function executeNodeScript(scriptPath, url) {
59+
const extraArgs = process.argv.slice(2);
60+
const args = [scriptPath]
61+
.concat(extraArgs) // add any extra flags
62+
.concat(url); // add url last
63+
spawn('node', args);
64+
return true;
65+
}
2866

67+
function startBrowserProcess(browser, url) {
2968
// If we're on OS X, the user hasn't specifically
3069
// requested a different browser, we can try opening
3170
// Chrome with AppleScript. This lets us reuse an
@@ -67,4 +106,27 @@ function openBrowser(url) {
67106
}
68107
}
69108

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

‎packages/react-dev-utils/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"anser": "1.1.0",
3030
"babel-code-frame": "6.20.0",
3131
"chalk": "1.1.3",
32+
"cross-spawn": "^4.0.0",
3233
"escape-string-regexp": "1.0.5",
3334
"filesize": "3.3.0",
3435
"gzip-size": "3.0.0",

‎packages/react-scripts/template/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1551,7 +1551,7 @@ You can adjust various development and production settings by setting environmen
15511551
15521552
Variable | Development | Production | Usage
15531553
:--- | :---: | :---: | :---
1554-
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.
1554+
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. 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.
15551555
HOST | :white_check_mark: | :x: | By default, the development web server binds to `localhost`. You may use this variable to specify a different host.
15561556
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.
15571557
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)
Please sign in to comment.