UNPKG

17.2 kBJavaScriptView Raw
1"use strict";
2var __assign = (this && this.__assign) || function () {
3 __assign = Object.assign || function(t) {
4 for (var s, i = 1, n = arguments.length; i < n; i++) {
5 s = arguments[i];
6 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7 t[p] = s[p];
8 }
9 return t;
10 };
11 return __assign.apply(this, arguments);
12};
13var __read = (this && this.__read) || function (o, n) {
14 var m = typeof Symbol === "function" && o[Symbol.iterator];
15 if (!m) return o;
16 var i = m.call(o), r, ar = [], e;
17 try {
18 while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
19 }
20 catch (error) { e = { error: error }; }
21 finally {
22 try {
23 if (r && !r.done && (m = i["return"])) m.call(i);
24 }
25 finally { if (e) throw e.error; }
26 }
27 return ar;
28};
29var __spread = (this && this.__spread) || function () {
30 for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
31 return ar;
32};
33var __importStar = (this && this.__importStar) || function (mod) {
34 if (mod && mod.__esModule) return mod;
35 var result = {};
36 if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
37 result["default"] = mod;
38 return result;
39};
40var __importDefault = (this && this.__importDefault) || function (mod) {
41 return (mod && mod.__esModule) ? mod : { "default": mod };
42};
43Object.defineProperty(exports, "__esModule", { value: true });
44var fs = __importStar(require("fs"));
45var path = __importStar(require("path"));
46var chalk_1 = __importDefault(require("chalk"));
47var command_line_args_1 = __importDefault(require("command-line-args"));
48var command_line_usage_1 = __importDefault(require("command-line-usage"));
49var signale_1 = __importDefault(require("signale"));
50var p = chalk_1.default.hex('#870048');
51var y = chalk_1.default.hex('#F1A60E');
52var r = chalk_1.default.hex('#C5000B');
53var g = chalk_1.default.hex('#888888');
54// prettier-ignore
55var logo = "\n " + y('_________') + "\n " + p('/') + y('\\ /') + r('\\') + " _______ _ _ _______ _____\n " + p('/') + " " + y('\\_____/') + " " + r('\\') + " |_____| | | | | |\n " + p('/ /') + " " + r('\\ \\') + " | | |_____| | |_____|\n " + p('/___/') + " \\\u2594\u2594\\ " + r(' \\___\\') + "\n " + g('\\ \\') + " \\_/ " + g('/ /') + " ______ _______ _______ _______ _______ _______\n " + g('\\ \\') + " " + g('/ /') + " |_____/ |______ | |______ |_____| |______ |______\n " + g('\\ ▔▔▔▔▔ /') + " | \\_ |______ |_____ |______ | | ______| |______\n " + g('\\ /') + "\n " + g('▔▔▔▔▔▔▔▔▔ ') + "\n";
56var help = {
57 name: 'help',
58 alias: 'h',
59 type: Boolean
60};
61var version = {
62 name: 'version',
63 alias: 'V',
64 type: Boolean,
65 description: "Display auto's version"
66};
67var mainDefinitions = [
68 { name: 'command', type: String, defaultOption: true },
69 __assign({}, help, { description: 'Display the help output. Works on each command as well' }),
70 version
71];
72var defaultOptions = [
73 __assign({}, help, { description: 'Display the help output for the command', group: 'misc' }),
74 {
75 name: 'verbose',
76 alias: 'v',
77 type: Boolean,
78 description: 'Show some more logs',
79 group: 'misc'
80 },
81 {
82 name: 'very-verbose',
83 alias: 'w',
84 type: Boolean,
85 description: 'Show a lot more logs',
86 group: 'misc'
87 },
88 {
89 name: 'repo',
90 type: String,
91 description: 'The repo to set the status on. Defaults to looking in the package definition for the platform',
92 group: 'misc'
93 },
94 {
95 name: 'owner',
96 type: String,
97 description: 'The owner of the GitHub repo. Defaults to reading from the package definition for the platform',
98 group: 'misc'
99 },
100 {
101 name: 'github-api',
102 type: String,
103 description: 'GitHub API to use',
104 group: 'misc'
105 },
106 {
107 name: 'plugins',
108 type: String,
109 multiple: true,
110 description: 'Plugins to load auto with. (defaults to just npm)',
111 group: 'misc'
112 }
113];
114var pr = {
115 name: 'pr',
116 type: Number,
117 description: 'The pull request number you want the labels of',
118 group: 'main'
119};
120var dryRun = {
121 name: 'dry-run',
122 alias: 'd',
123 type: Boolean,
124 description: 'Report what command will do but do not actually do anything',
125 group: 'main'
126};
127var url = {
128 name: 'url',
129 type: String,
130 description: 'URL to associate with this status',
131 group: 'main'
132};
133var noVersionPrefix = {
134 name: 'no-version-prefix',
135 type: Boolean,
136 description: "Use the version as the tag without the 'v' prefix",
137 group: 'main'
138};
139var jira = {
140 name: 'jira',
141 type: String,
142 description: 'Jira base URL',
143 group: 'main'
144};
145var name = {
146 name: 'name',
147 type: String,
148 description: 'Git name to commit and release with. Defaults to package definition for the platform',
149 group: 'main'
150};
151var email = {
152 name: 'email',
153 type: String,
154 description: 'Git email to commit with. Defaults to package definition for the platform',
155 group: 'main'
156};
157var context = {
158 name: 'context',
159 type: String,
160 description: 'A string label to differentiate this status from others',
161 group: 'main'
162};
163var message = {
164 name: 'message',
165 group: 'main',
166 type: String,
167 alias: 'm'
168};
169var skipReleaseLabels = {
170 name: 'skip-release-labels',
171 type: String,
172 group: 'main',
173 multiple: true,
174 description: "Labels that will not create a release. Defaults to just 'skip-release'"
175};
176var commands = [
177 {
178 name: 'init',
179 summary: 'Interactive setup for most configurable options',
180 examples: ['{green $} auto init'],
181 options: [
182 {
183 name: 'only-labels',
184 type: Boolean,
185 group: 'main',
186 description: 'Only run init for the labels. As most other options are for advanced users'
187 },
188 dryRun
189 ]
190 },
191 {
192 name: 'create-labels',
193 summary: "Create your project's labels on github. If labels exist it will update them.",
194 examples: ['{green $} auto create-labels'],
195 options: __spread(defaultOptions, [dryRun])
196 },
197 {
198 name: 'label',
199 summary: 'Get the labels for a pull request',
200 options: __spread([
201 __assign({}, pr, { description: pr.description + " (defaults to last merged PR)" })
202 ], defaultOptions),
203 examples: ['{green $} auto label --pr 123']
204 },
205 {
206 name: 'pr-check',
207 summary: 'Check that a pull request has a SemVer label',
208 require: ['pr', 'url'],
209 options: __spread([
210 pr,
211 url,
212 dryRun,
213 __assign({}, context, { defaultValue: 'ci/pr-check' }),
214 skipReleaseLabels
215 ], defaultOptions),
216 examples: [
217 '{green $} auto pr-check --pr 32 --url http://your-ci.com/build/123'
218 ]
219 },
220 {
221 name: 'pr',
222 summary: 'Set the status on a PR commit',
223 require: ['state', 'url', 'description', 'context'],
224 options: __spread([
225 {
226 name: 'sha',
227 type: String,
228 group: 'main',
229 description: 'Specify a custom git sha. Defaults to the HEAD for a git repo in the current repository'
230 },
231 __assign({}, pr, { description: 'PR to set the status on' }),
232 url,
233 {
234 name: 'state',
235 type: String,
236 group: 'main',
237 description: "State of the PR. ['pending', 'success', 'error', 'failure']"
238 },
239 {
240 name: 'description',
241 type: String,
242 group: 'main',
243 description: 'A description of the status'
244 },
245 {
246 name: 'context',
247 type: String,
248 group: 'main',
249 description: 'A string label to differentiate this status from others'
250 },
251 dryRun
252 ], defaultOptions),
253 examples: [
254 "{green $} auto pr \\\\ \n --pr 32 \\\\ \n --state pending \\\\ \n --description \"Build still running...\" \\\\ \n --context build-check"
255 ]
256 },
257 {
258 name: 'version',
259 summary: 'Get the semantic version bump for the given changes.',
260 options: __spread([
261 {
262 name: 'only-publish-with-release-label',
263 type: Boolean,
264 description: "Only bump version if 'release' label is on pull request",
265 group: 'main'
266 },
267 skipReleaseLabels
268 ], defaultOptions),
269 examples: [
270 {
271 desc: 'Get the new version using the last release to head',
272 example: '{green $} auto version'
273 },
274 {
275 desc: 'Skip releases with multiple labels',
276 example: '{green $} auto version --skip-release-labels documentation CI'
277 }
278 ]
279 },
280 {
281 name: 'changelog',
282 summary: "Prepend release notes to 'CHANGELOG.md'",
283 options: __spread([
284 dryRun,
285 noVersionPrefix,
286 name,
287 email,
288 jira,
289 {
290 name: 'from',
291 type: String,
292 group: 'main',
293 description: 'Tag to start changelog generation on. Defaults to latest tag.'
294 },
295 {
296 name: 'to',
297 type: String,
298 group: 'main',
299 description: 'Tag to end changelog generation on. Defaults to HEAD.'
300 },
301 __assign({}, message, { description: "Message to commit the changelog with. Defaults to 'Update CHANGELOG.md [skip ci]'" })
302 ], defaultOptions),
303 examples: [
304 {
305 desc: 'Generate a changelog from the last release to head',
306 example: '{green $} auto changelog'
307 },
308 {
309 desc: 'Generate a changelog across specific versions',
310 example: '{green $} auto changelog --from v0.20.1 --to v0.21.0'
311 }
312 ]
313 },
314 {
315 name: 'release',
316 summary: 'Auto-generate a github release',
317 options: __spread([
318 dryRun,
319 noVersionPrefix,
320 name,
321 email,
322 jira,
323 {
324 name: 'use-version',
325 type: String,
326 group: 'main',
327 description: 'Version number to publish as. Defaults to reading from the package definition for the platform.'
328 },
329 {
330 name: 'slack',
331 alias: 's',
332 type: String,
333 group: 'main',
334 description: 'Url to post a slack message to about the release. Make sure the SLACK_TOKEN environment variable is set.'
335 }
336 ], defaultOptions),
337 examples: ['{green $} auto release']
338 },
339 {
340 name: 'comment',
341 summary: 'Comment on a pull request with a markdown message',
342 require: ['pr', 'message'],
343 options: __spread([
344 pr,
345 context,
346 __assign({}, message, { description: 'Message to post to comment' }),
347 dryRun
348 ], defaultOptions),
349 examples: [
350 '{green $} auto comment --pr 123 --comment "# Why you\'re wrong..."'
351 ]
352 },
353 {
354 name: 'shipit',
355 summary: 'Run the full `auto` release pipeline. Detects if in a lerna project',
356 examples: ['{green $} auto shipit'],
357 options: __spread(defaultOptions, [dryRun])
358 }
359];
360function filterCommands(allCommands, include) {
361 return allCommands
362 .filter(function (command) { return include.includes(command.name); })
363 .map(function (command) { return ({
364 name: command.name,
365 summary: command.summary
366 }); });
367}
368function printRootHelp() {
369 var options = __spread([
370 __assign({}, version, { group: 'misc' })
371 ], mainDefinitions, defaultOptions);
372 options.forEach(function (option) {
373 styleTypes({}, option);
374 });
375 var usage = command_line_usage_1.default([
376 {
377 content: logo.replace(/\\/g, '\\\\'),
378 raw: true
379 },
380 {
381 content: 'Generate releases based on semantic version labels on pull requests'
382 },
383 {
384 header: 'Synopsis',
385 content: '$ auto <command> <options>'
386 },
387 {
388 header: 'Setup Commands',
389 content: filterCommands(commands, ['init', 'create-labels'])
390 },
391 {
392 header: 'Release Commands',
393 content: filterCommands(commands, [
394 'release',
395 'version',
396 'changelog',
397 'shipit'
398 ])
399 },
400 {
401 header: 'Pull Request Interaction Commands',
402 content: filterCommands(commands, ['label', 'pr-check', 'pr', 'comment'])
403 },
404 {
405 header: 'Global Options',
406 optionList: options,
407 group: 'misc'
408 }
409 ]);
410 console.log(usage);
411}
412function printCommandHelp(command) {
413 var sections = [
414 {
415 header: "auto " + command.name,
416 content: command.summary
417 }
418 ];
419 if (command.options) {
420 var hasLocalOptions = command.options.filter(function (option) { return option.group === 'main'; });
421 if (hasLocalOptions.length > 0) {
422 sections.push({
423 header: 'Options',
424 optionList: command.options,
425 group: 'main'
426 });
427 }
428 var hasGlobalOptions = command.options.filter(function (option) { return option.group === 'misc'; });
429 if (hasGlobalOptions.length > 0) {
430 sections.push({
431 header: 'Global Options',
432 optionList: command.options,
433 group: 'misc'
434 });
435 }
436 }
437 sections.push({
438 header: 'Examples',
439 content: command.examples,
440 raw: command.name === 'pr'
441 });
442 console.log(command_line_usage_1.default(sections));
443}
444function printVersion() {
445 var packagePath = path.join(__dirname, '../../package.json');
446 var packageJson = JSON.parse(fs.readFileSync(packagePath, 'utf8'));
447 console.log("v" + packageJson.version);
448}
449function styleTypes(command, option) {
450 var isRequired = command.require && command.require.includes(option.name);
451 if (isRequired && option.type === Number) {
452 option.typeLabel =
453 '{rgb(173, 216, 230) {underline number}} [{rgb(254,91,92) required}]';
454 }
455 else if (option.type === Number) {
456 option.typeLabel = '{rgb(173, 216, 230) {underline number}}';
457 }
458 if (isRequired && option.type === String) {
459 option.typeLabel =
460 '{rgb(173, 216, 230) {underline string}} [{rgb(254,91,92) required}]';
461 }
462 else if (option.multiple && option.type === String) {
463 option.typeLabel = '{rgb(173, 216, 230) {underline string[]}}';
464 }
465 else if (option.type === String) {
466 option.typeLabel = '{rgb(173, 216, 230) {underline string}}';
467 }
468}
469function parseArgs(testArgs) {
470 var mainOptions = command_line_args_1.default(mainDefinitions, {
471 stopAtFirstUnknown: true,
472 camelCase: true,
473 argv: testArgs
474 });
475 var argv = mainOptions._unknown || [];
476 var command = commands.find(function (c) { return c.name === mainOptions.command; });
477 if (!command && mainOptions.version) {
478 printVersion();
479 return;
480 }
481 if (!command) {
482 printRootHelp();
483 return;
484 }
485 var options = command.options || [];
486 options.forEach(function (option) {
487 styleTypes(command, option);
488 });
489 if (mainOptions.help) {
490 printCommandHelp(command);
491 return;
492 }
493 var autoOptions = __assign({ command: mainOptions.command }, command_line_args_1.default(options, { argv: argv, camelCase: true })._all);
494 if (command.require) {
495 var missing = command.require
496 .filter(function (option) {
497 return !autoOptions.hasOwnProperty(option) ||
498 // tslint:disable-next-line strict-type-predicates
499 autoOptions[option] === null;
500 })
501 .map(function (option) { return "--" + option; });
502 var multiple = missing.length > 1;
503 if (missing.length > 0) {
504 printCommandHelp(command);
505 signale_1.default.error("Missing required flag" + (multiple ? 's' : '') + ": " + missing.join(', '));
506 return process.exit(1);
507 }
508 }
509 return autoOptions;
510}
511exports.default = parseArgs;
512//# sourceMappingURL=args.js.map
\No newline at end of file