UNPKG

five-server

Version:

Development Server with Live Reload Capability. (Maintained Fork of Live Server)

141 lines 5.77 kB
"use strict"; /** * @author Yannick Deubel (https://github.com/yandeu) * @copyright Copyright (c) 2021 Yannick Deubel * @license {@link https://github.com/yandeu/five-server/blob/main/LICENSE LICENSE} */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.openBrowser = exports.OpenBrowser = void 0; const node_child_process_1 = __importDefault(require("node:child_process")); const colors_1 = require("./colors"); const msg_1 = require("./msg"); const open_cjs_1 = __importDefault(require("@yandeu/open-cjs")); class OpenBrowser { constructor(_open) { this._open = _open; } async _o(target, cfg = {}) { return new Promise(resolve => { try { this._open(target, { wait: false, ...cfg }).then(({ command, cliArguments, childProcessOptions }) => { const child = node_child_process_1.default.spawn(command, cliArguments, childProcessOptions); child.once('error', () => { return resolve(); }); child.once('exit', () => { return resolve(); }); child.once('spawn', () => { return resolve(child); }); }); } catch (error) { resolve(); } }); } async launchDefaultBrowser(target) { await this.launchBrowser(target, 'default'); } async open(target, browser) { if (!browser || browser === 'default') return await this._o(target); const hasArguments = browser.includes('--'); if (!hasArguments) return await this._o(target, { app: { name: browser } }); if (hasArguments) { const b = browser.split('--').map(c => c.trim()); return await this._o(target, { app: { name: b.shift(), arguments: b.map(arg => `--${arg}`) } }); } } async launchBrowser(target, browser = 'default', index = -1) { let res; // if browser is empty array; set to default if (Array.isArray(browser) && browser.length === 0) { browser = 'default'; } // browser is string if (typeof browser === 'string') res = await this.open(target, browser); // browser is non-empty array else if (Array.isArray(browser)) { index++; res = await this.open(target, browser[index]); } const is_undefined_str = browser => typeof browser === 'string' && browser === 'undefined'; if (!res) { if (typeof browser === 'string') { if (browser === 'default') { msg_1.message.log((0, colors_1.colors)(`Could not open the default browser. Will abort!`, 'red')); return; } else { if (!is_undefined_str(browser)) { msg_1.message.log((0, colors_1.colors)(`Could not open browser "${browser}". Trying the default browser next.`, 'yellow')); } await this.launchDefaultBrowser(target); } } else if (Array.isArray(browser)) { if (typeof browser[index + 1] === 'undefined') { if (!is_undefined_str(browser[index])) { msg_1.message.log((0, colors_1.colors)(`Could not open browser "${browser[index]}". Trying the default browser next.`, 'yellow')); } await this.launchDefaultBrowser(target); } else { if (!is_undefined_str(browser[index])) { msg_1.message.log((0, colors_1.colors)(`Could not open browser "${browser[index]}". Trying "${browser[index + 1]}" next.`, 'yellow')); } await this.launchBrowser(target, browser, index); } } } } /** Launch a new browser window. */ async openBrowser(openURL, path = '', browser = 'default') { // openURL is required if (!openURL || typeof openURL !== 'string') return; // Don't open a browser if (path === null) return; // remove trailing slash if (openURL) openURL = openURL.replace(/\/+$/, ''); const isURL = path => /^https?.\/\//gm.test(path); // add leading slash if (!isURL(path) && typeof path === 'string' && path.length > 0 && !/^\//.test(path)) path = `/${path}`; // Try to open one browser from a list of browsers if (Array.isArray(path)) { for (let p of path) { if (isURL(p)) await this.launchBrowser(p, browser); else { // add leading slash if (typeof p === 'string' && !/^\//.test(p)) p = `/${p}`; await this.launchBrowser(`${openURL}${p}`, browser); } } } // Open browser "browser" if (typeof path === 'string') { if (isURL(path)) await this.launchBrowser(path, browser); else await this.launchBrowser(`${openURL}${path}`, browser); } } } exports.OpenBrowser = OpenBrowser; const ob = new OpenBrowser(open_cjs_1.default); exports.openBrowser = ob.openBrowser.bind(ob); //# sourceMappingURL=openBrowser.js.map