Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bootstrap an automation tool #15699

Merged
merged 23 commits into from
May 24, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 124 additions & 0 deletions bin/commander.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#!/usr/bin/env node

/* eslint-disable no-console */

// Dependencies
const path = require( 'path' );
const program = require( 'commander' );
const inquirer = require( 'inquirer' );
const semver = require( 'semver' );
const chalk = require( 'chalk' );
const fs = require( 'fs' );
const SimpleGit = require( 'simple-git/promise' );

// Common info
const rootFolder = path.resolve( __dirname, '../' );
const packageJsonPath = rootFolder + '/package.json';
const packageLockPath = rootFolder + '/package-lock.json';
const pluginFilePath = rootFolder + '/gutenberg.php';
const packageJson = require( packageJsonPath );
const packageLock = require( packageLockPath );
const simpleGit = SimpleGit( rootFolder );
youknowriad marked this conversation as resolved.
Show resolved Hide resolved

// UI
const error = chalk.bold.red;
const warning = chalk.bold.keyword( 'orange' );
const success = chalk.bold.green;

program
.command( 'release-plugin-rc' )
.alias( 'rc' )
.description( 'Release an RC version of the plugin (supports only rc.1 for now)' )
.action( async () => {
youknowriad marked this conversation as resolved.
Show resolved Hide resolved
// Check the current branch and if the versions
const gitStatus = await simpleGit.status();
const parsedVersion = semver.parse( packageJson.version );
if ( gitStatus.current !== 'master' ) {
const { acceptBranch } = await inquirer.prompt( [ {
type: 'confirm',
name: 'acceptBranch',
message: 'Releasing plugin RC versions usually happens from the ' + warning( 'master' ) + ' branch. Do you want to continue from the current branch?',
default: false,
} ] );

if ( ! acceptBranch ) {
console.log( error( 'Aborting' ) );
process.exit( 1 );
}
}
if ( gitStatus.files.length ) {
const { acceptLocalChanges } = await inquirer.prompt( [ {
type: 'confirm',
name: 'acceptLocalChanges',
message: 'Your working tree is dirty. Do you want to continue? (some changes might be inadvertantly commited)',
default: false,
} ] );

if ( ! acceptLocalChanges ) {
console.log( error( 'Aborting' ) );
process.exit( 1 );
}
}

// Choosing the right version
let nextVersion;
let releaseBranch;
if ( parsedVersion.minor === 9 ) {
nextVersion = ( parsedVersion.major + 1 ) + '.0.0-rc.1';
releaseBranch = 'release/' + ( parsedVersion.major + 1 ) + '.0';
} else {
nextVersion = parsedVersion.major + '.' + ( parsedVersion.minor + 1 ) + '.0-rc.1';
releaseBranch = 'release/' + parsedVersion.major + '.' + ( parsedVersion.minor + 1 );
}
const { acceptVersion } = await inquirer.prompt( [ {
type: 'confirm',
name: 'acceptVersion',
message: 'The RC Version to be applied is ' + success( nextVersion ) + '. Proceed with the creation of the release branch?',
default: true,
} ] );
if ( ! acceptVersion ) {
console.log( error( 'Aborting' ) );
process.exit( 1 );
}

// Creating the release branch
await simpleGit.checkoutLocalBranch( releaseBranch );
console.log( '>> The local release branch "' + success( releaseBranch ) + '" has been successfully created.' );

// Bumping the version in the different files (package.json, package-lock.json, gutenberg.php)
const newPackageJson = {
...packageJson,
version: nextVersion,
};
await fs.writeFileSync( packageJsonPath, JSON.stringify( newPackageJson, null, '\t' ) + '\n' );
const newPackageLock = {
...packageLock,
version: nextVersion,
};
await fs.writeFileSync( packageLockPath, JSON.stringify( newPackageLock, null, '\t' ) + '\n' );
const content = await fs.readFileSync( pluginFilePath, 'utf8' );
await fs.writeFileSync( pluginFilePath, content.replace( ' * Version: ' + packageJson.version, ' * Version: ' + nextVersion ) );
console.log( '>> The plugin version has been updated successfully.' );

// Commit the version bump
const { acceptDiff } = await inquirer.prompt( [ {
type: 'confirm',
name: 'acceptDiff',
message: 'Please check the diff. Proceed with the version bump commit?',
default: true,
} ] );
if ( ! acceptDiff ) {
console.log( error( 'Aborting. Make sure to remove the local release branch' ) );
process.exit( 1 );
}
await simpleGit.add( [
packageJsonPath,
packageLockPath,
pluginFilePath,
] );
const { commit } = await simpleGit.commit( 'Bump plugin version to ' + nextVersion );

console.log( '>> The plugin version bump was commited succesfully. Please push the release branch to the repository and cherry-pick the ' + success( commit ) + ' commit to the master branch.' );
} );

program.parse( process.argv );
Loading