index.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. 'use strict';
  2. var read = require('read');
  3. var promptly = module.exports;
  4. promptly.prompt = function (message, opts, fn) {
  5. // Arguments parsing
  6. if (typeof opts === 'function') {
  7. fn = opts;
  8. opts = {};
  9. } else if (!opts) {
  10. opts = {};
  11. }
  12. if (opts.trim === undefined) {
  13. opts.trim = true;
  14. }
  15. if (opts.retry === undefined) {
  16. opts.retry = true;
  17. }
  18. // Setup read's options
  19. var readOpts = {
  20. prompt: message,
  21. input: opts.input || process.stdin,
  22. output: opts.output || process.stdout,
  23. silent: opts.silent
  24. };
  25. // Use readline question
  26. read(readOpts, function (err, data) {
  27. // Ignore the error attribute
  28. // It is set on SIGINT or if timeout reached (we are not using timeout)
  29. if (err) {
  30. return;
  31. }
  32. // Trim?
  33. if (opts.trim) {
  34. data = data.trim();
  35. }
  36. // Mandatory?
  37. if (opts['default'] == null && !data) {
  38. return promptly.prompt(message, opts, fn);
  39. } else {
  40. data = data || opts['default'];
  41. }
  42. // Validator verification
  43. if (opts.validator) {
  44. if (!Array.isArray(opts.validator)) {
  45. opts.validator = [opts.validator];
  46. }
  47. var x;
  48. var length = opts.validator.length;
  49. for (x = 0; x < length; x += 1) {
  50. try {
  51. data = opts.validator[x](data);
  52. } catch (e) {
  53. // Retry automatically if the retry option is enabled
  54. if (opts.retry) {
  55. if (e.message) {
  56. readOpts.output.write(e.message + '\n');
  57. }
  58. return promptly.prompt(message, opts, fn);
  59. }
  60. e.retry = promptly.prompt.bind(promptly, message, opts, fn);
  61. return fn(e);
  62. }
  63. }
  64. }
  65. // Everything ok
  66. fn(null, data);
  67. });
  68. };
  69. promptly.password = function (message, opts, fn) {
  70. // Arguments parsing
  71. if (typeof opts === 'function') {
  72. fn = opts;
  73. opts = {};
  74. } else {
  75. opts = opts || {};
  76. }
  77. // Set default options
  78. if (opts.silent === undefined) {
  79. opts.silent = true;
  80. }
  81. if (opts.trim === undefined) {
  82. opts.trim = false;
  83. }
  84. if (opts['default'] === undefined) {
  85. opts['default'] = '';
  86. }
  87. // Use prompt()
  88. promptly.prompt(message, opts, fn);
  89. };
  90. promptly.confirm = function (message, opts, fn) {
  91. // Arguments parsing
  92. if (typeof opts === 'function') {
  93. fn = opts;
  94. opts = {};
  95. } else if (!opts) {
  96. opts = {};
  97. }
  98. opts.validator = opts.validator || [];
  99. if (!Array.isArray(opts.validator)) {
  100. opts.validator = [opts.validator];
  101. }
  102. // Push the validator that will coerse boolean values
  103. var validator = function (value) {
  104. if (typeof value === 'string') {
  105. value = value.toLowerCase();
  106. }
  107. switch (value) {
  108. case 'y':
  109. case 'yes':
  110. case '1':
  111. case true:
  112. return true;
  113. case 'n':
  114. case 'no':
  115. case '0':
  116. case false:
  117. return false;
  118. }
  119. throw new Error();
  120. };
  121. opts.validator.push(validator);
  122. // Use choose() with true, false
  123. promptly.choose(message, [true, false], opts, fn);
  124. };
  125. promptly.choose = function (message, choices, opts, fn) {
  126. // Arguments parsing
  127. if (typeof opts === 'function') {
  128. fn = opts;
  129. opts = {};
  130. } else if (!opts) {
  131. opts = {};
  132. }
  133. opts.validator = opts.validator || [];
  134. if (!Array.isArray(opts.validator)) {
  135. opts.validator = [opts.validator];
  136. }
  137. // Push the choice validator
  138. var nrChoices = choices.length;
  139. var validator = function (value) {
  140. var x;
  141. for (x = 0; x < nrChoices; x++) {
  142. if (choices[x] == value) {
  143. return choices[x];
  144. }
  145. }
  146. throw new Error('Invalid choice: ' + value);
  147. };
  148. opts.validator.push(validator);
  149. // Use prompt()
  150. promptly.prompt(message, opts, fn);
  151. };