1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | exports.openBrowser = void 0;
|
7 | const open_1 = __importDefault(require("open"));
|
8 | const path_1 = __importDefault(require("path"));
|
9 | function isValidURL(str) {
|
10 | try {
|
11 | return !!new URL(str);
|
12 | }
|
13 | catch (error) {
|
14 | return false;
|
15 | }
|
16 | }
|
17 | function 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 | }
|
26 | async function openBrowser(config) {
|
27 | const basePath = getBasePath(config.basePath);
|
28 | let openPath;
|
29 | if (typeof config.open === 'string') {
|
30 |
|
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 |
|
39 | openPath = `${basePath}${appIndexDir.endsWith('/') ? appIndexDir : `${appIndexDir}/`}`;
|
40 | }
|
41 | else {
|
42 | openPath = `${basePath}/`;
|
43 | }
|
44 | if (!isValidURL(openPath)) {
|
45 |
|
46 | openPath = new URL(openPath, `http${config.http2 ? 's' : ''}://${config.hostname}:${config.port}`).href;
|
47 | }
|
48 | await (0, open_1.default)(openPath);
|
49 | }
|
50 | exports.openBrowser = openBrowser;
|
51 |
|
\ | No newline at end of file |