forked from mixi-inc/JavaScriptTraining
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
66 lines (53 loc) · 1.09 KB
/
index.js
File metadata and controls
66 lines (53 loc) · 1.09 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
'use strict';
var path = require('path');
var childProcess = require('child_process');
module.exports = function (target, app, cb) {
if (typeof target !== 'string') {
throw new Error('Expected a `target`');
}
if (typeof app === 'function') {
cb = app;
app = null;
}
var cmd;
var args = [];
if (process.platform === 'darwin') {
cmd = 'open';
if (cb) {
args.push('-W');
}
if (app) {
args.push('-a', app);
}
} else if (process.platform === 'win32') {
cmd = 'cmd';
args.push('/c', 'start');
target = target.replace(/&/g, '^&');
if (cb) {
args.push('/wait');
}
if (app) {
args.push(app);
}
} else {
if (app) {
cmd = app;
} else {
// http://portland.freedesktop.org/download/xdg-utils-1.1.0-rc1.tar.gz
cmd = path.join(__dirname, 'xdg-open');
}
}
args.push(target);
var opts = {};
if (!cb) {
// xdg-open will block the process unless stdio is ignored even if it's unref()'d
opts.stdio = 'ignore';
}
var cp = childProcess.spawn(cmd, args, opts);
if (cb) {
cp.once('error', cb);
cp.once('close', cb);
} else {
cp.unref();
}
};