example_async.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. var ExternalEditor = require('./main');
  2. var readline = require('readline');
  3. var rl = readline.createInterface({
  4. input: process.stdin,
  5. output: null
  6. });
  7. var message = '\n\n# Please Write a message\n# Any line starting with # is ignored';
  8. process.stdout.write('Please write a message. (press enter to launch your preferred editor)');
  9. editor = new ExternalEditor(message);
  10. rl.on('line', function () {
  11. try {
  12. rl.pause();
  13. editor.runAsync(function (error, response)
  14. {
  15. if (error) {
  16. process.stdout.write(error.message);
  17. process.exit(1);
  18. }
  19. if (response.length === 0) {
  20. readline.moveCursor(process.stdout, 0, -1);
  21. process.stdout.write('Your message was empty, please try again. (press enter to launch your preferred editor)');
  22. rl.resume();
  23. } else {
  24. process.stdout.write('Your Message:\n');
  25. process.stdout.write(response);
  26. process.stdout.write('\n');
  27. rl.close();
  28. }
  29. });
  30. } catch (err) {
  31. process.stderr.write(err.message);
  32. process.stdout.write('\n');
  33. rl.close();
  34. }
  35. });