index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. var inq = require('inquirer'),
  2. es = require('event-stream'),
  3. template = require("lodash.template");
  4. module.exports = {
  5. /**
  6. * The following method will act as a proxy to the inquirer
  7. * prompt function. It will pass the questions object directly to
  8. * inqurier's prompt function
  9. * @param {*} questions
  10. * @param {*} callback
  11. */
  12. prompt: function(questions, callback) {
  13. var prompted = false;
  14. return es.map(function(file, cb) {
  15. /**
  16. * The following chainHandler was designed to be called recursively so as to allow
  17. * users the ability to chain multple calls to inquirer together. It will stop
  18. * calls to the chain handler when the chain function returns undefined.
  19. * @param {objects} options
  20. */
  21. var chainHandler = function( options ){
  22. return new Promise( (resolve,reject)=>{
  23. inq.prompt([options]).then( resp =>{
  24. let opts = chainFunction( options, resp );
  25. if( typeof opts === 'undefined'){
  26. return resolve('response');
  27. }else{
  28. chainHandler( opts );
  29. }
  30. }).catch( err =>{
  31. reject( 'Unexpected Error');
  32. });
  33. });
  34. }
  35. if (prompted === true) {
  36. cb(null,file);
  37. return;
  38. }
  39. if (!questions instanceof Array) {
  40. questions = [questions];
  41. }
  42. if (typeof callback !== 'function') {
  43. callback = function(){};
  44. }
  45. if( typeof questions.chainFunction === 'undefined' ){
  46. inq.prompt(questions).then(function(res) {
  47. callback(res);
  48. cb(null, file);
  49. });
  50. prompted = true;
  51. }else{
  52. chainFunction = questions.chainFunction;
  53. return chainHandler( questions ).then(function(res) {
  54. if (res.val) {
  55. cb(null, file);
  56. }
  57. }).catch( err => {
  58. cb(null, file);
  59. });
  60. prompted = true;
  61. }
  62. });
  63. },
  64. confirm: function(options) {
  65. var prompted = false;
  66. var chainFunction;
  67. return es.map(function(file, cb) {
  68. if (prompted === true) {
  69. cb(null,file);
  70. return;
  71. }
  72. /**
  73. * The following chainHandler was designed to be called recursively so as to allow
  74. * users the ability to chain multple calls to inquirer together. It will stop
  75. * calls to the chain handler when the chain function returns undefined.
  76. * @param {objects} options
  77. */
  78. var chainHandler = function( options ){
  79. return new Promise( (resolve,reject)=>{
  80. inq.prompt([options]).then( resp =>{
  81. var opts = chainFunction( options, resp );
  82. if( typeof opts === 'undefined'){
  83. return resolve('response');
  84. }else{
  85. chainHandler( opts );
  86. }
  87. }).catch( err =>{
  88. reject( 'Unexpected Error');
  89. });
  90. });
  91. }
  92. var opts = {
  93. type: 'confirm',
  94. name: 'val',
  95. message: 'Are you sure?',
  96. default: false
  97. };
  98. if (typeof options === 'string') {
  99. opts.message = options;
  100. }
  101. if (typeof options !== 'object') {
  102. options = {};
  103. }
  104. if( typeof options.templateOptions !== 'undefined'){
  105. var compiled = template( options.message );
  106. options.message = compiled( options.templateOptions);
  107. }
  108. opts.message = options.message || opts.message;
  109. opts.default = options.default || opts.default;
  110. if( typeof options.chainFunction === 'undefined'){
  111. inq.prompt([opts]).then(function(res) {
  112. if (res.val) {
  113. cb(null, file);
  114. }
  115. });
  116. prompted = true;
  117. }else{
  118. chainFunction = options.chainFunction;
  119. return chainHandler( opts ).then(function(res) {
  120. if (res.val) {
  121. cb(null, file);
  122. }
  123. }).catch( err => {
  124. cb(null, file);
  125. });
  126. prompted = true;
  127. }
  128. });
  129. },
  130. inq: inq
  131. };