forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathopen-editor.js
42 lines (33 loc) · 935 Bytes
/
open-editor.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
'use strict';
var Promise = require('../ext/promise');
var spawn = require('child_process').spawn;
function openEditor(file) {
if (!openEditor.canEdit()) {
throw new Error('EDITOR environment variable is not set');
}
if (!file) {
throw new Error('No `file` option provided');
}
var editorArgs = openEditor._env().EDITOR.split(' ');
var editor = editorArgs.shift();
var editProcess = openEditor._spawn(editor, [file].concat(editorArgs), {stdio: 'inherit'});
return new Promise(function(resolve, reject) {
editProcess.on('close', function (code) {
if (code === 0) {
resolve();
} else {
reject();
}
});
});
}
openEditor.canEdit = function() {
return openEditor._env().EDITOR !== undefined;
};
openEditor._env = function() {
return process.env;
};
openEditor._spawn = function() {
return spawn.apply(this, arguments);
};
module.exports = openEditor;