Skip to content

Commit

Permalink
workflow: adjust release setup
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jan 2, 2021
1 parent 960e3e5 commit 2de3222
Show file tree
Hide file tree
Showing 4 changed files with 216 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@types/jest": "^26.0.19",
"@types/node": "^14.14.10",
"@typescript-eslint/parser": "^4.9.1",
"chalk": "^4.1.0",
"conventional-changelog-cli": "^2.1.1",
"cross-env": "^7.0.3",
"enquirer": "^2.3.6",
Expand Down
9 changes: 9 additions & 0 deletions packages/plugin-vue/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## [1.0.2](https://github.com/vitejs/vite/compare/plugin-vue@1.0.2...plugin-vue@1.0.2) (2021-01-02)


### Bug Fixes

* **plugin-vue:** avoid throwing on never requested file ([48a24c1](https://github.com/vitejs/vite/commit/48a24c1fa1f64e89ca853635580911859ef5881b))
* **plugin-vue:** custom block prev handling ([8dbc2b4](https://github.com/vitejs/vite/commit/8dbc2b47dd8fea4a953fb05057edb47122e2dcb7))
* avoid self referencing type in plugin-vue ([9cccdaa](https://github.com/vitejs/vite/commit/9cccdaa0935ca664c8a709a89ebd1f2216565546))
* **plugin-vue:** ensure id on descriptor ([91217f6](https://github.com/vitejs/vite/commit/91217f6d968485303e71128bb79ad4400b9b4412))
5 changes: 3 additions & 2 deletions packages/plugin-vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"build": "rimraf dist && run-s build-bundle build-types",
"build-bundle": "esbuild src/index.ts --bundle --platform=node --target=node12 --external:@vue/compiler-sfc --outfile=dist/index.js",
"build-types": "tsc -p . --emitDeclarationOnly --outDir temp && api-extractor run && rimraf temp",
"prepublishOnly": "yarn build"
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s --commit-path . --lerna-package plugin-vue",
"release": "node ../../scripts/release.js"
},
"engines": {
"node": ">=12.0.0"
Expand All @@ -24,7 +25,7 @@
"bugs": {
"url": "https://github.com/vitejs/vite/issues"
},
"homepage": "https://github.com/vitejs/vite/tree/master/#readme",
"homepage": "https://github.com/vitejs/vite/tree/master/packages/plugin-vue#readme",
"peerDependencies": {
"@vue/compiler-sfc": "^3.0.4"
},
Expand Down
203 changes: 203 additions & 0 deletions scripts/release.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
// @ts-check

/**
* modified from https://github.com/vuejs/vue-next/blob/master/scripts/release.js
*/
const execa = require('execa')
const path = require('path')
const fs = require('fs')
const args = require('minimist')(process.argv.slice(2))
const semver = require('semver')
const chalk = require('chalk')
const { prompt } = require('enquirer')

const pkgDir = process.cwd()
const pkgPath = path.resolve(pkgDir, 'package.json')
/**
* @type {{ name: string, version: string }}
*/
const pkg = require(pkgPath)
const pkgName = pkg.name.replace(/^@vitejs\//, '')
const currentVersion = pkg.version
/**
* @type {boolean}
*/
const isDryRun = args.dry
/**
* @type {boolean}
*/
const skipBuild = args.skipBuild

/**
* @type {import('semver').ReleaseType[]}
*/
const versionIncrements = [
'patch',
'minor',
'major',
'prepatch',
'preminor',
'premajor',
'prerelease'
]

/**
* @param {import('semver').ReleaseType} i
*/
const inc = (i) => semver.inc(currentVersion, i)

/**
* @param {string} bin
* @param {string[]} args
* @param {object} opts
*/
const run = (bin, args, opts = {}) =>
execa(bin, args, { stdio: 'inherit', ...opts })

/**
* @param {string} bin
* @param {string[]} args
* @param {object} opts
*/
const dryRun = (bin, args, opts = {}) =>
console.log(chalk.blue(`[dryrun] ${bin} ${args.join(' ')}`), opts)

const runIfNotDry = isDryRun ? dryRun : run

/**
* @param {string} msg
*/
const step = (msg) => console.log(chalk.cyan(msg))

async function main() {
let targetVersion = args._[0]

if (!targetVersion) {
// no explicit version, offer suggestions
/**
* @type {{ release: string }}
*/
const { release } = await prompt({
type: 'select',
name: 'release',
message: 'Select release type',
choices: versionIncrements
.map((i) => `${i} (${inc(i)})`)
.concat(['custom'])
})

if (release === 'custom') {
/**
* @type {{ version: string }}
*/
const res = await prompt({
type: 'input',
name: 'version',
message: 'Input custom version',
initial: currentVersion
})
targetVersion = res.version
} else {
targetVersion = release.match(/\((.*)\)/)[1]
}
}

if (!semver.valid(targetVersion)) {
throw new Error(`invalid target version: ${targetVersion}`)
}

const tag =
pkgName === 'vite' ? `v${targetVersion}` : `${pkgName}@${targetVersion}`

/**
* @type {{ yes: boolean }}
*/
const { yes } = await prompt({
type: 'confirm',
name: 'yes',
message: `Releasing ${tag}. Confirm?`
})

if (!yes) {
return
}

step('\nUpdating package version...')
updateVersion(targetVersion)

step('\nBuilding package...')
if (!skipBuild && !isDryRun) {
await run('yarn', ['build'])
} else {
console.log(`(skipped)`)
}

step('\nGenerating changelog...')
await run('yarn', ['changelog'])

const { stdout } = await run('git', ['diff'], { stdio: 'pipe' })
if (stdout) {
step('\nCommitting changes...')
await runIfNotDry('git', ['add', '-A'])
await runIfNotDry('git', ['commit', '-m', `release: ${tag}`])
} else {
console.log('No changes to commit.')
}

step('\nPublishing package...')
await publishPackage(targetVersion, runIfNotDry)

step('\nPushing to GitHub...')
await runIfNotDry('git', ['tag', tag])
await runIfNotDry('git', ['push', 'origin', `refs/tags/${tag}`])
await runIfNotDry('git', ['push'])

if (isDryRun) {
console.log(`\nDry run finished - run git diff to see package changes.`)
}

console.log()
}

/**
* @param {string} version
*/
function updateVersion(version) {
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))
pkg.version = version
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n')
}

/**
* @param {string} version
* @param {Function} runIfNotDry
*/
async function publishPackage(version, runIfNotDry) {
const publicArgs = [
'publish',
'--no-git-tag-version',
'--new-version',
version,
'--access',
'public'
]
if (args.tag) {
publicArgs.push(`--tag`, args.tag)
}
try {
await runIfNotDry('yarn', publicArgs, {
stdio: 'pipe'
})
console.log(chalk.green(`Successfully published ${pkgName}@${version}`))
} catch (e) {
if (e.stderr.match(/previously published/)) {
console.log(chalk.red(`Skipping already published: ${pkgName}`))
} else {
throw e
}
}
}

main().catch((err) => {
console.error(err)
})

0 comments on commit 2de3222

Please sign in to comment.