2019-02-21 03:58:28 -06:00
|
|
|
import execa from 'execa';
|
|
|
|
import { execTask } from '../utils/execTask';
|
2019-05-22 07:10:13 -05:00
|
|
|
import { changeCwdToGrafanaUiDist, changeCwdToGrafanaUi, restoreCwd } from '../utils/cwd';
|
2019-02-21 03:58:28 -06:00
|
|
|
import semver from 'semver';
|
|
|
|
import inquirer from 'inquirer';
|
|
|
|
import chalk from 'chalk';
|
2019-03-05 01:56:29 -06:00
|
|
|
import { useSpinner } from '../utils/useSpinner';
|
2019-05-22 07:10:13 -05:00
|
|
|
import { savePackage, buildTask, clean } from './grafanaui.build';
|
2019-03-05 01:56:29 -06:00
|
|
|
import { TaskRunner, Task } from './task';
|
2019-02-21 03:58:28 -06:00
|
|
|
|
2019-03-05 01:56:29 -06:00
|
|
|
type VersionBumpType = 'prerelease' | 'patch' | 'minor' | 'major';
|
|
|
|
interface ReleaseTaskOptions {
|
|
|
|
publishToNpm: boolean;
|
2019-03-06 02:52:31 -06:00
|
|
|
usePackageJsonVersion: boolean;
|
2019-03-08 07:42:19 -06:00
|
|
|
createVersionCommit: boolean;
|
2019-03-05 01:56:29 -06:00
|
|
|
}
|
2019-02-21 03:58:28 -06:00
|
|
|
|
|
|
|
const promptBumpType = async () => {
|
|
|
|
return inquirer.prompt<{ type: VersionBumpType }>([
|
|
|
|
{
|
|
|
|
type: 'list',
|
|
|
|
message: 'Select version bump',
|
|
|
|
name: 'type',
|
2019-03-05 01:56:29 -06:00
|
|
|
choices: ['prerelease', 'patch', 'minor', 'major'],
|
2019-02-21 03:58:28 -06:00
|
|
|
validate: answer => {
|
|
|
|
if (answer.length < 1) {
|
|
|
|
return 'You must choose something';
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
};
|
|
|
|
|
2019-03-05 01:56:29 -06:00
|
|
|
const promptPrereleaseId = async (message = 'Is this a prerelease?', allowNo = true) => {
|
2019-02-21 03:58:28 -06:00
|
|
|
return inquirer.prompt<{ id: string }>([
|
|
|
|
{
|
|
|
|
type: 'list',
|
2019-03-05 01:56:29 -06:00
|
|
|
message: message,
|
2019-02-21 03:58:28 -06:00
|
|
|
name: 'id',
|
2019-03-05 01:56:29 -06:00
|
|
|
choices: allowNo ? ['no', 'alpha', 'beta'] : ['alpha', 'beta'],
|
2019-02-21 03:58:28 -06:00
|
|
|
validate: answer => {
|
|
|
|
if (answer.length < 1) {
|
|
|
|
return 'You must choose something';
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
};
|
|
|
|
|
|
|
|
const promptConfirm = async (message?: string) => {
|
|
|
|
return inquirer.prompt<{ confirmed: boolean }>([
|
|
|
|
{
|
|
|
|
type: 'confirm',
|
|
|
|
message: message || 'Is that correct?',
|
|
|
|
name: 'confirmed',
|
|
|
|
default: false,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
};
|
|
|
|
|
2019-03-08 07:42:19 -06:00
|
|
|
// Since Grafana core depends on @grafana/ui highly, we run full check before release
|
|
|
|
const runChecksAndTests = async () =>
|
|
|
|
useSpinner<void>(`Running checks and tests`, async () => {
|
|
|
|
await execa('npm', ['run', 'test']);
|
|
|
|
})();
|
|
|
|
|
2019-03-05 01:56:29 -06:00
|
|
|
const bumpVersion = (version: string) =>
|
|
|
|
useSpinner<void>(`Saving version ${version} to package.json`, async () => {
|
|
|
|
changeCwdToGrafanaUi();
|
2019-02-21 03:58:28 -06:00
|
|
|
await execa('npm', ['version', version]);
|
2019-03-05 01:56:29 -06:00
|
|
|
changeCwdToGrafanaUiDist();
|
|
|
|
const pkg = require(`${process.cwd()}/package.json`);
|
|
|
|
pkg.version = version;
|
|
|
|
await savePackage({ path: `${process.cwd()}/package.json`, pkg });
|
|
|
|
})();
|
|
|
|
|
|
|
|
const publishPackage = (name: string, version: string) =>
|
|
|
|
useSpinner<void>(`Publishing ${name} @ ${version} to npm registry...`, async () => {
|
|
|
|
changeCwdToGrafanaUiDist();
|
2019-02-21 08:56:57 -06:00
|
|
|
await execa('npm', ['publish', '--access', 'public']);
|
2019-03-05 01:56:29 -06:00
|
|
|
})();
|
|
|
|
|
2019-03-05 14:12:21 -06:00
|
|
|
const ensureMasterBranch = async () => {
|
|
|
|
const currentBranch = await execa.stdout('git', ['symbolic-ref', '--short', 'HEAD']);
|
|
|
|
const status = await execa.stdout('git', ['status', '--porcelain']);
|
|
|
|
|
|
|
|
if (currentBranch !== 'master' && status !== '') {
|
|
|
|
console.error(chalk.red.bold('You need to be on clean master branch to release @grafana/ui'));
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-03-08 07:42:19 -06:00
|
|
|
const prepareVersionCommitAndPush = async (version: string) =>
|
|
|
|
useSpinner<void>('Commiting and pushing @grafana/ui version update', async () => {
|
|
|
|
await execa.stdout('git', ['commit', '-a', '-m', `Upgrade @grafana/ui version to v${version}`]);
|
|
|
|
await execa.stdout('git', ['push']);
|
|
|
|
})();
|
|
|
|
|
|
|
|
const releaseTaskRunner: TaskRunner<ReleaseTaskOptions> = async ({
|
|
|
|
publishToNpm,
|
|
|
|
usePackageJsonVersion,
|
|
|
|
createVersionCommit,
|
|
|
|
}) => {
|
2019-05-22 07:10:13 -05:00
|
|
|
changeCwdToGrafanaUi();
|
|
|
|
await clean(); // Clean previous build if exists
|
|
|
|
restoreCwd();
|
|
|
|
|
2019-03-05 14:18:26 -06:00
|
|
|
if (publishToNpm) {
|
2019-03-08 07:42:19 -06:00
|
|
|
// TODO: Ensure release branch
|
|
|
|
// When need to update this when we star keeping @grafana/ui releases in sync with core
|
2019-03-05 14:18:26 -06:00
|
|
|
await ensureMasterBranch();
|
|
|
|
}
|
|
|
|
|
2019-05-22 07:10:13 -05:00
|
|
|
runChecksAndTests();
|
|
|
|
|
2019-03-05 01:56:29 -06:00
|
|
|
await execTask(buildTask)();
|
2019-02-21 03:58:28 -06:00
|
|
|
|
|
|
|
let releaseConfirmed = false;
|
|
|
|
let nextVersion;
|
|
|
|
changeCwdToGrafanaUiDist();
|
|
|
|
|
|
|
|
const pkg = require(`${process.cwd()}/package.json`);
|
|
|
|
|
|
|
|
console.log(`Current version: ${pkg.version}`);
|
|
|
|
|
|
|
|
do {
|
2019-03-06 02:52:31 -06:00
|
|
|
if (!usePackageJsonVersion) {
|
|
|
|
const { type } = await promptBumpType();
|
|
|
|
console.log(type);
|
|
|
|
if (type === 'prerelease') {
|
|
|
|
const { id } = await promptPrereleaseId('What kind of prerelease?', false);
|
|
|
|
nextVersion = semver.inc(pkg.version, type, id);
|
2019-03-05 01:56:29 -06:00
|
|
|
} else {
|
2019-03-06 02:52:31 -06:00
|
|
|
const { id } = await promptPrereleaseId();
|
|
|
|
if (id !== 'no') {
|
|
|
|
nextVersion = semver.inc(pkg.version, `pre${type}`, id);
|
|
|
|
} else {
|
|
|
|
nextVersion = semver.inc(pkg.version, type);
|
|
|
|
}
|
2019-03-05 01:56:29 -06:00
|
|
|
}
|
2019-03-06 02:52:31 -06:00
|
|
|
} else {
|
|
|
|
nextVersion = pkg.version;
|
2019-02-21 03:58:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
console.log(chalk.yellowBright.bold(`You are going to release a new version of ${pkg.name}`));
|
2019-03-06 02:52:31 -06:00
|
|
|
|
|
|
|
if (usePackageJsonVersion) {
|
|
|
|
console.log(chalk.green(`Version based on package.json: `), chalk.bold.yellowBright(`${nextVersion}`));
|
|
|
|
} else {
|
|
|
|
console.log(chalk.green(`Version bump: ${pkg.version} ->`), chalk.bold.yellowBright(`${nextVersion}`));
|
|
|
|
}
|
|
|
|
|
2019-02-21 03:58:28 -06:00
|
|
|
const { confirmed } = await promptConfirm();
|
|
|
|
|
|
|
|
releaseConfirmed = confirmed;
|
|
|
|
} while (!releaseConfirmed);
|
|
|
|
|
2019-03-06 02:52:31 -06:00
|
|
|
if (!usePackageJsonVersion) {
|
|
|
|
await bumpVersion(nextVersion);
|
|
|
|
}
|
2019-02-21 03:58:28 -06:00
|
|
|
|
2019-03-08 07:42:19 -06:00
|
|
|
if (createVersionCommit) {
|
|
|
|
await prepareVersionCommitAndPush(nextVersion);
|
|
|
|
}
|
|
|
|
|
2019-03-05 01:56:29 -06:00
|
|
|
if (publishToNpm) {
|
2019-05-22 07:10:13 -05:00
|
|
|
console.log(chalk.yellowBright.bold(`\nReview dist package.json before proceeding!\n`));
|
|
|
|
const { confirmed } = await promptConfirm('Are you ready to publish to npm?');
|
|
|
|
|
|
|
|
if (!confirmed) {
|
|
|
|
process.exit();
|
|
|
|
}
|
|
|
|
|
2019-03-05 01:56:29 -06:00
|
|
|
await publishPackage(pkg.name, nextVersion);
|
|
|
|
console.log(chalk.green(`\nVersion ${nextVersion} of ${pkg.name} succesfully released!`));
|
2019-03-08 07:42:19 -06:00
|
|
|
console.log(chalk.yellow(`\nUpdated @grafana/ui/package.json with version bump created.`));
|
|
|
|
|
2019-03-05 01:56:29 -06:00
|
|
|
process.exit();
|
|
|
|
} else {
|
|
|
|
console.log(
|
|
|
|
chalk.green(
|
|
|
|
`\nVersion ${nextVersion} of ${pkg.name} succesfully prepared for release. See packages/grafana-ui/dist`
|
|
|
|
)
|
|
|
|
);
|
|
|
|
console.log(chalk.green(`\nTo publish to npm registry run`), chalk.bold.blue(`npm run gui:publish`));
|
|
|
|
}
|
2019-02-21 03:58:28 -06:00
|
|
|
};
|
|
|
|
|
2019-03-05 01:56:29 -06:00
|
|
|
export const releaseTask = new Task<ReleaseTaskOptions>();
|
|
|
|
releaseTask.setName('@grafana/ui release');
|
|
|
|
releaseTask.setRunner(releaseTaskRunner);
|