Skip to content

Commit

Permalink
git-node: add git-node-release
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere committed Feb 29, 2020
1 parent 4ea4dfa commit b41c616
Show file tree
Hide file tree
Showing 4 changed files with 540 additions and 0 deletions.
102 changes: 102 additions & 0 deletions components/git/release.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
'use strict';

const semver = require('semver');
const CLI = require('../../lib/cli');
const Release = require('../../lib/release');
const Request = require('../../lib/request');
const { runPromise } = require('../../lib/run');
const yargs = require('yargs');

const PREPARE = 'prepare';
const PROMOTE = 'promote';

const releaseOptions = {
prepare: {
describe: 'Prepare a new release with the given version number',
type: 'boolean'
},
security: {
describe: 'Prepare a new security release',
type: 'boolean'
}
};

function builder(yargs) {
return yargs
.options(releaseOptions).positional('newVersion', {
describe: 'Version number of the release to be created'
})
.example('git node release 1.2.3',
'Prepare a new release of Node.js tagged v1.2.3');
}

function handler(argv) {
if (argv.newVersion) {
const newVersion = semver.coerce(argv.newVersion);
if (semver.valid(newVersion)) {
return release(PREPARE, argv);
}
}

// If more than one action is provided or no valid action
// is provided, show help.
yargs.showHelp();
}

function release(state, argv) {
const logStream = process.stdout.isTTY ? process.stdout : process.stderr;
const cli = new CLI(logStream);

const req = new Request();
const dir = process.cwd();

return runPromise(main(state, argv, cli, req, dir)).catch((err) => {
if (cli.spinner.enabled) {
cli.spinner.fail();
}
throw err;
});
}

module.exports = {
command: 'release [newVersion|options]',
describe:
'Manage an in-progress release or start a new one.',
builder,
handler
};

async function main(state, argv, cli, req, dir) {
const release = new Release(state, argv, cli, req, dir);

// TODO(codebytere): check if this command is being run by
// someone on the Releasers team in GitHub before proceeding.

if (state === PREPARE) {
if (release.warnForWrongBranch()) return;

// Check the branch diff to determine if the releaser
// wants to backport any more commits before proceeding.
cli.startSpinner('Fetching branch-diff');
const raw = release.checkBranchDiff();
const diff = raw.split('*');
cli.stopSpinner('Got branch diff');

const staging = `v${semver.major(argv.newVersion)}.x-staging`;
const proceed = await cli.prompt(
`There are ${diff.length} commits that may be backported ` +
`to ${staging} - do you still want to proceed?`,
false);

if (!proceed) {
const seeDiff = await cli.prompt(
'Do you want to see the branch diff?', true);
if (seeDiff) console.log(raw);
return;
}

return release.prepare();
} else if (state === PROMOTE) {
return release.promote();
}
}
10 changes: 10 additions & 0 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ class CLI {
this.figureIndent = ' '.repeat(indent);
}

async promptInput(question, showSeparator = true) {
if (showSeparator) this.separator();
const { answer } = await inquirer.prompt([{
type: 'input',
name: 'answer',
message: question
}]);
return answer;
}

async prompt(question, defaultAnswer = true) {
this.separator();
if (this.assumeYes) {
Expand Down
Loading

0 comments on commit b41c616

Please sign in to comment.