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