add-change.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. var ChangeCreator = require('./change-creator').ChangeCreator;
  2. /**
  3. * The CLI class to add a changelog entry.
  4. */
  5. function AddChangeCli() {
  6. this._changeCreator = new ChangeCreator();
  7. this._maxRetries = 2;
  8. this._retryCount = 0;
  9. }
  10. AddChangeCli.prototype = {
  11. /**
  12. * Prints a string to stdout.
  13. * @param {string} message - text to print.
  14. */
  15. print: function print(message) {
  16. process.stdout.write(message);
  17. },
  18. /**
  19. * Prints the CLI intro message.
  20. */
  21. showIntro: function showIntro() {
  22. var intro = '\n';
  23. intro += 'This utility will walk you through creating a changelog entry.\n\n';
  24. intro += 'A changelog entry requires:\n';
  25. intro += '\t- type: Type should be one of: feature, bugfix.\n';
  26. intro += '\t- category: This can be a service identifier (e.g. "s3"), or something like: Paginator.\n';
  27. intro += '\t- description: A brief description of the change.\n';
  28. intro += '\t You can also include a github style reference such as "#111".\n\n'
  29. intro += 'Please run this script before submitting a pull request.\n\n';
  30. intro += 'Press ^C at any time to quit.\n';
  31. this.print(intro);
  32. },
  33. /**
  34. * Gets a string from stdin and returns a promise resolved with the string.
  35. * Note: stdin is read when the user presses 'Enter'.
  36. * Returns a promise that is resolved with the trimmed user input.
  37. */
  38. retrieveInputAsync: function retrieveInput() {
  39. return new Promise(function(resolve, reject) {
  40. function getData() {
  41. var chunk = process.stdin.read();
  42. if (chunk !== null) {
  43. // Remove self from stdin and call callback
  44. process.stdin.removeListener('readable', getData);
  45. resolve(chunk.trim());
  46. }
  47. }
  48. process.stdin.setEncoding('utf8');
  49. // start listening for input
  50. process.stdin.on('readable', getData);
  51. });
  52. },
  53. /**
  54. * Prompts the user to enter a type.
  55. * Will also process the user input.
  56. * Returns a promise.
  57. */
  58. promptType: function promptType() {
  59. var changeCreator = this._changeCreator;
  60. var existingType = changeCreator.getChangeType();
  61. this.print('\nValid types are "feature" or "bugfix"\n');
  62. this.print('type: ' + (existingType ? '(' + existingType + ') ' : ''));
  63. return this.retrieveInputAsync()
  64. .then(this.processType.bind(this));
  65. },
  66. /**
  67. * Prompts the user to enter a category.
  68. * Will also process the user input.
  69. * Returns a promise.
  70. */
  71. promptCategory: function promptCategory() {
  72. var changeCreator = this._changeCreator;
  73. var existingCategory = changeCreator.getChangeCategory();
  74. this.print('\nCategory can be a service identifier or something like: Paginator\n');
  75. this.print('category: ' + (existingCategory ? '(' + existingCategory + ') ' : ''));
  76. return this.retrieveInputAsync()
  77. .then(this.processCategory.bind(this));
  78. },
  79. /**
  80. * Prompts the user to enter a description.
  81. * Will also process the user input.
  82. * Returns a promise.
  83. */
  84. promptDescription: function promptDescription() {
  85. var changeCreator = this._changeCreator;
  86. var existingDescription = changeCreator.getChangeDescription();
  87. this.print('\nA brief description of your change.\n');
  88. this.print('description: ' + (existingDescription ? '(' + existingDescription + ') ' : ''));
  89. return this.retrieveInputAsync()
  90. .then(this.processDescription.bind(this));
  91. },
  92. /**
  93. * Handles processing of `type` based on user input.
  94. * If validation of `type` fails, the prompt will be shown again up to 3 times.
  95. * Returns a promise.
  96. */
  97. processType: function processType(type) {
  98. var changeCreator = this._changeCreator;
  99. var type = type.toLowerCase();
  100. // validate
  101. try {
  102. if (type) {
  103. changeCreator.setChangeType(type);
  104. }
  105. changeCreator.validateChangeType(type);
  106. } catch (err) {
  107. // Log the error
  108. this.print(err.message + '\n');
  109. // re-prompt if we still have retries
  110. if (this._retryCount < this._maxRetries) {
  111. this._retryCount++;
  112. return this.promptType();
  113. }
  114. //otherwise, just exit
  115. return Promise.reject();
  116. }
  117. // reset retry count
  118. this._retryCount = 0;
  119. return Promise.resolve();
  120. },
  121. /**
  122. * Handles processing of `category` based on user input.
  123. * If validation of `category` fails, the prompt will be shown again up to 3 times.
  124. * Returns a promise.
  125. */
  126. processCategory: function processCategory(category) {
  127. var changeCreator = this._changeCreator;
  128. // validate
  129. try {
  130. if (category) {
  131. changeCreator.setChangeCategory(category);
  132. }
  133. changeCreator.validateChangeCategory(category);
  134. } catch (err) {
  135. // Log the error
  136. this.print(err.message + '\n');
  137. // re-prompt if we still have retries
  138. if (this._retryCount < this._maxRetries) {
  139. this._retryCount++;
  140. return this.promptCategory();
  141. }
  142. //otherwise, just exit
  143. return Promise.reject();
  144. }
  145. // reset retry count
  146. this._retryCount = 0;
  147. return Promise.resolve();
  148. },
  149. /**
  150. * Handles processing of `description` based on user input.
  151. * If validation of `description` fails, the prompt will be shown again up to 3 times.
  152. * Returns a promise.
  153. */
  154. processDescription: function processDescription(description) {
  155. var changeCreator = this._changeCreator;
  156. // validate
  157. try {
  158. if (description) {
  159. changeCreator.setChangeDescription(description);
  160. }
  161. changeCreator.validateChangeDescription(description);
  162. } catch (err) {
  163. // Log the error
  164. this.print(err.message + '\n');
  165. // re-prompt if we still have retries
  166. if (this._retryCount < this._maxRetries) {
  167. this._retryCount++;
  168. return this.promptDescription();
  169. }
  170. //otherwise, just exit
  171. return Promise.reject();
  172. }
  173. // reset retry count
  174. this._retryCount = 0;
  175. return Promise.resolve();
  176. },
  177. /**
  178. * Prompts the user for all inputs.
  179. * Returns a promise.
  180. */
  181. promptInputs: function promptInputs() {
  182. var self = this;
  183. return this.promptType()
  184. .then(this.promptCategory.bind(this))
  185. .then(this.promptDescription.bind(this))
  186. .catch(function(err) {
  187. self.print(err.message);
  188. });
  189. },
  190. /**
  191. * Writes the changelog entry to a JSON file.
  192. * Returns a promise that is resolved with the output filename.
  193. */
  194. writeChangeEntry: function writeChangeEntry() {
  195. var self = this;
  196. return new Promise(function(resolve, reject) {
  197. var changeCreator = self._changeCreator;
  198. changeCreator.writeChanges(function(err, data) {
  199. if (err) {
  200. return reject(err);
  201. }
  202. self.print('\nFile created at ' + data.file + '\n');
  203. return resolve(data);
  204. });
  205. });
  206. }
  207. };
  208. // Run the CLI program
  209. var cli = new AddChangeCli();
  210. cli.showIntro();
  211. cli.promptInputs()
  212. .then(cli.writeChangeEntry.bind(cli))
  213. .then(function() {
  214. // CLI done with its work, exit successfully.
  215. setTimeout(function() {
  216. process.exit(0)
  217. }, 0);
  218. })
  219. .catch(function(err) {
  220. cli.print(err.message);
  221. cli.print('\nExiting...\n');
  222. setTimeout(function() {
  223. // CLI failed, exit with an error
  224. process.exit(1);
  225. }, 0);
  226. });