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