Skip to content

Commit

Permalink
Some fixes on the release script (#557)
Browse files Browse the repository at this point in the history
* Some fixes on the release script

* add --no-latest flag

* readability

* Use inquirer async getters
  • Loading branch information
Janpot authored Jun 15, 2022
1 parent 3218ea6 commit 9b0daf3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 24 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,12 @@
"yarn-deduplicate": "^4.0.0"
},
"dependencies": {
"chalk": "^5.0.1",
"cross-env": "^7.0.3",
"dotenv-cli": "^5.1.0",
"inquirer": "^8.2.4",
"jest-environment-jsdom": "^28.0.0-alpha.8",
"semver": "^7.3.7",
"yargs": "^17.5.1"
}
}
63 changes: 39 additions & 24 deletions scripts/releaseDocker.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const yargs = require('yargs');
const inquirer = require('inquirer');
const semver = require('semver');

const IMAGE_NAME = 'muicom/toolpad';
const LATEST_TAG = 'latest';

async function checkTagExists(image, tag) {
const { execa } = await import('execa');
Expand Down Expand Up @@ -51,21 +53,22 @@ async function showFile(commit, file) {
return stdout;
}

async function main({ yes, force, commit, releaseTag, prerelease }) {
// TODO: if no --commit provided, assume last commit and ask for confirmation
// TODO: if no --releaseTag provided, read it from the --commit and ask for confirmation
async function main({ yes, force, commit, releaseTag, 'no-latest': noLatest, ...rest }) {
const { default: chalk } = await import('chalk');

if (!commit) {
const log = await commitLog();
const answers = await inquirer.prompt([
{
name: 'commit',
message: 'Which commit contains the release?',
type: 'list',
choices: log.map((entry) => ({
value: entry.commit,
name: `${entry.commit} ${entry.subject}`,
})),
async choices() {
const log = await commitLog();
return log.map((entry) => ({
value: entry.commit,
name: `${chalk.blue(entry.commit)} ${entry.subject}`,
}));
},
pageSize: 20,
},
]);
Expand All @@ -74,30 +77,48 @@ async function main({ yes, force, commit, releaseTag, prerelease }) {
}

if (!releaseTag) {
const lernaJson = JSON.parse(await showFile(commit, 'lerna.json'));

const answers = await inquirer.prompt([
{
name: 'releaseTag',
message: 'Which tag will this be release under?',
type: 'input',
default: lernaJson.version,
async default() {
const lernaJson = JSON.parse(await showFile(commit, 'lerna.json'));
return lernaJson.version;
},
},
]);

releaseTag = answers.releaseTag;
}

const parsedVersion = semver.parse(releaseTag);
const isPrerelease = parsedVersion.prerelease.length > 0;

const tags = [releaseTag];

if (!noLatest && !isPrerelease) {
tags.push(LATEST_TAG);
}

if (!yes) {
const baseImage = `${IMAGE_NAME}:${commit}`;
const imagesToBePublished = tags.map((tag) => `${IMAGE_NAME}:${tag}`);
const message = [
`Docker image ${chalk.blue(baseImage)} will be published as:`,
...imagesToBePublished.map((image) => ` - ${chalk.blue(image)}`),
` Does this look right?`,
].join('\n');

const answers = await inquirer.prompt([
{
name: 'confirmed',
message: `Release commit ${commit} as ${IMAGE_NAME}:${releaseTag}?`,
name: 'publishConfirmed',
type: 'confirm',
message,
},
]);

if (!answers.confirmed) {
if (!answers.publishConfirmed) {
// eslint-disable-next-line no-console
console.log(`Canceled`);
return;
Expand All @@ -111,12 +132,6 @@ async function main({ yes, force, commit, releaseTag, prerelease }) {
return;
}

const tags = [releaseTag];

if (!prerelease) {
tags.push('latest-test');
}

await dockerCreateTag(IMAGE_NAME, commit, tags);
}

Expand All @@ -134,13 +149,13 @@ yargs
describe: 'The tag under which to release this image e.g. v1.2.3',
type: 'string',
})
.option('prerelease', {
describe: 'Mark as a prerelease (omits the "latest" tag)',
.option('force', {
describe: 'Create tag, even if it already exists',
type: 'boolean',
default: false,
})
.option('force', {
describe: 'Create tag, even if it already exists',
.option('no-latest', {
describe: 'Don\'t create the "latest" tag.',
type: 'boolean',
default: false,
});
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3095,6 +3095,11 @@ chalk@^4.0.0, chalk@^4.1.0, chalk@^4.1.1:
ansi-styles "^4.1.0"
supports-color "^7.1.0"

chalk@^5.0.1:
version "5.0.1"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-5.0.1.tgz#ca57d71e82bb534a296df63bbacc4a1c22b2a4b6"
integrity sha512-Fo07WOYGqMfCWHOzSXOt2CxDbC6skS/jO9ynEcmpANMoPrD+W1r1K6Vx7iNm+AQmETU1Xr2t+n8nzkV9t6xh3w==

char-regex@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/char-regex/-/char-regex-1.0.2.tgz#d744358226217f981ed58f479b1d6bcc29545dcf"
Expand Down

0 comments on commit 9b0daf3

Please sign in to comment.