|
| 1 | +// @ts-check |
| 2 | + |
| 3 | +/** |
| 4 | + * modified from https://github.com/vuejs/vue-next/blob/master/scripts/release.js |
| 5 | + */ |
| 6 | +const execa = require('execa') |
| 7 | +const path = require('path') |
| 8 | +const fs = require('fs') |
| 9 | +const args = require('minimist')(process.argv.slice(2)) |
| 10 | +const semver = require('semver') |
| 11 | +const chalk = require('chalk') |
| 12 | +const { prompt } = require('enquirer') |
| 13 | + |
| 14 | +const pkgDir = process.cwd() |
| 15 | +const pkgPath = path.resolve(pkgDir, 'package.json') |
| 16 | +/** |
| 17 | + * @type {{ name: string, version: string }} |
| 18 | + */ |
| 19 | +const pkg = require(pkgPath) |
| 20 | +const pkgName = pkg.name.replace(/^@vitejs\//, '') |
| 21 | +const currentVersion = pkg.version |
| 22 | +/** |
| 23 | + * @type {boolean} |
| 24 | + */ |
| 25 | +const isDryRun = args.dry |
| 26 | +/** |
| 27 | + * @type {boolean} |
| 28 | + */ |
| 29 | +const skipBuild = args.skipBuild |
| 30 | + |
| 31 | +/** |
| 32 | + * @type {import('semver').ReleaseType[]} |
| 33 | + */ |
| 34 | +const versionIncrements = [ |
| 35 | + 'patch', |
| 36 | + 'minor', |
| 37 | + 'major', |
| 38 | + 'prepatch', |
| 39 | + 'preminor', |
| 40 | + 'premajor', |
| 41 | + 'prerelease' |
| 42 | +] |
| 43 | + |
| 44 | +/** |
| 45 | + * @param {import('semver').ReleaseType} i |
| 46 | + */ |
| 47 | +const inc = (i) => semver.inc(currentVersion, i) |
| 48 | + |
| 49 | +/** |
| 50 | + * @param {string} bin |
| 51 | + * @param {string[]} args |
| 52 | + * @param {object} opts |
| 53 | + */ |
| 54 | +const run = (bin, args, opts = {}) => |
| 55 | + execa(bin, args, { stdio: 'inherit', ...opts }) |
| 56 | + |
| 57 | +/** |
| 58 | + * @param {string} bin |
| 59 | + * @param {string[]} args |
| 60 | + * @param {object} opts |
| 61 | + */ |
| 62 | +const dryRun = (bin, args, opts = {}) => |
| 63 | + console.log(chalk.blue(`[dryrun] ${bin} ${args.join(' ')}`), opts) |
| 64 | + |
| 65 | +const runIfNotDry = isDryRun ? dryRun : run |
| 66 | + |
| 67 | +/** |
| 68 | + * @param {string} msg |
| 69 | + */ |
| 70 | +const step = (msg) => console.log(chalk.cyan(msg)) |
| 71 | + |
| 72 | +async function main() { |
| 73 | + let targetVersion = args._[0] |
| 74 | + |
| 75 | + if (!targetVersion) { |
| 76 | + // no explicit version, offer suggestions |
| 77 | + /** |
| 78 | + * @type {{ release: string }} |
| 79 | + */ |
| 80 | + const { release } = await prompt({ |
| 81 | + type: 'select', |
| 82 | + name: 'release', |
| 83 | + message: 'Select release type', |
| 84 | + choices: versionIncrements |
| 85 | + .map((i) => `${i} (${inc(i)})`) |
| 86 | + .concat(['custom']) |
| 87 | + }) |
| 88 | + |
| 89 | + if (release === 'custom') { |
| 90 | + /** |
| 91 | + * @type {{ version: string }} |
| 92 | + */ |
| 93 | + const res = await prompt({ |
| 94 | + type: 'input', |
| 95 | + name: 'version', |
| 96 | + message: 'Input custom version', |
| 97 | + initial: currentVersion |
| 98 | + }) |
| 99 | + targetVersion = res.version |
| 100 | + } else { |
| 101 | + targetVersion = release.match(/\((.*)\)/)[1] |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + if (!semver.valid(targetVersion)) { |
| 106 | + throw new Error(`invalid target version: ${targetVersion}`) |
| 107 | + } |
| 108 | + |
| 109 | + const tag = |
| 110 | + pkgName === 'vite' ? `v${targetVersion}` : `${pkgName}@${targetVersion}` |
| 111 | + |
| 112 | + /** |
| 113 | + * @type {{ yes: boolean }} |
| 114 | + */ |
| 115 | + const { yes } = await prompt({ |
| 116 | + type: 'confirm', |
| 117 | + name: 'yes', |
| 118 | + message: `Releasing ${tag}. Confirm?` |
| 119 | + }) |
| 120 | + |
| 121 | + if (!yes) { |
| 122 | + return |
| 123 | + } |
| 124 | + |
| 125 | + step('\nUpdating package version...') |
| 126 | + updateVersion(targetVersion) |
| 127 | + |
| 128 | + step('\nBuilding package...') |
| 129 | + if (!skipBuild && !isDryRun) { |
| 130 | + await run('yarn', ['build']) |
| 131 | + } else { |
| 132 | + console.log(`(skipped)`) |
| 133 | + } |
| 134 | + |
| 135 | + step('\nGenerating changelog...') |
| 136 | + await run('yarn', ['changelog']) |
| 137 | + |
| 138 | + const { stdout } = await run('git', ['diff'], { stdio: 'pipe' }) |
| 139 | + if (stdout) { |
| 140 | + step('\nCommitting changes...') |
| 141 | + await runIfNotDry('git', ['add', '-A']) |
| 142 | + await runIfNotDry('git', ['commit', '-m', `release: ${tag}`]) |
| 143 | + } else { |
| 144 | + console.log('No changes to commit.') |
| 145 | + } |
| 146 | + |
| 147 | + step('\nPublishing package...') |
| 148 | + await publishPackage(targetVersion, runIfNotDry) |
| 149 | + |
| 150 | + step('\nPushing to GitHub...') |
| 151 | + await runIfNotDry('git', ['tag', tag]) |
| 152 | + await runIfNotDry('git', ['push', 'origin', `refs/tags/${tag}`]) |
| 153 | + await runIfNotDry('git', ['push']) |
| 154 | + |
| 155 | + if (isDryRun) { |
| 156 | + console.log(`\nDry run finished - run git diff to see package changes.`) |
| 157 | + } |
| 158 | + |
| 159 | + console.log() |
| 160 | +} |
| 161 | + |
| 162 | +/** |
| 163 | + * @param {string} version |
| 164 | + */ |
| 165 | +function updateVersion(version) { |
| 166 | + const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8')) |
| 167 | + pkg.version = version |
| 168 | + fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n') |
| 169 | +} |
| 170 | + |
| 171 | +/** |
| 172 | + * @param {string} version |
| 173 | + * @param {Function} runIfNotDry |
| 174 | + */ |
| 175 | +async function publishPackage(version, runIfNotDry) { |
| 176 | + const publicArgs = [ |
| 177 | + 'publish', |
| 178 | + '--no-git-tag-version', |
| 179 | + '--new-version', |
| 180 | + version, |
| 181 | + '--access', |
| 182 | + 'public' |
| 183 | + ] |
| 184 | + if (args.tag) { |
| 185 | + publicArgs.push(`--tag`, args.tag) |
| 186 | + } |
| 187 | + try { |
| 188 | + await runIfNotDry('yarn', publicArgs, { |
| 189 | + stdio: 'pipe' |
| 190 | + }) |
| 191 | + console.log(chalk.green(`Successfully published ${pkgName}@${version}`)) |
| 192 | + } catch (e) { |
| 193 | + if (e.stderr.match(/previously published/)) { |
| 194 | + console.log(chalk.red(`Skipping already published: ${pkgName}`)) |
| 195 | + } else { |
| 196 | + throw e |
| 197 | + } |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +main().catch((err) => { |
| 202 | + console.error(err) |
| 203 | +}) |
0 commit comments