代码拉取完成,页面将自动刷新
const fs = require('fs')
const path = require('path')
const semver = require('semver')
const prompts = require('prompts')
const execa = require('execa')
const kolorist = require('kolorist')
const argv = require('minimist')(process.argv.slice(2))
const pkgDir = process.cwd()
const pkgPath = path.resolve(pkgDir, 'package.json')
const pkg = require(pkgPath)
const pkgName = pkg.name
const currentVersion = pkg.version
const step = (msg) => console.log(kolorist.cyan(msg))
async function main() {
// 获取发布版本号
let targetVersion = argv._[0]
if (!targetVersion) {
const res = await prompts({
type: 'text',
name: 'version',
message: '输入自定义版本',
initial: currentVersion,
})
targetVersion = res.version
}
if (!semver.valid(targetVersion)) {
throw new Error(`invalid target version: ${targetVersion}`)
}
const tag = `${pkgName}@${targetVersion}`
const { yes } = await prompts({
type: 'confirm',
name: 'yes',
message: `Releasing ${tag}. Confirm?`,
})
if (!yes) {
return
}
// 更新版本信息
step('\n更新版本信息..')
updateVersion(targetVersion)
// git add
step('\n提交代码...')
const { stdout } = await execa('git', ['diff'], { stdio: 'pipe' })
if (stdout) {
step('\nCommiting changes...')
await execa('git', ['add', '-A'])
await execa('git', ['commit', '-m', `release: ${tag}`])
} else {
console.log('No changes to commit.')
}
// 发布
step('\n发布 package...')
await publishPackage(targetVersion)
// 提交
step('\n发布到 Gitee...')
await execa('git', ['push', 'origin', 'master'])
console.log()
}
async function publishPackage(version) {
// 发布
const publicArgs = [
'publish',
'--no-git-tag-version',
'--access=public',
'--registry=https://registry.npmjs.org',
]
const { otp } = await prompts({
type: 'text',
name: 'otp',
message: '输入一次性验证码',
})
if (otp) {
publicArgs.push(`--otp=${otp}`)
console.log('optCode ', publicArgs)
try {
await execa('npm', publicArgs, { stdio: 'pipe' })
console.log(kolorist.green(`成功发布 ${pkgName}@${version}`))
} catch (e) {
if (e.stderr.match(/previously published/)) {
console.log(kolorist.red(`跳过~当前版本已经发布过`))
} else {
throw e
}
}
}
}
// 更新版本信息
function updateVersion(version) {
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))
pkg.version = version
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n')
}
main().catch((err) => {
console.error(err)
})
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。