diff --git a/src/command/npm.ts b/src/command/npm.ts index cca45e7..5e65c5c 100644 --- a/src/command/npm.ts +++ b/src/command/npm.ts @@ -210,5 +210,29 @@ const install = new Command('install') }); command.addCommand(install); +// npm patch +const patch = new Command('patch').description('npm patch 发布补丁版本').action(async () => { + const cwd = process.cwd(); + const packageJson = path.resolve(cwd, 'package.json'); + if (fileIsExist(packageJson)) { + const pkg = fs.readFileSync(packageJson, 'utf-8'); + const pkgJson = parseIfJson(pkg); + const version = pkgJson?.version as string; + if (version) { + const versionArr = String(version).split('.'); + if (versionArr.length === 3) { + const patchVersion = Number(versionArr[2]) + 1; + const newVersion = `${versionArr[0]}.${versionArr[1]}.${patchVersion}`; + pkgJson.version = newVersion; + fs.writeFileSync(packageJson, JSON.stringify(pkgJson, null, 2)); + console.log(chalk.green(`${pkgJson?.name} 更新到版本: ${newVersion}`)); + } + } + } +}); +command.addCommand(patch); + // program 添加npm 的命令 program.addCommand(command); + +