UNPKG

1.86 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.openBrowser = void 0;
7const open_1 = __importDefault(require("open"));
8const path_1 = __importDefault(require("path"));
9function isValidURL(str) {
10 try {
11 return !!new URL(str);
12 }
13 catch (error) {
14 return false;
15 }
16}
17function getBasePath(basePath) {
18 if (!basePath) {
19 return '';
20 }
21 if (basePath.endsWith('/')) {
22 return basePath.substring(0, basePath.length - 1);
23 }
24 return basePath;
25}
26async function openBrowser(config) {
27 const basePath = getBasePath(config.basePath);
28 let openPath;
29 if (typeof config.open === 'string') {
30 // user-provided open path
31 openPath = config.open === '' ? '/' : config.open;
32 }
33 else if (config.appIndex) {
34 const resolvedAppIndex = path_1.default.resolve(config.appIndex);
35 const relativeAppIndex = path_1.default.relative(config.rootDir, resolvedAppIndex);
36 const appIndexBrowserPath = `/${relativeAppIndex.split(path_1.default.sep).join('/')}`;
37 const appIndexDir = path_1.default.dirname(appIndexBrowserPath);
38 // if an appIndex was provided, use it's directory as open path
39 openPath = `${basePath}${appIndexDir.endsWith('/') ? appIndexDir : `${appIndexDir}/`}`;
40 }
41 else {
42 openPath = `${basePath}/`;
43 }
44 if (!isValidURL(openPath)) {
45 // construct a full URL to open if the user didn't provide a full URL
46 openPath = new URL(openPath, `http${config.http2 ? 's' : ''}://${config.hostname}:${config.port}`).href;
47 }
48 await (0, open_1.default)(openPath);
49}
50exports.openBrowser = openBrowser;
51//# sourceMappingURL=openBrowser.js.map
\No newline at end of file