|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const semver = require('semver'); |
| 4 | +const CLI = require('../../lib/cli'); |
| 5 | +const Release = require('../../lib/release'); |
| 6 | +const Request = require('../../lib/request'); |
| 7 | +const { runPromise } = require('../../lib/run'); |
| 8 | +const yargs = require('yargs'); |
| 9 | + |
| 10 | +const PREPARE = 'prepare'; |
| 11 | +const PROMOTE = 'promote'; |
| 12 | + |
| 13 | +const releaseOptions = { |
| 14 | + prepare: { |
| 15 | + describe: 'Prepare a new release with the given version number', |
| 16 | + type: 'boolean' |
| 17 | + }, |
| 18 | + security: { |
| 19 | + describe: 'Prepare a new security release', |
| 20 | + type: 'boolean' |
| 21 | + } |
| 22 | +}; |
| 23 | + |
| 24 | +function builder(yargs) { |
| 25 | + return yargs |
| 26 | + .options(releaseOptions).positional('newVersion', { |
| 27 | + describe: 'Version number of the release to be created' |
| 28 | + }) |
| 29 | + .example('git node release 1.2.3', |
| 30 | + 'Prepare a new release of Node.js tagged v1.2.3'); |
| 31 | +} |
| 32 | + |
| 33 | +function handler(argv) { |
| 34 | + if (argv.newVersion) { |
| 35 | + const newVersion = semver.coerce(argv.newVersion); |
| 36 | + if (semver.valid(newVersion)) { |
| 37 | + return release(PREPARE, argv); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + // If more than one action is provided or no valid action |
| 42 | + // is provided, show help. |
| 43 | + yargs.showHelp(); |
| 44 | +} |
| 45 | + |
| 46 | +function release(state, argv) { |
| 47 | + const logStream = process.stdout.isTTY ? process.stdout : process.stderr; |
| 48 | + const cli = new CLI(logStream); |
| 49 | + |
| 50 | + const req = new Request(); |
| 51 | + const dir = process.cwd(); |
| 52 | + |
| 53 | + return runPromise(main(state, argv, cli, req, dir)).catch((err) => { |
| 54 | + if (cli.spinner.enabled) { |
| 55 | + cli.spinner.fail(); |
| 56 | + } |
| 57 | + throw err; |
| 58 | + }); |
| 59 | +} |
| 60 | + |
| 61 | +module.exports = { |
| 62 | + command: 'release [newVersion|options]', |
| 63 | + describe: |
| 64 | + 'Manage an in-progress release or start a new one.', |
| 65 | + builder, |
| 66 | + handler |
| 67 | +}; |
| 68 | + |
| 69 | +async function main(state, argv, cli, req, dir) { |
| 70 | + const release = new Release(state, argv, cli, req, dir); |
| 71 | + |
| 72 | + if (state === PREPARE) { |
| 73 | + if (release.warnForWrongBranch()) return; |
| 74 | + |
| 75 | + // Check the branch diff to determine if the releaser |
| 76 | + // wants to backport any more commits before proceeding. |
| 77 | + cli.startSpinner('Fetching branch-diff'); |
| 78 | + const raw = release.checkBranchDiff(); |
| 79 | + const diff = raw.split('*'); |
| 80 | + cli.stopSpinner('Got branch diff'); |
| 81 | + |
| 82 | + const staging = `v${semver.major(argv.newVersion)}.x-staging`; |
| 83 | + const proceed = await cli.prompt( |
| 84 | + `There are ${diff.length} commits that may be backported ` + |
| 85 | + `to ${staging} - do you still want to proceed?`, |
| 86 | + false); |
| 87 | + |
| 88 | + if (!proceed) { |
| 89 | + const seeDiff = await cli.prompt( |
| 90 | + 'Do you want to see the branch diff?', true); |
| 91 | + if (seeDiff) console.log(raw); |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + return release.prepare(); |
| 96 | + } else if (state === PROMOTE) { |
| 97 | + return release.promote(); |
| 98 | + } |
| 99 | +} |
0 commit comments