UNPKG

2.18 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 isWsl = require('is-wsl');
7function open(target, opts = {}) {
8 // opts = {wait: true, ...opts}
9 let cmd;
10 let appArgs = [];
11 let args = [];
12 const cpOpts = {};
13 if (Array.isArray(opts.app)) {
14 appArgs = opts.app.slice(1);
15 opts.app = opts.app[0];
16 }
17 if (process.platform === 'darwin') {
18 cmd = 'open';
19 // if (opts.wait) {
20 // args.push('-W')
21 // }
22 if (opts.app) {
23 args.push('-a', opts.app);
24 }
25 }
26 else if (process.platform === 'win32' || isWsl) {
27 cmd = 'cmd' + (isWsl ? '.exe' : '');
28 args.push('/c', 'start', '""', '/b');
29 target = target.replace(/&/g, '^&');
30 // if (opts.wait) {
31 // args.push('/wait')
32 // }
33 if (opts.app) {
34 args.push(opts.app);
35 }
36 if (appArgs.length > 0) {
37 args = args.concat(appArgs);
38 }
39 }
40 else {
41 if (opts.app) {
42 cmd = opts.app;
43 }
44 else {
45 // try local xdg-open
46 cmd = '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;