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.

README.md 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # External Editor
  2. [![ExternalEditor on Travis CI](https://img.shields.io/travis/mrkmg/node-external-editor.svg?style=flat-square)](https://travis-ci.org/mrkmg/node-external-editor/branches)
  3. [![ExternalEditor on NPM](https://img.shields.io/npm/v/external-editor.svg?style=flat-square)](https://www.npmjs.com/package/external-editor)
  4. [![ExternalEditor uses the MIT](https://img.shields.io/npm/l/external-editor.svg?style=flat-square)](https://opensource.org/licenses/MIT)
  5. A node module to edit a string with a users preferred text editor using $VISUAL or $ENVIRONMENT.
  6. Version: 3.0.3
  7. As of version 3.0.0, the minimum version of node supported is 4.
  8. ## Install
  9. `npm install external-editor --save`
  10. ## Usage
  11. A simple example using the `.edit` convenience method
  12. import {edit} from "external-editor";
  13. const data = edit('\n\n# Please write your text above');
  14. console.log(data);
  15. A full featured example
  16. import {ExternalEditor, CreateFileError, ReadFileError, RemoveFileError} from "external-editor"
  17. try {
  18. const editor = new ExternalEditor();
  19. const text = editor.run() // the text is also available in editor.text
  20. if (editor.last_exit_status !== 0) {
  21. console.log("The editor exited with a non-zero code");
  22. }
  23. } catch (err) {
  24. if (err instanceOf CreateFileError) {
  25. console.log('Failed to create the temporary file');
  26. } else if (err instanceOf ReadFileError) {
  27. console.log('Failed to read the temporary file');
  28. } else if (err instanceOf LaunchEditorError) {
  29. console.log('Failed to launch your editor');
  30. } else {
  31. throw err;
  32. }
  33. }
  34. // Do things with the text
  35. // Eventually call the cleanup to remove the temporary file
  36. try {
  37. editor.cleanup();
  38. } catch (err) {
  39. if (err instanceOf RemoveFileError) {
  40. console.log('Failed to remove the temporary file');
  41. } else {
  42. throw err
  43. }
  44. }
  45. #### API
  46. **Convenience Methods**
  47. - `edit(text)`
  48. - `text` (string) *Optional* Defaults to empty string
  49. - **Returns** (string) The contents of the file
  50. - Could throw `CreateFileError`, `ReadFileError`, or `LaunchEditorError`, or `RemoveFileError`
  51. - `editAsync(text, callback)`
  52. - `text` (string) *Optional* Defaults to empty string
  53. - `callback` (function (error, text))
  54. - `error` could be of type `CreateFileError`, `ReadFileError`, or `LaunchEditorError`, or `RemoveFileError`
  55. - `text`(string) The contents of the file
  56. **Errors**
  57. - `CreateFileError` Error thrown if the temporary file could not be created.
  58. - `ReadFileError` Error thrown if the temporary file could not be read.
  59. - `RemoveFileError` Error thrown if the temporary file could not be removed during cleanup.
  60. - `LaunchEditorError` Error thrown if the editor could not be launched.
  61. **External Editor Public Methods**
  62. - `new ExternalEditor(text)`
  63. - `text` (string) *Optional* Defaults to empty string
  64. - Could throw `CreateFileError`
  65. - `run()` Launches the editor.
  66. - **Returns** (string) The contents of the file
  67. - Could throw `LaunchEditorError` or `ReadFileError`
  68. - `runAsync(callback)` Launches the editor in an async way
  69. - `callback` (function (error, text))
  70. - `error` could be of type `ReadFileError` or `LaunchEditorError`
  71. - `text`(string) The contents of the file
  72. - `cleanup()` Removes the temporary file.
  73. - Could throw `RemoveFileError`
  74. **External Editor Public Properties**
  75. - `text` (string) *readonly* The text in the temporary file.
  76. - `editor.bin` (string) The editor determined from the environment.
  77. - `editor.args` (array) Default arguments for the bin
  78. - `tempFile` (string) Path to temporary file. Can be changed, but be careful as the temporary file probably already
  79. exists and would need be removed manually.
  80. - `lastExitStatus` (number) The last exit code emitted from the editor.
  81. ## Errors
  82. All errors have a simple message explaining what went wrong. They all also have an `originalError` property containing
  83. the original error thrown for debugging purposes.
  84. ## Why Synchronous?
  85. Everything is synchronous to make sure the editor has complete control of the stdin and stdout. Testing has shown
  86. async launching of the editor can lead to issues when using readline or other packages which try to read from stdin or
  87. write to stdout. Seeing as this will be used in an interactive CLI environment, I made the decision to force the package
  88. to be synchronous. If you know a reliable way to force all stdin and stdout to be limited only to the child_process,
  89. please submit a PR.
  90. If async is really needed, you can use `editAsync` or `runAsync`. If you are using readline or have anything else
  91. listening to the stdin or you write to stdout, you will most likely have problem, so make sure to remove any other
  92. listeners on stdin, stdout, or stderr.
  93. ## Demo
  94. [![asciicast](https://asciinema.org/a/a1qh9lypbe65mj0ivfuoslz2s.png)](https://asciinema.org/a/a1qh9lypbe65mj0ivfuoslz2s)
  95. ## Breaking Changes from v2 to v3
  96. - NodeJS 0.12 support dropped.
  97. - Switched to named imports.
  98. - All "snake_cased" variables and properties are now "camelCased".
  99. - `ExternalEditor.temp_file` is now `ExternalEditor.tempFile`.
  100. - `ExternalEditor.last_exit_status` is now `ExternalEditor.lastExitStatus`.
  101. - `Error.original_error` is now `Error.originalError`.
  102. ## License
  103. The MIT License (MIT)
  104. Copyright (c) 2016-2018 Kevin Gravier
  105. Permission is hereby granted, free of charge, to any person obtaining a copy
  106. of this software and associated documentation files (the "Software"), to deal
  107. in the Software without restriction, including without limitation the rights
  108. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  109. copies of the Software, and to permit persons to whom the Software is
  110. furnished to do so, subject to the following conditions:
  111. The above copyright notice and this permission notice shall be included in all
  112. copies or substantial portions of the Software.
  113. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  114. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  115. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  116. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  117. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  118. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  119. SOFTWARE.