forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopen.ts
More file actions
85 lines (70 loc) · 2.19 KB
/
open.ts
File metadata and controls
85 lines (70 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
'use strict';
//https://github.com/sindresorhus/opn/blob/master/index.js
//Modified as this uses target as an argument
import * as childProcess from 'child_process';
// tslint:disable:no-any no-function-expression prefer-template
export function open(opts: any): Promise<childProcess.ChildProcess> {
// opts = objectAssign({wait: true}, opts);
if (!opts.hasOwnProperty('wait')) {
(<any>opts).wait = true;
}
let cmd;
let appArgs = [];
let args: string[] = [];
const cpOpts: any = {};
if (opts.cwd && typeof opts.cwd === 'string' && opts.cwd.length > 0) {
cpOpts.cwd = opts.cwd;
}
if (opts.env && Object.keys(opts.env).length > 0) {
cpOpts.env = opts.env;
}
if (Array.isArray(opts.app)) {
appArgs = opts.app.slice(1);
opts.app = opts.app[0];
}
if (process.platform === 'darwin') {
const sudoPrefix = opts.sudo === true ? 'sudo ' : '';
cmd = 'osascript';
args = [
'-e',
'tell application "terminal"',
'-e',
'activate',
'-e',
'do script "' + sudoPrefix + [opts.app].concat(appArgs).join(' ') + '"',
'-e',
'end tell'
];
} else if (process.platform === 'win32') {
cmd = 'cmd';
args.push('/c', 'start');
if (opts.wait) {
args.push('/wait');
}
if (opts.app) {
args.push(opts.app);
}
if (appArgs.length > 0) {
args = args.concat(appArgs);
}
} else {
cmd = 'gnome-terminal';
const sudoPrefix = opts.sudo === true ? 'sudo ' : '';
args = ['-x', 'sh', '-c', `"${sudoPrefix}${opts.app}" ${appArgs.join(' ')}`];
}
const cp = childProcess.spawn(cmd, args, cpOpts);
if (opts.wait) {
return new Promise(function(resolve, reject) {
cp.once('error', reject);
cp.once('close', function(code) {
if (code > 0) {
reject(new Error(`Exited with code ${code}`));
return;
}
resolve(cp);
});
});
}
cp.unref();
return Promise.resolve(cp);
}