UNPKG

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