2023-12-18 13:12:25 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const open = require('opn');
|
|
|
|
const isAbsoluteUrl = require('is-absolute-url');
|
|
|
|
|
|
|
|
function runOpen(uri, options, log) {
|
|
|
|
// https://github.com/webpack/webpack-dev-server/issues/1990
|
|
|
|
let openOptions = { wait: false };
|
2024-01-16 21:26:16 +08:00
|
|
|
let openOptionValue = '';
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
if (typeof options.open === 'string') {
|
|
|
|
openOptions = Object.assign({}, openOptions, { app: options.open });
|
2024-01-16 21:26:16 +08:00
|
|
|
openOptionValue = `: "${options.open}"`;
|
|
|
|
} else if (typeof options.open === 'object') {
|
|
|
|
openOptions = options.open;
|
|
|
|
openOptionValue = `: "${JSON.stringify(options.open)}"`;
|
2023-12-18 13:12:25 +08:00
|
|
|
}
|
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
const pages =
|
|
|
|
typeof options.openPage === 'string'
|
|
|
|
? [options.openPage]
|
|
|
|
: options.openPage || [''];
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
return Promise.all(
|
|
|
|
pages.map((page) => {
|
|
|
|
const pageUrl = page && isAbsoluteUrl(page) ? page : `${uri}${page}`;
|
|
|
|
|
|
|
|
return open(pageUrl, openOptions).catch(() => {
|
|
|
|
log.warn(
|
|
|
|
`Unable to open "${pageUrl}" in browser${openOptionValue}. If you are running in a headless environment, please do not use the --open flag`
|
|
|
|
);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
2023-12-18 13:12:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = runOpen;
|