1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const tslib_1 = require("tslib");
|
4 |
|
5 | const childProcess = tslib_1.__importStar(require("child_process"));
|
6 | const isWsl = require('is-wsl');
|
7 | function open(target, opts = {}) {
|
8 |
|
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 |
|
20 |
|
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 |
|
31 |
|
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 |
|
46 | cmd = 'xdg-open';
|
47 | }
|
48 | if (appArgs.length > 0) {
|
49 | args = args.concat(appArgs);
|
50 | }
|
51 |
|
52 |
|
53 |
|
54 |
|
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 | }
|
76 | exports.default = open;
|