refactor(cli): rewrite release command (#12133)

* refactor(cli): rewrite release command

* fix: duplicated build

* fix: no git check

* feat: rollback version

* chore: release config

* chore: add release config
This commit is contained in:
neverland
2023-07-30 10:43:26 +08:00
committed by GitHub
parent 2513ad1217
commit e6a0694807
24 changed files with 222 additions and 1704 deletions

View File

@@ -50,7 +50,7 @@ npx vant-cli dev
### release
发布组件库,发布前会自动执行 build 和 changelog 命令,并通过 [release-it](https://github.com/release-it/release-it) 发布 npm 包。
发布组件库,发布前会自动执行 build 和 changelog 命令,并按照流程发布 npm 包。
### changelog

View File

@@ -13,7 +13,7 @@
"scripts": {
"dev": "tsc --watch",
"build": "rimraf ./lib && tsc",
"release": "pnpm build & release-it",
"release": "vant-cli release",
"prepare": "pnpm build"
},
"files": [
@@ -63,6 +63,7 @@
"esbuild": "^0.18.11",
"eslint": "^8.31.0",
"execa": "^6.1.0",
"enquirer": "2.3.6",
"fast-glob": "^3.2.11",
"fs-extra": "^11.1.0",
"hash-sum": "^2.0.0",
@@ -82,18 +83,11 @@
"postcss": "^8.4.23",
"postcss-load-config": "^4.0.1",
"prettier": "^3.0.0",
"release-it": "^16.1.3",
"terser": "^5.16.1",
"transliteration": "^2.3.5",
"typescript": "^5.0.4",
"vite": "^4.4.2",
"vite-plugin-md": "^0.11.9",
"vue-router": "^4.1.6"
},
"release-it": {
"git": {
"tag": false,
"commitMessage": "release: @vant/cli v${version}"
}
}
}

View File

@@ -1,22 +1,103 @@
/* eslint-disable no-template-curly-in-string */
import releaseIt from 'release-it';
import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import fse from 'fs-extra';
import { join } from 'node:path';
import color from 'picocolors';
import enquirer from 'enquirer';
import { consola } from '../common/logger.js';
import { getPackageManager } from '../common/manager.js';
import { execSync } from 'child_process';
const __dirname = dirname(fileURLToPath(import.meta.url));
const PLUGIN_PATH = join(__dirname, '../compiler/vant-cli-release-plugin.js');
function logCurrentVersion(cwd: string) {
const pkgJson = join(cwd, 'package.json');
const pkg = fse.readJSONSync(pkgJson);
consola.success(`${color.bold('Current package:')} ${color.green(pkg.name)}`);
consola.success(
`${color.bold('Current version:')} ${color.green(pkg.version)}`,
);
return {
pkgName: pkg.name,
currentVersion: pkg.version,
};
}
async function getNewVersion() {
const { version } = await enquirer.prompt<{ version: string }>({
type: 'input',
name: 'version',
message: 'Version to release:',
});
return version;
}
function getNpmTag(version: string, forceTag?: string) {
let tag: string;
if (forceTag) {
tag = forceTag;
} else if (version.includes('beta')) {
tag = 'beta';
} else if (version.includes('alpha')) {
tag = 'alpha';
} else if (version.includes('rc')) {
tag = 'rc';
} else {
tag = 'latest';
}
consola.success(`${color.bold('Npm tag:')} ${color.green(tag)}`);
return tag;
}
function setPkgVersion(version: string, cwd: string) {
const pkgJson = join(cwd, 'package.json');
const pkg = fse.readJSONSync(pkgJson);
pkg.version = version;
fse.writeJSONSync(pkgJson, pkg, { spaces: 2 });
}
function buildPackage(packageManager: string) {
const command = `${packageManager} run build`;
consola.success(`${color.bold('Build package:')} ${color.green(command)}`);
execSync(command, { stdio: 'inherit' });
}
function generateChangelog() {
execSync('vant-cli changelog', { stdio: 'inherit' });
}
function publishPackage(packageManager: string, tag: string) {
let command = `${packageManager} publish --tag ${tag}`;
if (packageManager === 'pnpm') {
command += ' --no-git-checks';
}
execSync(command, { stdio: 'inherit' });
}
function commitChanges(pkgName: string, version: string) {
const message = `release: ${pkgName} v${version}`;
execSync(`git add -A && git commit -m "${message}"`, { stdio: 'inherit' });
}
export async function release(command: { tag?: string }) {
await releaseIt({
plugins: {
[PLUGIN_PATH]: {},
},
npm: {
tag: command.tag,
},
git: {
tagName: 'v${version}',
commitMessage: 'release: ${version}',
},
});
const cwd = process.cwd();
const { pkgName, currentVersion } = logCurrentVersion(cwd);
const version = await getNewVersion();
const tag = getNpmTag(version, command.tag);
const packageManager = getPackageManager();
setPkgVersion(version, cwd);
try {
buildPackage(packageManager);
generateChangelog();
} catch (err) {
consola.error('Failed to build package, rollback to the previous version.');
setPkgVersion(currentVersion, cwd);
throw err;
}
publishPackage(packageManager, tag);
commitChanges(pkgName, version);
}

View File

@@ -18,7 +18,7 @@ export function hasYarn() {
return hasYarnCache;
}
function getPackageManager() {
export function getPackageManager() {
const { build } = getVantConfig();
if (build?.packageManager) {

View File

@@ -1,14 +0,0 @@
import { Plugin } from 'release-it';
import { execSync } from 'child_process';
class VantCliReleasePlugin extends Plugin {
async beforeRelease() {
// log an empty line
console.log('');
execSync('vant-cli build', { stdio: 'inherit' });
execSync('vant-cli changelog', { stdio: 'inherit' });
}
}
export default VantCliReleasePlugin;

View File

@@ -1,5 +1,4 @@
// some modules with missing type definitions
declare module 'hash-sum';
declare module '@babel/core';
declare module 'release-it';
declare module 'conventional-changelog';

View File

@@ -0,0 +1,5 @@
export default {
build: {
packageManager: 'pnpm',
},
};