1 Star 0 Fork 0

neterm/create-vite-vue2

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
release.js 2.60 KB
一键复制 编辑 原始数据 按行查看 历史
neterm 提交于 2022-07-18 22:51 +08:00 . release: create-vite-vue2@0.0.7
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)
})
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/neterm/create-vite-vue2.git
git@gitee.com:neterm/create-vite-vue2.git
neterm
create-vite-vue2
create-vite-vue2
master

搜索帮助