@spaceflow/publish 0.21.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +503 -0
- package/README.md +228 -0
- package/dist/index.js +1160 -0
- package/package.json +40 -0
- package/src/index.ts +30 -0
- package/src/locales/en/publish.json +8 -0
- package/src/locales/index.ts +11 -0
- package/src/locales/zh-cn/publish.json +8 -0
- package/src/monorepo.service.ts +376 -0
- package/src/publish.command.ts +71 -0
- package/src/publish.config.ts +90 -0
- package/src/publish.module.ts +12 -0
- package/src/publish.service.ts +602 -0
- package/tsconfig.json +5 -0
package/README.md
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
# @spaceflow/publish
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@spaceflow/publish)
|
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
|
5
|
+
|
|
6
|
+
> Spaceflow 自动化发布扩展,基于 [release-it](https://github.com/release-it/release-it) 实现版本管理与发布。
|
|
7
|
+
|
|
8
|
+
## 功能特性
|
|
9
|
+
|
|
10
|
+
- **Monorepo 支持** — 自动检测变更包,按依赖拓扑顺序发布
|
|
11
|
+
- **分支保护** — 发布期间自动锁定分支,防止其他推送干扰
|
|
12
|
+
- **Conventional Changelog** — 基于 conventional commits 自动生成 CHANGELOG
|
|
13
|
+
- **GitHub Release** — 自动创建 GitHub Release 并上传资产文件
|
|
14
|
+
- **预发布支持** — 支持 rc、beta、alpha 等预发布版本
|
|
15
|
+
- **进程退出保护** — 即使发布失败也能自动解锁分支
|
|
16
|
+
|
|
17
|
+
## 安装
|
|
18
|
+
|
|
19
|
+
`@spaceflow/publish` 是 `@spaceflow/cli` 的内置命令,安装 CLI 后即可使用:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
# 使用 pnpm
|
|
23
|
+
pnpm add -D @spaceflow/cli
|
|
24
|
+
|
|
25
|
+
pnpm spaceflow install @spaceflow/publish
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
然后在项目根目录创建 `spaceflow.json` 配置文件:
|
|
29
|
+
|
|
30
|
+
```json
|
|
31
|
+
{
|
|
32
|
+
"publish": {
|
|
33
|
+
"monorepo": {
|
|
34
|
+
"enabled": true
|
|
35
|
+
},
|
|
36
|
+
"git": {
|
|
37
|
+
"lockBranch": true,
|
|
38
|
+
"pushWhitelistUsernames": ["github-actions[bot]"]
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## 命令行参数
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
# 发布
|
|
48
|
+
spaceflow publish [options]
|
|
49
|
+
|
|
50
|
+
# 预演
|
|
51
|
+
spaceflow publish --rehearsal
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
| 参数 | 说明 |
|
|
55
|
+
| ------------------------ | ----------------------------------------------- |
|
|
56
|
+
| `-d, --dry-run` | 仅打印将要执行的操作,不实际执行 |
|
|
57
|
+
| `-c, --ci` | 在 CI 环境中运行(自动 fetch tags) |
|
|
58
|
+
| `-p, --prerelease <tag>` | 预发布标签,如 `rc`、`beta`、`alpha`、`nightly` |
|
|
59
|
+
| `-r, --rehearsal` | 预演模式:执行 hooks 但不修改文件/git |
|
|
60
|
+
|
|
61
|
+
## 配置
|
|
62
|
+
|
|
63
|
+
在 `spaceflow.json` 中配置 `publish` 字段:
|
|
64
|
+
|
|
65
|
+
```json
|
|
66
|
+
{
|
|
67
|
+
"publish": {
|
|
68
|
+
"monorepo": {
|
|
69
|
+
"enabled": true,
|
|
70
|
+
"propagateDeps": true
|
|
71
|
+
},
|
|
72
|
+
"changelog": {
|
|
73
|
+
"preset": {
|
|
74
|
+
"type": [
|
|
75
|
+
{ "type": "feat", "section": "新功能" },
|
|
76
|
+
{ "type": "fix", "section": "Bug 修复" },
|
|
77
|
+
{ "type": "refactor", "section": "代码重构" },
|
|
78
|
+
{ "type": "perf", "section": "性能优化" },
|
|
79
|
+
{ "type": "docs", "section": "文档更新" }
|
|
80
|
+
]
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
"npm": {
|
|
84
|
+
"publish": true,
|
|
85
|
+
"packageManager": "pnpm",
|
|
86
|
+
"registry": "https://registry.npmjs.org",
|
|
87
|
+
"tag": "latest",
|
|
88
|
+
"ignoreVersion": true,
|
|
89
|
+
"versionArgs": ["--workspaces false"]
|
|
90
|
+
},
|
|
91
|
+
"release": {
|
|
92
|
+
"host": "https://github.com",
|
|
93
|
+
"assets": [{ "path": "dist/*.zip", "name": "dist.zip", "type": "zip" }]
|
|
94
|
+
},
|
|
95
|
+
"git": {
|
|
96
|
+
"requireBranch": ["main", "dev", "develop"],
|
|
97
|
+
"lockBranch": true,
|
|
98
|
+
"pushWhitelistUsernames": ["github-actions[bot]"]
|
|
99
|
+
},
|
|
100
|
+
"hooks": {
|
|
101
|
+
"before:bump": "echo 'Before bump'",
|
|
102
|
+
"after:bump": ["pnpm build", "pnpm test"]
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
## 配置项说明
|
|
109
|
+
|
|
110
|
+
### monorepo
|
|
111
|
+
|
|
112
|
+
| 配置项 | 类型 | 默认值 | 说明 |
|
|
113
|
+
| --------------- | --------- | ------- | -------------------------------- |
|
|
114
|
+
| `enabled` | `boolean` | `false` | 是否启用 monorepo 发布模式 |
|
|
115
|
+
| `propagateDeps` | `boolean` | `true` | 依赖的包变更时,依赖方是否也发布 |
|
|
116
|
+
|
|
117
|
+
### changelog
|
|
118
|
+
|
|
119
|
+
| 配置项 | 类型 | 默认值 | 说明 |
|
|
120
|
+
| ------------- | -------- | ----------------------- | ----------------------------- |
|
|
121
|
+
| `infileDir` | `string` | `"."` | CHANGELOG 文件输出目录 |
|
|
122
|
+
| `preset.name` | `string` | `"conventionalcommits"` | Changelog preset 名称 |
|
|
123
|
+
| `preset.type` | `array` | `[]` | Commit type 到 section 的映射 |
|
|
124
|
+
|
|
125
|
+
### npm
|
|
126
|
+
|
|
127
|
+
| 配置项 | 类型 | 默认值 | 说明 |
|
|
128
|
+
| ---------------- | ----------------- | ------------------------ | -------------------------------- |
|
|
129
|
+
| `publish` | `boolean` | `false` | 是否发布到 npm registry |
|
|
130
|
+
| `packageManager` | `"npm" \| "pnpm"` | `"npm"` | 包管理器 |
|
|
131
|
+
| `registry` | `string` | - | npm registry 地址 |
|
|
132
|
+
| `tag` | `string` | `"latest"` | npm tag |
|
|
133
|
+
| `ignoreVersion` | `boolean` | `true` | 是否忽略 package.json 中的版本号 |
|
|
134
|
+
| `versionArgs` | `string[]` | `["--workspaces false"]` | npm version 命令额外参数 |
|
|
135
|
+
| `publishArgs` | `string[]` | `[]` | npm/pnpm publish 命令额外参数 |
|
|
136
|
+
|
|
137
|
+
### release
|
|
138
|
+
|
|
139
|
+
| 配置项 | 类型 | 默认值 | 说明 |
|
|
140
|
+
| ---------------- | -------- | ------------- | -------------------- |
|
|
141
|
+
| `host` | `string` | `"localhost"` | Git 服务器地址 |
|
|
142
|
+
| `assets` | `array` | `[]` | Release 资产文件列表 |
|
|
143
|
+
| `assetSourcemap` | `object` | - | Sourcemap 资产配置 |
|
|
144
|
+
|
|
145
|
+
### git
|
|
146
|
+
|
|
147
|
+
| 配置项 | 类型 | 默认值 | 说明 |
|
|
148
|
+
| ------------------------ | ---------- | ---------------------------- | ------------------------ |
|
|
149
|
+
| `requireBranch` | `string[]` | `["main", "dev", "develop"]` | 允许发布的分支列表 |
|
|
150
|
+
| `lockBranch` | `boolean` | `true` | 发布时是否锁定分支 |
|
|
151
|
+
| `pushWhitelistUsernames` | `string[]` | `[]` | 锁定期间允许推送的用户名 |
|
|
152
|
+
|
|
153
|
+
### hooks
|
|
154
|
+
|
|
155
|
+
支持 release-it 的所有 hooks,如:
|
|
156
|
+
|
|
157
|
+
- `before:init` / `after:init`
|
|
158
|
+
- `before:bump` / `after:bump`
|
|
159
|
+
- `before:release` / `after:release`
|
|
160
|
+
|
|
161
|
+
## 环境变量
|
|
162
|
+
|
|
163
|
+
| 变量 | 说明 |
|
|
164
|
+
| ------------------- | ----------------------------------------- |
|
|
165
|
+
| `GITHUB_TOKEN` | GitHub API Token |
|
|
166
|
+
| `GITHUB_REPOSITORY` | 仓库名称(格式:`owner/repo`) |
|
|
167
|
+
| `GITHUB_REF_NAME` | 当前分支名称 |
|
|
168
|
+
| `PUBLISH_REHEARSAL` | 预演模式标志(由 `--rehearsal` 自动设置) |
|
|
169
|
+
|
|
170
|
+
## CI 工作流示例
|
|
171
|
+
|
|
172
|
+
```yaml
|
|
173
|
+
name: Publish
|
|
174
|
+
|
|
175
|
+
on:
|
|
176
|
+
push:
|
|
177
|
+
branches: [main]
|
|
178
|
+
|
|
179
|
+
jobs:
|
|
180
|
+
publish:
|
|
181
|
+
runs-on: ubuntu-latest
|
|
182
|
+
steps:
|
|
183
|
+
- uses: actions/checkout@v4
|
|
184
|
+
with:
|
|
185
|
+
fetch-depth: 0
|
|
186
|
+
fetch-tags: true
|
|
187
|
+
|
|
188
|
+
- uses: pnpm/action-setup@v4
|
|
189
|
+
|
|
190
|
+
- run: pnpm install --frozen-lockfile
|
|
191
|
+
- run: pnpm build
|
|
192
|
+
- run: pnpm spaceflow publish --ci
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
## 版本计算
|
|
196
|
+
|
|
197
|
+
版本号基于 [Conventional Commits](https://www.conventionalcommits.org/) 自动计算:
|
|
198
|
+
|
|
199
|
+
| Commit 类型 | 版本变更 |
|
|
200
|
+
| ----------------- | ------------- |
|
|
201
|
+
| `feat` | Minor (0.x.0) |
|
|
202
|
+
| `fix` | Patch (0.0.x) |
|
|
203
|
+
| `BREAKING CHANGE` | Major (x.0.0) |
|
|
204
|
+
| 其他 | Patch (0.0.x) |
|
|
205
|
+
|
|
206
|
+
## Monorepo 模式
|
|
207
|
+
|
|
208
|
+
启用 `monorepo.enabled: true` 后:
|
|
209
|
+
|
|
210
|
+
1. **变更检测**:基于 git diff 检测哪些包有变更
|
|
211
|
+
2. **依赖传递**:如果包 A 依赖包 B,且 B 有变更,A 也会被标记为需要发布
|
|
212
|
+
3. **拓扑排序**:被依赖的包先发布,确保依赖关系正确
|
|
213
|
+
4. **独立版本**:每个包有独立的版本号和 CHANGELOG
|
|
214
|
+
5. **Tag 格式**:`@scope/package@version`(如 `@spaceflow/cli@1.0.0`)
|
|
215
|
+
|
|
216
|
+
## 分支保护机制
|
|
217
|
+
|
|
218
|
+
发布期间会自动锁定分支,防止其他推送干扰发布流程:
|
|
219
|
+
|
|
220
|
+
1. **锁定**:创建分支保护规则,仅允许白名单用户推送
|
|
221
|
+
2. **发布**:执行版本更新、CHANGELOG 生成、git tag、npm publish
|
|
222
|
+
3. **解锁**:删除分支保护规则,恢复正常状态
|
|
223
|
+
|
|
224
|
+
即使发布过程中发生错误,也会通过 `process.on('exit')` 确保分支被解锁。
|
|
225
|
+
|
|
226
|
+
## 许可证
|
|
227
|
+
|
|
228
|
+
[MIT](../../LICENSE)
|