UNPKG

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