Skip to content

配置指南 ​

@neosjs/release 开箱即用,内置完整的默认配置。配置文件中只需写入需要覆盖的选项即可。

支持的配置文件 ​

工具基于 c12 在项目根目录自动查找以下配置文件:

  • release.config.ts
  • release.config.js / release.config.mjs / release.config.cjs
  • release.config.json
  • release.config.yaml / release.config.yml
  • release.config.toml
  • package.json 中的 "release.config" 字段

也可以通过命令行 -c / --config 指定自定义路径,或使用 --no-config 禁用本地配置文件:

bash
release --config ./config/release.prod.ts
release --no-config

TypeScript 配置(推荐) ​

使用 defineConfig 和 ConfigOptions 获得完整的类型检查与编辑器自动补全:

ts
import { type ConfigOptions, defineConfig } from '@neosjs/release'

export default defineConfig({
  changelog: {
    generate: true,
    header: '# 更新日志\n\n此变更日志使用 `@neosjs/release` 自动更新.\n',
    infile: 'CHANGELOG.md'
  },
  git: {
    commitMessage: 'chore: release v${version}',
    push: true,
    requireCommits: true,
    requireCommitsFail: false
  },
  npm: {
    publish: true
  },
  github: {
    release: true
  }
}) satisfies ConfigOptions
json
{
  "git": {
    "commitMessage": "chore: release v${version}",
    "push": true
  },
  "npm": {
    "publish": true
  }
}
js
export default {
  git: {
    push: true
  },
  github: {
    release: true
  }
}
yaml
git:
  push: true
  commitMessage: "chore: release v${version}"
npm:
  publish: true

继承配置 ​

借助 c12 的配置继承能力,可以在配置文件中通过 extends 复用团队公共配置(支持本地路径、npm 包或 Git 仓库):

json
{
  "extends": [
    "./release.base.json",
    "@my-org/release-config"
  ],
  "npm": {
    "publish": true
  }
}

优先级与合并规则 ​

最终生效的配置按以下优先级从高到低深度合并:

  1. 命令行参数 / 编程式传入的 configOptions(最高优先级)
  2. CI 环境自动检测(通过 ci-info 自动设置 ci: true,可用 --no-ci 覆盖)
  3. 本地配置文件(含 extends 继承的配置)
  4. 内置默认配置(最低优先级)

需要特别注意的合并行为 ​

  • 数组整体覆盖,不逐项合并: 如果配置了数组类型的选项,会直接替换默认数组,不会保留默认值中的剩余元素。 例如只配置了 git.pushArgs: ['--force-with-lease'],不会自动补上默认的 --follow-tags;配置空数组 [] 会保持为空。 同样,changelog.preset.types 只写几项就只保留那几项(同时保留 preset.name: 'conventionalcommits')。
  • 显式 null 会保留:例如 changelog.infile: null 不会被默认值 'CHANGELOG.md' 替换。
  • undefined 不会覆盖低优先级值:命令行或编程式调用中未传入的项不会冲掉本地配置。
  • 整体禁用内置插件:将插件设为 false(如 git: false、npm: false、github: false、gitlab: false)会保持为 false,不会展开成默认对象。
  • pr 布尔简写与对象深度合并:在配置文件写了 pr: { draft: true, branch: 'rel/v${version}' } 后,命令行再传 --pr 会被规范化为 { pr: { enabled: true } } 并与本地 pr 对象合并,不会丢失其它字段。

通过命令行覆盖配置 ​

任何嵌套配置项都可以用点号路径(.)在命令行直接覆盖:

bash
release --git.commitMessage="release: v\${version}" --npm.publish --github.release

使用 --no- 前缀可将布尔选项设为 false 或禁用整个内置插件:

bash
release --no-git.push
release --no-npm
release --no-npm.checkFiles
release --no-git.requireCleanWorkingDir

注意大小写:CLI 参数解析关闭了 camel-case-expansion,因此嵌套配置项必须与配置对象中的精确驼峰拼写一致(例如 --git.requireUpToDate、--npm.checkBreakingChanges、--github.autoGenerate,而不是 --git.require-up-to-date)。

模板变量插值 ​

@neosjs/release 使用 Eta 模板引擎渲染 ${...} 占位符,适用于 git.commitMessage、git.tagName、git.tagAnnotation、git.logCommand、github.releaseName、github.assets、github.comments.*、gitlab.releaseName、gitlab.milestones、gitlab.assets、pr.branch、pr.title、pr.body 以及所有 hooks 脚本命令。

安全提示:${...} 里写的是会被求值的 JavaScript 表达式,而不只是占位符;hooks、git.logCommand 与字符串形式的 releaseNotes 的命令最终交给系统 Shell 执行。模板与命令只应来自你自己维护的配置,不要用环境变量或外部输入拼接而成。模板渲染失败时只输出写错的模板与原因,不输出上下文。这几处命令模板里,变量的值带有 Shell 语法(空白、引号、$、;、|、& 等)时会拒绝执行,多行文本(如更新日志)请改用下文的环境变量,详见生命周期钩子 - 模板变量的值与 Shell。

常用上下文变量如下:

变量说明示例值
${version}本次发版的目标版本号1.4.0
${latestVersion}发版前的最新版本号1.3.3
${name}项目名称(优先取 package.json 的 name,否则取 Git 仓库名)@neosjs/release
${tagName}本次发版的 Git 标签名v1.4.0
${latestTag}上一次发版的 Git 标签名v1.3.3
${secondLatestTag}倒数第二个 Git 标签名(--no-increment 时解析)v1.3.2
${branchName}当前 Git 分支名main
${remoteUrl}解析后的 Git 远程 URLgit@github.com:NeosJS/release-core.git
${repo.protocol}远程协议(https / ssh / file)ssh
${repo.host}远程主机名(ssh.github.com 会规范为 github.com)github.com
${repo.owner}仓库所有者 / 组织名NeosJS
${repo.project}仓库项目名release-core
${repo.repository}owner/project 完整仓库路径NeosJS/release-core
${repo.remote}规范化后的远程仓库地址https://github.com/NeosJS/release-core
${changelog}本次生成的更新日志正文### ✨ Features ...
${isPreRelease}目标版本是否为预发布版本false
${preReleaseId}预发布标识(如 beta,非预发布时为 null)beta
${npm.name}package.json 中的包名@neosjs/release
${npm.tag}发布到 npm 使用的 dist-taglatest
${releaseName}托管平台 Release 标题(在 GitHub/GitLab 评论等上下文可用)Release 1.4.0
${releaseUrl}已创建的 GitHub / GitLab Release 链接(在 afterRelease 与评论上下文可用)https://github.com/.../releases/tag/v1.4.0
${isReleased}是否已创建 GitHub / GitLab Releasetrue
${discussionUrl}GitHub Release 关联的 Discussion 链接(没有时为 undefined)https://github.com/.../discussions/12
${prBranch}Release-via-PR 模式下生成的发版分支名release/v1.4.0
${baseBranch}Release-via-PR 模式下的目标合并分支main
${originalBranch}Release-via-PR 模式下发版前所在的分支main
${prUrl}Release-via-PR 模式创建/更新后的 PR 或 MR 链接(after:pr:release / after:release 可用)https://github.com/.../pull/26
${prAction}PR / MR 的处理结果:created、updated,或 web(改为在浏览器打开)created
${prMethod}创建 PR / MR 的方式:gh(gh CLI)、api 或 webapi

在命令模板里用环境变量 ​

hooks、git.logCommand 与字符串形式的 releaseNotes 最终交给系统 Shell 执行,而 ${branchName}、${latestTag}、${changelog} 这些值来自仓库本身。渲染进命令的值因此不能带有 Shell 语法(否则会被拒绝执行),多行文本或带特殊字符的内容请改用环境变量:上表变量(${releaseName} 除外)同时以 RELEASE_ 前缀的下划线命名环境变量传给命令,在命令里给变量名加上引号即可原样读取。

模板变量环境变量
${version}$RELEASE_VERSION
${branchName}$RELEASE_BRANCH_NAME
${repo.owner}$RELEASE_REPO_OWNER
${changelog}$RELEASE_CHANGELOG
ts
export default defineConfig({
  hooks: {
    // 更新日志是多行文本: 用环境变量, 不用 ${changelog}
    'after:bump': 'printf "%s" "$RELEASE_CHANGELOG" > release-notes.md'
  }
})

配置项不在这份环境变量里(其中可能有 npm.otp 这样的敏感值),只有发版过程产生的值才会传给命令。

顶层选项 ​

选项类型默认值说明
lang'auto' | 'zh-CN' | 'en''auto'界面语言:'auto' 按环境变量 NEOS_RELEASE_LANG → 系统 locale(LC_ALL / LC_MESSAGES / LANG / Intl)自动识别,中文环境显示中文,其它显示英文

各模块配置详表 ​

完整默认配置 ​

以下为 @neosjs/release 的全部内置默认值(与源码 src/defaultConfig.ts 保持一致):

ts
export default {
  lang: 'auto',
  hooks: {},
  plugins: {},
  changelog: {
    generate: true,
    header: '# Changelog\n',
    infile: 'CHANGELOG.md',
    ignoreRecommendedBump: false,
    requireReleasableCommits: true,
    contributors: true,
    contributorsLookup: true,
    contributorsEmail: false,
    preset: {
      name: 'conventionalcommits',
      types: [
        { section: '✨ Features', type: 'feat', release: 'minor' },
        { section: '🐛 Bug Fixes', type: 'fix', release: 'patch' },
        { section: '🔥 Performance Improvements', type: 'perf', release: 'patch' },
        { section: '🔙 Reverts', type: 'revert', release: 'patch' },
        { section: '📚 Documentation', type: 'docs', hidden: true },
        { section: '💅 Code Refactoring', type: 'refactor', hidden: true },
        { section: '🎨 Code Style Changes', type: 'style', hidden: true },
        { section: '✅ Tests', type: 'test', hidden: true },
        { section: '🏗️ Build Changes', type: 'build', hidden: true },
        { section: '🤖 Continuous Integration', type: 'ci', hidden: true },
        { section: '🏡 Chores', type: 'chore', hidden: true }
      ]
    }
  },
  git: {
    logCommand: 'git log --pretty=format:"* %s (%h)" ${from}...${to}',
    requireCleanWorkingDir: true,
    requireBranch: false,
    requireUpstream: true,
    requireUpToDate: true,
    requireCommits: false,
    requireCommitsFail: true,
    commitsPath: '',
    addUntrackedFiles: false,
    commit: true,
    commitMessage: 'chore: release v${version}',
    commitArgs: [],
    tag: true,
    tagExclude: null,
    tagName: 'v${version}',
    tagMatch: null,
    getLatestTagFromAllRefs: false,
    tagAnnotation: 'Release v${version}',
    tagArgs: [],
    push: true,
    pushArgs: ['--follow-tags'],
    pushRepo: ''
  },
  npm: {
    publish: false,
    publishPath: '.',
    publishArgs: [],
    publishPackageManager: 'npm',
    tag: null,
    stage: false,
    otp: null,
    ignoreVersion: false,
    allowSameVersion: false,
    versionArgs: [],
    skipChecks: false,
    checkFiles: true,
    checkBreakingChanges: true,
    strictChecks: false,
    timeout: 10
  },
  github: {
    release: false,
    releaseName: 'Release v${version}',
    releaseNotes: null,
    autoGenerate: false,
    preRelease: false,
    draft: false,
    update: false,
    tokenRef: 'GITHUB_TOKEN',
    assets: null,
    host: null,
    timeout: 0,
    proxy: null,
    makeLatest: true,
    skipChecks: false,
    web: false,
    comments: {
      submit: false,
      issue: ':rocket: _This issue has been resolved in ${tagName}. See [${releaseName}](${releaseUrl}) for release notes._',
      pr: ':rocket: _This pull request is included in ${tagName}. See [${releaseName}](${releaseUrl}) for release notes._'
    }
  },
  gitlab: {
    release: false,
    releaseName: 'Release v${version}',
    releaseNotes: null,
    milestones: [],
    update: false,
    tokenRef: 'GITLAB_TOKEN',
    tokenHeader: 'Private-Token',
    certificateAuthorityFile: null,
    certificateAuthorityFileRef: 'CI_SERVER_TLS_CA_FILE',
    secure: true,
    proxy: false,
    assets: null,
    useIdsForUrls: false,
    useGenericPackages: false,
    genericPackageName: '',
    origin: null,
    skipChecks: false,
    repoId: null
  },
  pr: {
    enabled: false,
    branch: 'release/v${version}',
    base: '',
    title: 'chore: release v${version}',
    body: null,
    draft: false,
    forcePush: true
  }
}