fix(cli): allow missing build script when releasing

This commit is contained in:
chenjiahan 2023-09-17 11:09:03 +08:00 committed by neverland
parent aceb40a5f6
commit c1bf174270

View File

@ -48,17 +48,21 @@ function getNpmTag(version: string, forceTag?: string) {
return tag; return tag;
} }
function setPkgVersion(version: string, cwd: string) { function setPkgVersion(
const pkgJson = join(cwd, 'package.json'); pkgJson: Record<string, any>,
const pkg = fse.readJSONSync(pkgJson); pkgJsonPath: string,
pkg.version = version; version: string,
fse.writeJSONSync(pkgJson, pkg, { spaces: 2 }); ) {
pkgJson.version = version;
fse.writeJSONSync(pkgJsonPath, pkgJson, { spaces: 2 });
} }
function buildPackage(packageManager: string) { function buildPackage(pkgJson: Record<string, any>, packageManager: string) {
const command = `${packageManager} run build`; if (pkgJson.scripts?.build) {
consola.success(`${color.bold('Build package:')} ${color.green(command)}`); const command = `${packageManager} run build`;
execSync(command, { stdio: 'inherit' }); consola.success(`${color.bold('Build package:')} ${color.green(command)}`);
execSync(command, { stdio: 'inherit' });
}
} }
function publishPackage(packageManager: string, tag: string) { function publishPackage(packageManager: string, tag: string) {
@ -101,13 +105,16 @@ export async function release(command: { tag?: string; gitTag?: boolean }) {
const tag = getNpmTag(version, command.tag); const tag = getNpmTag(version, command.tag);
const packageManager = getPackageManager(); const packageManager = getPackageManager();
setPkgVersion(version, cwd); const pkgJsonPath = join(cwd, 'package.json');
const pkgJson = fse.readJSONSync(pkgJsonPath);
setPkgVersion(pkgJson, pkgJsonPath, version);
try { try {
buildPackage(packageManager); buildPackage(pkgJson, packageManager);
} catch (err) { } catch (err) {
consola.error('Failed to build package, rollback to the previous version.'); consola.error('Failed to build package, rollback to the previous version.');
setPkgVersion(currentVersion, cwd); setPkgVersion(pkgJson, pkgJsonPath, currentVersion);
throw err; throw err;
} }