UNPKG

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