97 lines
3.4 KiB
JavaScript
97 lines
3.4 KiB
JavaScript
"use strict";
|
|
|
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
|
|
|
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
|
|
const _require = require('child_process'),
|
|
execSync = _require.execSync;
|
|
|
|
const OSX_CHROME = 'google chrome';
|
|
const Actions = Object.freeze({
|
|
NONE: 0,
|
|
BROWSER: 1
|
|
});
|
|
|
|
const getBrowserEnv = () => {
|
|
// Attempt to honor this environment variable.
|
|
// It is specific to the operating system.
|
|
// See https://github.com/sindresorhus/opn#app for documentation.
|
|
const value = process.env.BROWSER;
|
|
let action;
|
|
|
|
if (!value) {
|
|
// Default.
|
|
action = Actions.BROWSER;
|
|
} else if (value.toLowerCase() === 'none') {
|
|
action = Actions.NONE;
|
|
} else {
|
|
action = Actions.BROWSER;
|
|
}
|
|
|
|
return {
|
|
action,
|
|
value
|
|
};
|
|
}; // Copy from
|
|
// https://github.com/facebook/create-react-app/blob/master/packages/react-dev-utils/openBrowser.js#L64
|
|
|
|
|
|
const startBrowserProcess = function startBrowserProcess(browser, url) {
|
|
let opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
// If we're on OS X, the user hasn't specifically
|
|
// requested a different browser, we can try opening
|
|
// Chrome with AppleScript. This lets us reuse an
|
|
// existing tab when possible instead of creating a new one.
|
|
const shouldTryOpenChromeWithAppleScript = process.platform === 'darwin' && (typeof browser !== 'string' || browser === OSX_CHROME);
|
|
|
|
if (shouldTryOpenChromeWithAppleScript) {
|
|
try {
|
|
// Try our best to reuse existing tab
|
|
// on OS X Google Chrome with AppleScript
|
|
execSync('ps cax | grep "Google Chrome"');
|
|
execSync(`osascript ../openChrome.applescript "${encodeURI(url)}"`, {
|
|
cwd: __dirname,
|
|
stdio: 'ignore'
|
|
});
|
|
return Promise.resolve(true);
|
|
} catch (err) {// Ignore errors.
|
|
// It it breaks, it will fallback to `opn` anyway
|
|
}
|
|
} // Another special case: on OS X, check if BROWSER has been set to "open".
|
|
// In this case, instead of passing `open` to `opn` (which won't work),
|
|
// just ignore it (thus ensuring the intended behavior, i.e. opening the system browser):
|
|
// https://github.com/facebook/create-react-app/pull/1690#issuecomment-283518768
|
|
|
|
|
|
if (process.platform === 'darwin' && browser === 'open') {
|
|
browser = undefined;
|
|
} // Fallback to opn
|
|
// (It will always open new tab)
|
|
|
|
|
|
const options = _objectSpread({
|
|
app: browser
|
|
}, opts);
|
|
|
|
console.debug(options);
|
|
return require('opn')(url, options);
|
|
};
|
|
|
|
module.exports = (target, opts) => {
|
|
const _getBrowserEnv = getBrowserEnv(),
|
|
action = _getBrowserEnv.action,
|
|
value = _getBrowserEnv.value;
|
|
|
|
switch (action) {
|
|
case Actions.NONE:
|
|
// Special case: BROWSER="none" will prevent opening completely.
|
|
return false;
|
|
|
|
case Actions.BROWSER:
|
|
return startBrowserProcess(value, target, opts);
|
|
|
|
default:
|
|
throw new Error('Not implemented.');
|
|
}
|
|
}; |