Skip to main content

ZX + GitHooks 自动发版

zx 是 Google 开源的 JavaScript 终端执行库,使用方式非常简洁。可用来编写自动化脚本。

import { $ } from 'zx'

// 打印
await $`echo 1`

// 返一个 returnCode
;(await $`bash -c "exit 100"`).exitCode // 100

// 交互场景 std 流直传
const { stdin, stdout, stderr } = process
await $`vi`.stdio(stdin, stdout, stderr)

Snippet

可通过 husky 部署 post-commit 钩子,每次提交完成自动发一个 Git Tag

#!tsx

import * as zx from 'zx'
import pkg from '../package.json'
export {}

const { stdin, stdout, stderr } = process

// 检查已安装 git
if ((await zx.$`git --version`.quiet().exitCode) !== 0) {
  throw 'git not found'
}

// 检查是一个 git 目录
if ((await zx.$`git tag -ln1`.quiet().exitCode) !== 0) {
  throw 'not a git directory'
}

const ver = pkg.version ?? '1.0.0'

// 强制发一个版本
await zx.$`git tag -f ${ver}`.stdio(stdin, stdout, stderr)