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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
24 changed files with 222 additions and 1704 deletions

View File

@ -9,7 +9,7 @@
"scripts": { "scripts": {
"dev": "tsc --watch", "dev": "tsc --watch",
"build": "rimraf ./lib && tsc", "build": "rimraf ./lib && tsc",
"release": "pnpm build & release-it", "release": "vant-cli release",
"prepare": "pnpm build" "prepare": "pnpm build"
}, },
"files": [ "files": [
@ -32,7 +32,6 @@
"license": "MIT", "license": "MIT",
"devDependencies": { "devDependencies": {
"@types/fs-extra": "^11.0.1", "@types/fs-extra": "^11.0.1",
"release-it": "^16.1.3",
"rimraf": "^5.0.0", "rimraf": "^5.0.0",
"typescript": "^5.0.4" "typescript": "^5.0.4"
}, },
@ -42,11 +41,5 @@
"fs-extra": "^11.1.0", "fs-extra": "^11.1.0",
"enquirer": "2.3.6", "enquirer": "2.3.6",
"picocolors": "^1.0.0" "picocolors": "^1.0.0"
},
"release-it": {
"git": {
"tag": false,
"commitMessage": "release: create-vant-cli-app v${version}"
}
} }
} }

View File

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

View File

@ -21,7 +21,7 @@
"build:types": "tsc -p ./tsconfig.json --emitDeclarationOnly", "build:types": "tsc -p ./tsconfig.json --emitDeclarationOnly",
"build:bundle": "node ./build.js", "build:bundle": "node ./build.js",
"build": "pnpm clean && pnpm build:bundle && pnpm build:types", "build": "pnpm clean && pnpm build:bundle && pnpm build:types",
"release": "pnpm build && release-it", "release": "vant-cli release",
"prepare": "pnpm build" "prepare": "pnpm build"
}, },
"publishConfig": { "publishConfig": {
@ -38,14 +38,7 @@
"license": "MIT", "license": "MIT",
"devDependencies": { "devDependencies": {
"esbuild": "^0.18.11", "esbuild": "^0.18.11",
"release-it": "^16.1.3",
"rimraf": "^5.0.0", "rimraf": "^5.0.0",
"typescript": "^5.0.4" "typescript": "^5.0.4"
},
"release-it": {
"git": {
"tag": false,
"commitMessage": "release: @vant/area-data v${version}"
}
} }
} }

View File

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

View File

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

View File

@ -13,7 +13,7 @@
"scripts": { "scripts": {
"dev": "tsc --watch", "dev": "tsc --watch",
"build": "rimraf ./lib && tsc", "build": "rimraf ./lib && tsc",
"release": "pnpm build & release-it", "release": "vant-cli release",
"prepare": "pnpm build" "prepare": "pnpm build"
}, },
"files": [ "files": [
@ -63,6 +63,7 @@
"esbuild": "^0.18.11", "esbuild": "^0.18.11",
"eslint": "^8.31.0", "eslint": "^8.31.0",
"execa": "^6.1.0", "execa": "^6.1.0",
"enquirer": "2.3.6",
"fast-glob": "^3.2.11", "fast-glob": "^3.2.11",
"fs-extra": "^11.1.0", "fs-extra": "^11.1.0",
"hash-sum": "^2.0.0", "hash-sum": "^2.0.0",
@ -82,18 +83,11 @@
"postcss": "^8.4.23", "postcss": "^8.4.23",
"postcss-load-config": "^4.0.1", "postcss-load-config": "^4.0.1",
"prettier": "^3.0.0", "prettier": "^3.0.0",
"release-it": "^16.1.3",
"terser": "^5.16.1", "terser": "^5.16.1",
"transliteration": "^2.3.5", "transliteration": "^2.3.5",
"typescript": "^5.0.4", "typescript": "^5.0.4",
"vite": "^4.4.2", "vite": "^4.4.2",
"vite-plugin-md": "^0.11.9", "vite-plugin-md": "^0.11.9",
"vue-router": "^4.1.6" "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 fse from 'fs-extra';
import releaseIt from 'release-it'; import { join } from 'node:path';
import { join, dirname } from 'node:path'; import color from 'picocolors';
import { fileURLToPath } from 'node:url'; 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)); function logCurrentVersion(cwd: string) {
const PLUGIN_PATH = join(__dirname, '../compiler/vant-cli-release-plugin.js'); 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 }) { export async function release(command: { tag?: string }) {
await releaseIt({ const cwd = process.cwd();
plugins: { const { pkgName, currentVersion } = logCurrentVersion(cwd);
[PLUGIN_PATH]: {}, const version = await getNewVersion();
}, const tag = getNpmTag(version, command.tag);
npm: { const packageManager = getPackageManager();
tag: command.tag,
}, setPkgVersion(version, cwd);
git: {
tagName: 'v${version}', try {
commitMessage: 'release: ${version}', 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; return hasYarnCache;
} }
function getPackageManager() { export function getPackageManager() {
const { build } = getVantConfig(); const { build } = getVantConfig();
if (build?.packageManager) { 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 // some modules with missing type definitions
declare module 'hash-sum'; declare module 'hash-sum';
declare module '@babel/core'; declare module '@babel/core';
declare module 'release-it';
declare module 'conventional-changelog'; declare module 'conventional-changelog';

View File

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

View File

@ -25,7 +25,7 @@
"build:types": "tsc -p ./tsconfig.json --emitDeclarationOnly", "build:types": "tsc -p ./tsconfig.json --emitDeclarationOnly",
"build:bundle": "node ./build.js", "build:bundle": "node ./build.js",
"build": "pnpm clean && pnpm build:bundle && pnpm build:types", "build": "pnpm clean && pnpm build:bundle && pnpm build:types",
"release": "pnpm build && release-it" "release": "vant-cli release"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
@ -40,14 +40,7 @@
"vant": "workspace:*", "vant": "workspace:*",
"vue": "^3.3.4", "vue": "^3.3.4",
"esbuild": "^0.18.11", "esbuild": "^0.18.11",
"release-it": "^16.1.3",
"rimraf": "^5.0.0", "rimraf": "^5.0.0",
"typescript": "^5.0.4" "typescript": "^5.0.4"
},
"release-it": {
"git": {
"tag": false,
"commitMessage": "release: @vant/compat v${version}"
}
} }
} }

View File

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

View File

@ -8,6 +8,7 @@
"registry": "https://registry.npmjs.org/" "registry": "https://registry.npmjs.org/"
}, },
"scripts": { "scripts": {
"release": "vant-cli release",
"update:deps": "pnpm update --latest --interactive" "update:deps": "pnpm update --latest --interactive"
}, },
"repository": { "repository": {

View File

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

View File

@ -8,7 +8,7 @@
"src" "src"
], ],
"scripts": { "scripts": {
"release": "release-it" "release": "vant-cli release"
}, },
"publishConfig": { "publishConfig": {
"access": "public", "access": "public",
@ -21,16 +21,5 @@
}, },
"bugs": "https://github.com/vant-ui/vant/issues", "bugs": "https://github.com/vant-ui/vant/issues",
"author": "chenjiahan", "author": "chenjiahan",
"license": "MIT", "license": "MIT"
"devDependencies": {
"release-it": "^16.1.3"
},
"release-it": {
"git": {
"tag": false,
"commitMessage": "release: @vant/icons v${version}",
"addUntrackedFiles": true,
"requireCleanWorkingDir": false
}
}
} }

View File

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

View File

@ -25,7 +25,7 @@
"build:types": "tsc -p ./tsconfig.json --emitDeclarationOnly", "build:types": "tsc -p ./tsconfig.json --emitDeclarationOnly",
"build:bundle": "node ./build.js", "build:bundle": "node ./build.js",
"build": "pnpm clean && pnpm build:bundle && pnpm build:types", "build": "pnpm clean && pnpm build:bundle && pnpm build:types",
"release": "pnpm build && release-it", "release": "vant-cli release",
"prepare": "pnpm build" "prepare": "pnpm build"
}, },
"repository": { "repository": {
@ -39,14 +39,7 @@
"devDependencies": { "devDependencies": {
"@popperjs/core": "^2.11.6", "@popperjs/core": "^2.11.6",
"esbuild": "^0.18.11", "esbuild": "^0.18.11",
"release-it": "^16.1.3",
"rimraf": "^5.0.0", "rimraf": "^5.0.0",
"typescript": "^5.0.4" "typescript": "^5.0.4"
},
"release-it": {
"git": {
"tag": false,
"commitMessage": "release: @vant/popperjs v${version}"
}
} }
} }

View File

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

View File

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

View File

@ -22,7 +22,7 @@
"build:types": "tsc -p ./tsconfig.json --emitDeclarationOnly", "build:types": "tsc -p ./tsconfig.json --emitDeclarationOnly",
"build:bundle": "node ./build.js", "build:bundle": "node ./build.js",
"build": "pnpm clean && pnpm build:bundle && pnpm build:types", "build": "pnpm clean && pnpm build:bundle && pnpm build:types",
"release": "pnpm build && release-it", "release": "vant-cli release",
"prepare": "pnpm build" "prepare": "pnpm build"
}, },
"publishConfig": { "publishConfig": {
@ -39,18 +39,11 @@
"license": "MIT", "license": "MIT",
"devDependencies": { "devDependencies": {
"esbuild": "^0.18.11", "esbuild": "^0.18.11",
"release-it": "^16.1.3",
"rimraf": "^5.0.0", "rimraf": "^5.0.0",
"typescript": "^5.0.4", "typescript": "^5.0.4",
"vue": "^3.3.4" "vue": "^3.3.4"
}, },
"peerDependencies": { "peerDependencies": {
"vue": "^3.0.0" "vue": "^3.0.0"
},
"release-it": {
"git": {
"tag": false,
"commitMessage": "release: @vant/use v${version}"
}
} }
} }

View File

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

View File

@ -70,10 +70,5 @@
"lib/**/style/*", "lib/**/style/*",
"*.css" "*.css"
], ],
"release-it": {
"git": {
"commitMessage": "release: vant v${version}"
}
},
"web-types": "lib/web-types.json" "web-types": "lib/web-types.json"
} }

1669
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff