serverless/lib/utils/openBrowser.js
Mariusz Nowak 80fef65397 refactor(CLI): Upgrade to v7 of open library
It's to unify open browser method with dashboard plugin, and this method feels more solid
2022-01-06 14:48:40 +01:00

31 lines
1012 B
JavaScript

'use strict';
/* eslint-disable no-console */
const open = require('open');
const chalk = require('chalk');
const isDockerContainer = require('is-docker');
const { legacy, log, style } = require('@serverless/utils/log');
module.exports = function openBrowser(url) {
legacy.write(`\nIf your browser does not open automatically, please open the URL: ${url}\n\n`);
log.notice();
log.notice(
style.aside(`If your browser does not open automatically, please open this URL: ${url}`)
);
log.notice();
let browser = process.env.BROWSER;
if (browser === 'none' || isDockerContainer()) return;
if (process.platform === 'darwin' && browser === 'open') browser = undefined;
open(url).then((subprocess) =>
subprocess.on('error', (err) => {
if (process.env.SLS_DEBUG) {
legacy.write(
`Serverless: ${chalk.red(`Opening of browser window errored with ${err.stack}`)}\n`
);
}
log.info(`Opening of browser window errored with ${err.stack}`);
})
);
};