From 9b0daf309bde9b4cfd46ea1240361b8986727984 Mon Sep 17 00:00:00 2001 From: Jan Potoms <2109932+Janpot@users.noreply.github.com> Date: Wed, 15 Jun 2022 13:46:58 +0200 Subject: [PATCH] Some fixes on the release script (#557) * Some fixes on the release script * add --no-latest flag * readability * Use inquirer async getters --- package.json | 2 ++ scripts/releaseDocker.js | 63 +++++++++++++++++++++++++--------------- yarn.lock | 5 ++++ 3 files changed, 46 insertions(+), 24 deletions(-) diff --git a/package.json b/package.json index 9722ba7f4c2..536dcb384e1 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/scripts/releaseDocker.js b/scripts/releaseDocker.js index ec61b35cd69..bcc1ac3c556 100644 --- a/scripts/releaseDocker.js +++ b/scripts/releaseDocker.js @@ -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'); @@ -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, }, ]); @@ -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; @@ -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); } @@ -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, }); diff --git a/yarn.lock b/yarn.lock index 5e96a15c69e..398b360d100 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"