UNPKG

6.44 kBJavaScriptView Raw
1// Generated by CoffeeScript 1.12.7
2
3/*
4 ExternalEditor
5 Kevin Gravier <kevin@mrkmg.com>
6 MIT
7 */
8
9(function() {
10 var ChatDet, CreateFileError, ExternalEditor, FS, IConvLite, LaunchEditorError, ReadFileError, RemoveFileError, Spawn, SpawnSync, Temp,
11 bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
12
13 FS = require('fs');
14
15 Temp = require('tmp');
16
17 SpawnSync = require('child_process').spawnSync;
18
19 Spawn = require('child_process').spawn;
20
21 IConvLite = require('iconv-lite');
22
23 ChatDet = require('chardet');
24
25 CreateFileError = require('./errors/CreateFileError');
26
27 ReadFileError = require('./errors/ReadFileError');
28
29 RemoveFileError = require('./errors/RemoveFileError');
30
31 LaunchEditorError = require('./errors/LaunchEditorError');
32
33 ExternalEditor = (function() {
34 ExternalEditor.edit = function(text) {
35 var editor;
36 if (text == null) {
37 text = '';
38 }
39 editor = new ExternalEditor(text);
40 editor.run();
41 editor.cleanup();
42 return editor.text;
43 };
44
45 ExternalEditor.editAsync = function(text, callback) {
46 var editor;
47 if (text == null) {
48 text = '';
49 }
50 editor = new ExternalEditor(text);
51 return editor.runAsync(function(error_run, text) {
52 var error_cleanup;
53 if (!error_run) {
54 try {
55 editor.cleanup();
56 if (typeof callback === 'function') {
57 return setImmediate(callback, null, text);
58 }
59 } catch (error) {
60 error_cleanup = error;
61 if (typeof callback === 'function') {
62 return setImmediate(callback, error_cleanup, null);
63 }
64 }
65 } else {
66 if (typeof callback === 'function') {
67 return setImmediate(callback, error_run, null);
68 }
69 }
70 });
71 };
72
73 ExternalEditor.CreateFileError = CreateFileError;
74
75 ExternalEditor.ReadFileError = ReadFileError;
76
77 ExternalEditor.RemoveFileError = RemoveFileError;
78
79 ExternalEditor.LaunchEditorError = LaunchEditorError;
80
81 ExternalEditor.prototype.text = '';
82
83 ExternalEditor.prototype.temp_file = void 0;
84
85 ExternalEditor.prototype.editor = {
86 bin: void 0,
87 args: []
88 };
89
90 ExternalEditor.prototype.last_exit_status = void 0;
91
92 function ExternalEditor(text1) {
93 this.text = text1 != null ? text1 : '';
94 this.launchEditorAsync = bind(this.launchEditorAsync, this);
95 this.launchEditor = bind(this.launchEditor, this);
96 this.removeTemporaryFile = bind(this.removeTemporaryFile, this);
97 this.readTemporaryFile = bind(this.readTemporaryFile, this);
98 this.createTemporaryFile = bind(this.createTemporaryFile, this);
99 this.determineEditor = bind(this.determineEditor, this);
100 this.cleanup = bind(this.cleanup, this);
101 this.runAsync = bind(this.runAsync, this);
102 this.run = bind(this.run, this);
103 this.determineEditor();
104 this.createTemporaryFile();
105 }
106
107 ExternalEditor.prototype.run = function() {
108 this.launchEditor();
109 return this.readTemporaryFile();
110 };
111
112 ExternalEditor.prototype.runAsync = function(callback) {
113 var error_launch;
114 try {
115 return this.launchEditorAsync((function(_this) {
116 return function() {
117 var error_read;
118 try {
119 _this.readTemporaryFile();
120 if (typeof callback === 'function') {
121 return setImmediate(callback, null, _this.text);
122 }
123 } catch (error) {
124 error_read = error;
125 if (typeof callback === 'function') {
126 return setImmediate(callback, error_read, null);
127 }
128 }
129 };
130 })(this));
131 } catch (error) {
132 error_launch = error;
133 if (typeof callback === 'function') {
134 return setImmediate(callback, error_launch, null);
135 }
136 }
137 };
138
139 ExternalEditor.prototype.cleanup = function() {
140 return this.removeTemporaryFile();
141 };
142
143 ExternalEditor.prototype.determineEditor = function() {
144 var args, ed, editor;
145 ed = /^win/.test(process.platform) ? 'notepad' : 'vim';
146 editor = process.env.VISUAL || process.env.EDITOR || ed;
147 args = editor.split(/\s+/);
148 this.editor.bin = args.shift();
149 return this.editor.args = args;
150 };
151
152 ExternalEditor.prototype.createTemporaryFile = function() {
153 var e;
154 try {
155 this.temp_file = Temp.tmpNameSync({});
156 return FS.writeFileSync(this.temp_file, this.text, {
157 encoding: 'utf8'
158 });
159 } catch (error) {
160 e = error;
161 throw new CreateFileError(e);
162 }
163 };
164
165 ExternalEditor.prototype.readTemporaryFile = function() {
166 var buffer, e, encoding;
167 try {
168 buffer = FS.readFileSync(this.temp_file);
169 if (!buffer.length) {
170 return this.text = '';
171 }
172 encoding = ChatDet.detect(buffer);
173 return this.text = IConvLite.decode(buffer, encoding);
174 } catch (error) {
175 e = error;
176 throw new ReadFileError(e);
177 }
178 };
179
180 ExternalEditor.prototype.removeTemporaryFile = function() {
181 var e;
182 try {
183 return FS.unlinkSync(this.temp_file);
184 } catch (error) {
185 e = error;
186 throw new RemoveFileError(e);
187 }
188 };
189
190 ExternalEditor.prototype.launchEditor = function() {
191 var e, run;
192 try {
193 run = SpawnSync(this.editor.bin, this.editor.args.concat([this.temp_file]), {
194 stdio: 'inherit'
195 });
196 return this.last_exit_status = run.status;
197 } catch (error) {
198 e = error;
199 throw new LaunchEditorError(e);
200 }
201 };
202
203 ExternalEditor.prototype.launchEditorAsync = function(callback) {
204 var child_process, e;
205 try {
206 child_process = Spawn(this.editor.bin, this.editor.args.concat([this.temp_file]), {
207 stdio: 'inherit'
208 });
209 return child_process.on('exit', (function(_this) {
210 return function(code) {
211 _this.last_exit_status = code;
212 if (typeof callback === 'function') {
213 return callback();
214 }
215 };
216 })(this));
217 } catch (error) {
218 e = error;
219 throw new LaunchEditorError(e);
220 }
221 };
222
223 return ExternalEditor;
224
225 })();
226
227 module.exports = ExternalEditor;
228
229}).call(this);