forked from node-migrator-bot/node-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (45 loc) · 1.45 KB
/
index.js
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
var spawn = require('child_process').spawn;
var _ = require('underscore');
var async = require('async');
var Error = require ("errno-codes");
module.exports = function (file, opts, cb) {
if (typeof opts === 'function') {
cb = opts;
opts = {};
}
if (!opts) opts = {};
var asyncTasks = [];
var edInvoked = false;
var wellKnownEditors = opts.editors || [];
var editor = opts.editor || process.env.VISUAL || process.env.EDITOR;
if( editor ){
wellKnownEditors.push(editor)
}
if(/^win/.test(process.platform)){
wellKnownEditors = wellKnownEditors.concat(['start notepad++', 'notepad'])
} else {
wellKnownEditors = wellKnownEditors.concat(['vim', 'vi', 'pico', 'nano'])
}
_.uniq(wellKnownEditors).forEach(function(ed){
asyncTasks.push(function invokeEditor(then){
if(edInvoked) return then();
var args = ed.split(/\s+/);
var bin = args.shift();
var ps = spawn(bin, args.concat([ file ]), { stdio: 'inherit' });
ps.on('error', function () {
ps.removeAllListeners('exit');
then();
});
ps.on('exit', function (code, sig) {
edInvoked = true;
if (typeof cb === 'function') cb(code, sig)
then();
});
});
});
async.series(asyncTasks, function allTested(){
if(!edInvoked){
if (typeof cb === 'function') cb(Error.get (Error.ENOENT))
}
});
};