Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. "use strict";
  2. /***
  3. * Node External Editor
  4. *
  5. * Kevin Gravier <kevin@mrkmg.com>
  6. * MIT 2018
  7. */
  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) {
  23. if (text === void 0) { text = ""; }
  24. var editor = new ExternalEditor(text);
  25. editor.run();
  26. editor.cleanup();
  27. return editor.text;
  28. }
  29. exports.edit = edit;
  30. function editAsync(text, callback) {
  31. if (text === void 0) { text = ""; }
  32. var editor = new ExternalEditor(text);
  33. editor.runAsync(function (err, result) {
  34. if (err) {
  35. setImmediate(callback, err, null);
  36. }
  37. else {
  38. try {
  39. editor.cleanup();
  40. setImmediate(callback, null, result);
  41. }
  42. catch (cleanupError) {
  43. setImmediate(callback, cleanupError, null);
  44. }
  45. }
  46. });
  47. }
  48. exports.editAsync = editAsync;
  49. var ExternalEditor = /** @class */ (function () {
  50. function ExternalEditor(text) {
  51. if (text === void 0) { text = ""; }
  52. this.text = "";
  53. this.text = text;
  54. this.determineEditor();
  55. this.createTemporaryFile();
  56. }
  57. ExternalEditor.splitStringBySpace = function (str) {
  58. var pieces = [];
  59. var currentString = "";
  60. for (var strIndex = 0; strIndex < str.length; strIndex++) {
  61. var currentLetter = str[strIndex];
  62. if (strIndex > 0 && currentLetter === " " && str[strIndex - 1] !== "\\" && currentString.length > 0) {
  63. pieces.push(currentString);
  64. currentString = "";
  65. }
  66. else {
  67. currentString += currentLetter;
  68. }
  69. }
  70. if (currentString.length > 0) {
  71. pieces.push(currentString);
  72. }
  73. return pieces;
  74. };
  75. Object.defineProperty(ExternalEditor.prototype, "temp_file", {
  76. get: function () {
  77. console.log("DEPRECATED: temp_file. Use tempFile moving forward.");
  78. return this.tempFile;
  79. },
  80. enumerable: true,
  81. configurable: true
  82. });
  83. Object.defineProperty(ExternalEditor.prototype, "last_exit_status", {
  84. get: function () {
  85. console.log("DEPRECATED: last_exit_status. Use lastExitStatus moving forward.");
  86. return this.lastExitStatus;
  87. },
  88. enumerable: true,
  89. configurable: true
  90. });
  91. ExternalEditor.prototype.run = function () {
  92. this.launchEditor();
  93. this.readTemporaryFile();
  94. return this.text;
  95. };
  96. ExternalEditor.prototype.runAsync = function (callback) {
  97. var _this = this;
  98. try {
  99. this.launchEditorAsync(function () {
  100. try {
  101. _this.readTemporaryFile();
  102. setImmediate(callback, null, _this.text);
  103. }
  104. catch (readError) {
  105. setImmediate(callback, readError, null);
  106. }
  107. });
  108. }
  109. catch (launchError) {
  110. setImmediate(callback, launchError, null);
  111. }
  112. };
  113. ExternalEditor.prototype.cleanup = function () {
  114. this.removeTemporaryFile();
  115. };
  116. ExternalEditor.prototype.determineEditor = function () {
  117. var editor = process.env.VISUAL ? process.env.VISUAL :
  118. process.env.EDITOR ? process.env.EDITOR :
  119. /^win/.test(process.platform) ? "notepad" :
  120. "vim";
  121. var editorOpts = ExternalEditor.splitStringBySpace(editor).map(function (piece) { return piece.replace("\\ ", " "); });
  122. var bin = editorOpts.shift();
  123. this.editor = { args: editorOpts, bin: bin };
  124. };
  125. ExternalEditor.prototype.createTemporaryFile = function () {
  126. try {
  127. this.tempFile = tmp_1.tmpNameSync({});
  128. fs_1.writeFileSync(this.tempFile, this.text, { encoding: "utf8" });
  129. }
  130. catch (createFileError) {
  131. throw new CreateFileError_1.CreateFileError(createFileError);
  132. }
  133. };
  134. ExternalEditor.prototype.readTemporaryFile = function () {
  135. try {
  136. var tempFileBuffer = fs_1.readFileSync(this.tempFile);
  137. if (tempFileBuffer.length === 0) {
  138. this.text = "";
  139. }
  140. else {
  141. var encoding = chardet_1.detect(tempFileBuffer).toString();
  142. if (!iconv_lite_1.encodingExists(encoding)) {
  143. // Probably a bad idea, but will at least prevent crashing
  144. encoding = "utf8";
  145. }
  146. this.text = iconv_lite_1.decode(tempFileBuffer, encoding);
  147. }
  148. }
  149. catch (readFileError) {
  150. throw new ReadFileError_1.ReadFileError(readFileError);
  151. }
  152. };
  153. ExternalEditor.prototype.removeTemporaryFile = function () {
  154. try {
  155. fs_1.unlinkSync(this.tempFile);
  156. }
  157. catch (removeFileError) {
  158. throw new RemoveFileError_1.RemoveFileError(removeFileError);
  159. }
  160. };
  161. ExternalEditor.prototype.launchEditor = function () {
  162. try {
  163. var editorProcess = child_process_1.spawnSync(this.editor.bin, this.editor.args.concat([this.tempFile]), { stdio: "inherit" });
  164. this.lastExitStatus = editorProcess.status;
  165. }
  166. catch (launchError) {
  167. throw new LaunchEditorError_1.LaunchEditorError(launchError);
  168. }
  169. };
  170. ExternalEditor.prototype.launchEditorAsync = function (callback) {
  171. var _this = this;
  172. try {
  173. var editorProcess = child_process_1.spawn(this.editor.bin, this.editor.args.concat([this.tempFile]), { stdio: "inherit" });
  174. editorProcess.on("exit", function (code) {
  175. _this.lastExitStatus = code;
  176. setImmediate(callback);
  177. });
  178. }
  179. catch (launchError) {
  180. throw new LaunchEditorError_1.LaunchEditorError(launchError);
  181. }
  182. };
  183. return ExternalEditor;
  184. }());
  185. exports.ExternalEditor = ExternalEditor;