UNPKG

2.26 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const tslib_1 = require("tslib");
4// this code is largely taken from opn
5const childProcess = tslib_1.__importStar(require("child_process"));
6const path = tslib_1.__importStar(require("path"));
7const isWsl = require('is-wsl');
8function open(target, opts = {}) {
9 // opts = {wait: true, ...opts}
10 let cmd;
11 let appArgs = [];
12 let args = [];
13 const cpOpts = {};
14 if (Array.isArray(opts.app)) {
15 appArgs = opts.app.slice(1);
16 opts.app = opts.app[0];
17 }
18 if (process.platform === 'darwin') {
19 cmd = 'open';
20 // if (opts.wait) {
21 // args.push('-W')
22 // }
23 if (opts.app) {
24 args.push('-a', opts.app);
25 }
26 }
27 else if (process.platform === 'win32' || isWsl) {
28 cmd = 'cmd' + (isWsl ? '.exe' : '');
29 args.push('/c', 'start', '""', '/b');
30 target = target.replace(/&/g, '^&');
31 // if (opts.wait) {
32 // args.push('/wait')
33 // }
34 if (opts.app) {
35 args.push(opts.app);
36 }
37 if (appArgs.length > 0) {
38 args = args.concat(appArgs);
39 }
40 }
41 else {
42 if (opts.app) {
43 cmd = opts.app;
44 }
45 else {
46 cmd = process.platform === 'android' ? 'xdg-open' : path.join(__dirname, 'xdg-open');
47 }
48 if (appArgs.length > 0) {
49 args = args.concat(appArgs);
50 }
51 // if (!opts.wait) {
52 // `xdg-open` will block the process unless
53 // stdio is ignored and it's detached from the parent
54 // even if it's unref'd
55 cpOpts.stdio = 'ignore';
56 cpOpts.detached = true;
57 // }
58 }
59 args.push(target);
60 if (process.platform === 'darwin' && appArgs.length > 0) {
61 args.push('--args');
62 args = args.concat(appArgs);
63 }
64 const cp = childProcess.spawn(cmd, args, cpOpts);
65 return new Promise((resolve, reject) => {
66 cp.once('error', reject);
67 cp.once('close', code => {
68 if (code > 0) {
69 reject(new Error('Exited with code ' + code));
70 return;
71 }
72 resolve(cp);
73 });
74 });
75}
76exports.default = open;