1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | 'use strict';
|
9 |
|
10 | var chalk = require('chalk');
|
11 | var execSync = require('child_process').execSync;
|
12 | var spawn = require('cross-spawn');
|
13 | var opn = require('opn');
|
14 |
|
15 |
|
16 | var OSX_CHROME = 'google chrome';
|
17 |
|
18 | const Actions = Object.freeze({
|
19 | NONE: 0,
|
20 | BROWSER: 1,
|
21 | SCRIPT: 2,
|
22 | });
|
23 |
|
24 | function getBrowserEnv() {
|
25 |
|
26 |
|
27 |
|
28 | const value = process.env.BROWSER;
|
29 | let action;
|
30 | if (!value) {
|
31 |
|
32 | action = Actions.BROWSER;
|
33 | } else if (value.toLowerCase().endsWith('.js')) {
|
34 | action = Actions.SCRIPT;
|
35 | } else if (value.toLowerCase() === 'none') {
|
36 | action = Actions.NONE;
|
37 | } else {
|
38 | action = Actions.BROWSER;
|
39 | }
|
40 | return { action, value };
|
41 | }
|
42 |
|
43 | function executeNodeScript(scriptPath, url) {
|
44 | const extraArgs = process.argv.slice(2);
|
45 | const child = spawn('node', [scriptPath, ...extraArgs, url], {
|
46 | stdio: 'inherit',
|
47 | });
|
48 | child.on('close', code => {
|
49 | if (code !== 0) {
|
50 | console.log();
|
51 | console.log(
|
52 | chalk.red(
|
53 | 'The script specified as BROWSER environment variable failed.'
|
54 | )
|
55 | );
|
56 | console.log(chalk.cyan(scriptPath) + ' exited with code ' + code + '.');
|
57 | console.log();
|
58 | return;
|
59 | }
|
60 | });
|
61 | return true;
|
62 | }
|
63 |
|
64 | function startBrowserProcess(browser, url) {
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | const shouldTryOpenChromeWithAppleScript =
|
70 | process.platform === 'darwin' &&
|
71 | (typeof browser !== 'string' || browser === OSX_CHROME);
|
72 |
|
73 | if (shouldTryOpenChromeWithAppleScript) {
|
74 | try {
|
75 |
|
76 |
|
77 | execSync('ps cax | grep "Google Chrome"');
|
78 | execSync('osascript openChrome.applescript "' + encodeURI(url) + '"', {
|
79 | cwd: __dirname,
|
80 | stdio: 'ignore',
|
81 | });
|
82 | return true;
|
83 | } catch (err) {
|
84 |
|
85 | }
|
86 | }
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 | if (process.platform === 'darwin' && browser === 'open') {
|
93 | browser = undefined;
|
94 | }
|
95 |
|
96 |
|
97 |
|
98 | try {
|
99 | var options = { app: browser };
|
100 | opn(url, options).catch(() => {});
|
101 | return true;
|
102 | } catch (err) {
|
103 | return false;
|
104 | }
|
105 | }
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 | function openBrowser(url) {
|
112 | const { action, value } = getBrowserEnv();
|
113 | switch (action) {
|
114 | case Actions.NONE:
|
115 |
|
116 | return false;
|
117 | case Actions.SCRIPT:
|
118 | return executeNodeScript(value, url);
|
119 | case Actions.BROWSER:
|
120 | return startBrowserProcess(value, url);
|
121 | default:
|
122 | throw new Error('Not implemented.');
|
123 | }
|
124 | }
|
125 |
|
126 | module.exports = openBrowser;
|