5 * Kevin Gravier <kevin@mrkmg.com>
8 Object.defineProperty(exports, "__esModule", { value: true });
9 var chardet_1 = require("chardet");
10 var child_process_1 = require("child_process");
11 var fs_1 = require("fs");
12 var iconv_lite_1 = require("iconv-lite");
13 var tmp_1 = require("tmp");
14 var CreateFileError_1 = require("./errors/CreateFileError");
15 exports.CreateFileError = CreateFileError_1.CreateFileError;
16 var LaunchEditorError_1 = require("./errors/LaunchEditorError");
17 exports.LaunchEditorError = LaunchEditorError_1.LaunchEditorError;
18 var ReadFileError_1 = require("./errors/ReadFileError");
19 exports.ReadFileError = ReadFileError_1.ReadFileError;
20 var RemoveFileError_1 = require("./errors/RemoveFileError");
21 exports.RemoveFileError = RemoveFileError_1.RemoveFileError;
22 function edit(text, fileOptions) {
23 if (text === void 0) { text = ""; }
24 var editor = new ExternalEditor(text, fileOptions);
30 function editAsync(text, callback, fileOptions) {
31 if (text === void 0) { text = ""; }
32 var editor = new ExternalEditor(text, fileOptions);
33 editor.runAsync(function (err, result) {
35 setImmediate(callback, err, null);
40 setImmediate(callback, null, result);
42 catch (cleanupError) {
43 setImmediate(callback, cleanupError, null);
48 exports.editAsync = editAsync;
49 var ExternalEditor = /** @class */ (function () {
50 function ExternalEditor(text, fileOptions) {
51 if (text === void 0) { text = ""; }
53 this.fileOptions = {};
56 this.fileOptions = fileOptions;
58 this.determineEditor();
59 this.createTemporaryFile();
61 ExternalEditor.splitStringBySpace = function (str) {
63 var currentString = "";
64 for (var strIndex = 0; strIndex < str.length; strIndex++) {
65 var currentLetter = str[strIndex];
66 if (strIndex > 0 && currentLetter === " " && str[strIndex - 1] !== "\\" && currentString.length > 0) {
67 pieces.push(currentString);
71 currentString += currentLetter;
74 if (currentString.length > 0) {
75 pieces.push(currentString);
79 Object.defineProperty(ExternalEditor.prototype, "temp_file", {
81 console.log("DEPRECATED: temp_file. Use tempFile moving forward.");
87 Object.defineProperty(ExternalEditor.prototype, "last_exit_status", {
89 console.log("DEPRECATED: last_exit_status. Use lastExitStatus moving forward.");
90 return this.lastExitStatus;
95 ExternalEditor.prototype.run = function () {
97 this.readTemporaryFile();
100 ExternalEditor.prototype.runAsync = function (callback) {
103 this.launchEditorAsync(function () {
105 _this.readTemporaryFile();
106 setImmediate(callback, null, _this.text);
109 setImmediate(callback, readError, null);
113 catch (launchError) {
114 setImmediate(callback, launchError, null);
117 ExternalEditor.prototype.cleanup = function () {
118 this.removeTemporaryFile();
120 ExternalEditor.prototype.determineEditor = function () {
121 var editor = process.env.VISUAL ? process.env.VISUAL :
122 process.env.EDITOR ? process.env.EDITOR :
123 /^win/.test(process.platform) ? "notepad" :
125 var editorOpts = ExternalEditor.splitStringBySpace(editor).map(function (piece) { return piece.replace("\\ ", " "); });
126 var bin = editorOpts.shift();
127 this.editor = { args: editorOpts, bin: bin };
129 ExternalEditor.prototype.createTemporaryFile = function () {
131 this.tempFile = tmp_1.tmpNameSync(this.fileOptions);
132 var opt = { encoding: "utf8" };
133 if (this.fileOptions.hasOwnProperty("mode")) {
134 opt.mode = this.fileOptions.mode;
136 fs_1.writeFileSync(this.tempFile, this.text, opt);
138 catch (createFileError) {
139 throw new CreateFileError_1.CreateFileError(createFileError);
142 ExternalEditor.prototype.readTemporaryFile = function () {
144 var tempFileBuffer = fs_1.readFileSync(this.tempFile);
145 if (tempFileBuffer.length === 0) {
149 var encoding = chardet_1.detect(tempFileBuffer).toString();
150 if (!iconv_lite_1.encodingExists(encoding)) {
151 // Probably a bad idea, but will at least prevent crashing
154 this.text = iconv_lite_1.decode(tempFileBuffer, encoding);
157 catch (readFileError) {
158 throw new ReadFileError_1.ReadFileError(readFileError);
161 ExternalEditor.prototype.removeTemporaryFile = function () {
163 fs_1.unlinkSync(this.tempFile);
165 catch (removeFileError) {
166 throw new RemoveFileError_1.RemoveFileError(removeFileError);
169 ExternalEditor.prototype.launchEditor = function () {
171 var editorProcess = child_process_1.spawnSync(this.editor.bin, this.editor.args.concat([this.tempFile]), { stdio: "inherit" });
172 this.lastExitStatus = editorProcess.status;
174 catch (launchError) {
175 throw new LaunchEditorError_1.LaunchEditorError(launchError);
178 ExternalEditor.prototype.launchEditorAsync = function (callback) {
181 var editorProcess = child_process_1.spawn(this.editor.bin, this.editor.args.concat([this.tempFile]), { stdio: "inherit" });
182 editorProcess.on("exit", function (code) {
183 _this.lastExitStatus = code;
184 setImmediate(callback);
187 catch (launchError) {
188 throw new LaunchEditorError_1.LaunchEditorError(launchError);
191 return ExternalEditor;
193 exports.ExternalEditor = ExternalEditor;