|
10 | 10 | 'use strict';
|
11 | 11 |
|
12 | 12 | var execSync = require('child_process').execSync;
|
| 13 | +var spawn = require('cross-spawn'); |
13 | 14 | var opn = require('opn');
|
14 | 15 |
|
15 | 16 | // https://github.com/sindresorhus/opn#app
|
16 | 17 | var OSX_CHROME = 'google chrome';
|
17 | 18 |
|
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() { |
19 | 39 | // Attempt to honor this environment variable.
|
20 | 40 | // It is specific to the operating system.
|
21 | 41 | // 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 | +} |
23 | 54 |
|
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 | +} |
28 | 66 |
|
| 67 | +function startBrowserProcess(browser, url) { |
29 | 68 | // If we're on OS X, the user hasn't specifically
|
30 | 69 | // requested a different browser, we can try opening
|
31 | 70 | // Chrome with AppleScript. This lets us reuse an
|
@@ -67,4 +106,27 @@ function openBrowser(url) {
|
67 | 106 | }
|
68 | 107 | }
|
69 | 108 |
|
| 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 | + |
70 | 132 | module.exports = openBrowser;
|
0 commit comments