tis-cli前端项目快速搭建命令行工具
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.

171 lines
7.1 KiB

4 years ago
  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.1.0
  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, config)`
  48. - `text` (string) *Optional* Defaults to empty string
  49. - `config` (Config) *Optional* Options for temporary file creation
  50. - **Returns** (string) The contents of the file
  51. - Could throw `CreateFileError`, `ReadFileError`, or `LaunchEditorError`, or `RemoveFileError`
  52. - `editAsync(text, callback, config)`
  53. - `text` (string) *Optional* Defaults to empty string
  54. - `callback` (function (error, text))
  55. - `error` could be of type `CreateFileError`, `ReadFileError`, or `LaunchEditorError`, or `RemoveFileError`
  56. - `text`(string) The contents of the file
  57. - `config` (Config) *Optional* Options for temporary file creation
  58. **Errors**
  59. - `CreateFileError` Error thrown if the temporary file could not be created.
  60. - `ReadFileError` Error thrown if the temporary file could not be read.
  61. - `RemoveFileError` Error thrown if the temporary file could not be removed during cleanup.
  62. - `LaunchEditorError` Error thrown if the editor could not be launched.
  63. **External Editor Public Methods**
  64. - `new ExternalEditor(text, config)`
  65. - `text` (string) *Optional* Defaults to empty string
  66. - `config` (Config) *Optional* Options for temporary file creation
  67. - Could throw `CreateFileError`
  68. - `run()` Launches the editor.
  69. - **Returns** (string) The contents of the file
  70. - Could throw `LaunchEditorError` or `ReadFileError`
  71. - `runAsync(callback)` Launches the editor in an async way
  72. - `callback` (function (error, text))
  73. - `error` could be of type `ReadFileError` or `LaunchEditorError`
  74. - `text`(string) The contents of the file
  75. - `cleanup()` Removes the temporary file.
  76. - Could throw `RemoveFileError`
  77. **External Editor Public Properties**
  78. - `text` (string) *readonly* The text in the temporary file.
  79. - `editor.bin` (string) The editor determined from the environment.
  80. - `editor.args` (array) Default arguments for the bin
  81. - `tempFile` (string) Path to temporary file. Can be changed, but be careful as the temporary file probably already
  82. exists and would need be removed manually.
  83. - `lastExitStatus` (number) The last exit code emitted from the editor.
  84. **Config Options**
  85. - `prefix` (string) *Optional* A prefix for the file name.
  86. - `postfix` (string; *Optional* A postfix for the file name. Useful if you want to provide an extension.
  87. - `mode` (number) *Optional* Which mode to create the file with. e.g. 644
  88. - `template` (string) *Optional* A template for the filename. See [tmp](https://www.npmjs.com/package/tmp).
  89. - `dir` (string) *Optional* Which path to store the file.
  90. ## Errors
  91. All errors have a simple message explaining what went wrong. They all also have an `originalError` property containing
  92. the original error thrown for debugging purposes.
  93. ## Why Synchronous?
  94. Everything is synchronous to make sure the editor has complete control of the stdin and stdout. Testing has shown
  95. async launching of the editor can lead to issues when using readline or other packages which try to read from stdin or
  96. write to stdout. Seeing as this will be used in an interactive CLI environment, I made the decision to force the package
  97. to be synchronous. If you know a reliable way to force all stdin and stdout to be limited only to the child_process,
  98. please submit a PR.
  99. If async is really needed, you can use `editAsync` or `runAsync`. If you are using readline or have anything else
  100. listening to the stdin or you write to stdout, you will most likely have problem, so make sure to remove any other
  101. listeners on stdin, stdout, or stderr.
  102. ## Demo
  103. [![asciicast](https://asciinema.org/a/a1qh9lypbe65mj0ivfuoslz2s.png)](https://asciinema.org/a/a1qh9lypbe65mj0ivfuoslz2s)
  104. ## Breaking Changes from v2 to v3
  105. - NodeJS 0.12 support dropped.
  106. - Switched to named imports.
  107. - All "snake_cased" variables and properties are now "camelCased".
  108. - `ExternalEditor.temp_file` is now `ExternalEditor.tempFile`.
  109. - `ExternalEditor.last_exit_status` is now `ExternalEditor.lastExitStatus`.
  110. - `Error.original_error` is now `Error.originalError`.
  111. ## License
  112. The MIT License (MIT)
  113. Copyright (c) 2016-2018 Kevin Gravier
  114. Permission is hereby granted, free of charge, to any person obtaining a copy
  115. of this software and associated documentation files (the "Software"), to deal
  116. in the Software without restriction, including without limitation the rights
  117. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  118. copies of the Software, and to permit persons to whom the Software is
  119. furnished to do so, subject to the following conditions:
  120. The above copyright notice and this permission notice shall be included in all
  121. copies or substantial portions of the Software.
  122. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  123. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  124. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  125. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  126. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  127. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  128. SOFTWARE.