editor.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * `editor` type prompt
  3. */
  4. import chalk from 'chalk';
  5. import { editAsync } from 'external-editor';
  6. import Base from './base.js';
  7. import observe from '../utils/events.js';
  8. import { Subject } from 'rxjs';
  9. export default class EditorPrompt extends Base {
  10. /**
  11. * Start the Inquiry session
  12. * @param {Function} cb Callback when prompt is done
  13. * @return {this}
  14. */
  15. _run(cb) {
  16. this.done = cb;
  17. this.editorResult = new Subject();
  18. // Open Editor on "line" (Enter Key)
  19. const events = observe(this.rl);
  20. this.lineSubscription = events.line.subscribe(this.startExternalEditor.bind(this));
  21. const waitUserInput =
  22. this.opt.waitUserInput === undefined ? true : this.opt.waitUserInput;
  23. if (!waitUserInput) {
  24. this.startExternalEditor();
  25. }
  26. // Trigger Validation when editor closes
  27. const validation = this.handleSubmitEvents(this.editorResult);
  28. validation.success.forEach(this.onEnd.bind(this));
  29. validation.error.forEach(this.onError.bind(this));
  30. // Prevents default from being printed on screen (can look weird with multiple lines)
  31. this.currentText = this.opt.default;
  32. this.opt.default = null;
  33. // Init
  34. this.render();
  35. return this;
  36. }
  37. /**
  38. * Render the prompt to screen
  39. * @return {EditorPrompt} self
  40. */
  41. render(error) {
  42. let bottomContent = '';
  43. let message = this.getQuestion();
  44. if (this.status === 'answered') {
  45. message += chalk.dim('Received');
  46. } else {
  47. message += chalk.dim('Press <enter> to launch your preferred editor.');
  48. }
  49. if (error) {
  50. bottomContent = chalk.red('>> ') + error;
  51. }
  52. this.screen.render(message, bottomContent);
  53. }
  54. /**
  55. * Launch $EDITOR on user press enter
  56. */
  57. startExternalEditor() {
  58. // Pause Readline to prevent stdin and stdout from being modified while the editor is showing
  59. this.rl.pause();
  60. editAsync(this.currentText, this.endExternalEditor.bind(this));
  61. }
  62. endExternalEditor(error, result) {
  63. this.rl.resume();
  64. if (error) {
  65. this.editorResult.error(error);
  66. } else {
  67. this.editorResult.next(result);
  68. }
  69. }
  70. onEnd(state) {
  71. this.editorResult.unsubscribe();
  72. this.lineSubscription.unsubscribe();
  73. this.answer = state.value;
  74. this.status = 'answered';
  75. // Re-render prompt
  76. this.render();
  77. this.screen.done();
  78. this.done(this.answer);
  79. }
  80. onError(state) {
  81. this.render(state.isValid);
  82. }
  83. }