@ruan-cat/vercel-deploy-tool 0.12.2 → 1.0.0

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/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # 阮喵喵自用的 vercel 部署工具
1
+ # 阮喵喵自用的 Vercel 部署工具
2
2
 
3
3
  <!-- automd:badges color="yellow" name="@ruan-cat/vercel-deploy-tool" -->
4
4
 
@@ -7,137 +7,423 @@
7
7
 
8
8
  <!-- /automd -->
9
9
 
10
- 生成满足 [Vercel Output API (v3)](https://vercel.com/docs/build-output-api)规范的目录结构,并推送到 vercel 平台内。
10
+ 一个功能完善的 Vercel 部署工具,支持 **CLI 命令行** 和 **API 编程式** 两种使用方式。
11
11
 
12
- 目前仅考虑简单的静态页面,没有实现云函数等功能。
12
+ 生成满足 [Vercel Output API (v3)](https://vercel.com/docs/build-output-api) 规范的目录结构,并自动部署到 Vercel 平台。
13
13
 
14
- ## 设计初衷
14
+ ## ✨ 特性
15
15
 
16
- - 优化冗长的 github action 写法。
17
- - 同时支持 monorepo 和单体项目的部署。
18
- - 自动实现文件移动,避免用户自写文件移动命令。
19
- - 实现复杂部署任务的并列执行,提高运行性能。
20
- - 配置实现类型提示,对用户友好。
21
- - 实现单一 vercel 项目的多项目部署,绕开 vercel 针对 monorepo 项目的部署限制。
16
+ - 🚀 **CLI 工具**:提供 `deploy` 和 `init` 命令,开箱即用
17
+ - 📦 **API 导出**:支持编程式调用,灵活集成到自定义工作流
18
+ - 🏗️ **Monorepo 支持**:完美支持 monorepo 和单体项目
19
+ - ⚡ **并行执行**:使用 [tasuku](https://github.com/privatenumber/tasuku) 实现任务可视化和并行调度
20
+ - 🎯 **类型安全**:导出 `defineConfig` 提供完整的 TypeScript 类型提示
21
+ - 🔧 **灵活配置**:基于 [c12](https://github.com/unjs/c12) 支持多种配置文件格式
22
+ - 🎨 **多命令别名**:支持 `vercel-deploy-tool`、`vdt`、`@ruan-cat/vercel-deploy-tool`
22
23
 
23
- ## 安装
24
+ ## 📦 安装
24
25
 
25
26
  ```bash
26
- pnpm i -D @ruan-cat/vercel-deploy-tool@latest
27
+ pnpm add -D @ruan-cat/vercel-deploy-tool
27
28
  ```
28
29
 
29
- ## 环境要求
30
+ ## 🔧 环境要求
30
31
 
31
- - node >=20.15.1
32
- - pnpm >=9
32
+ - Node.js >= 18
33
+ - pnpm >= 9 (推荐)
33
34
 
34
- ## 使用教程
35
+ ## 🚀 快速开始
35
36
 
36
- ### 增加 .gitignore 配置
37
+ ### 方式一:使用 CLI(推荐)
37
38
 
38
- 本工具会在根目录内默认生成一个全空配置的 vercel.null.def.json 文件,这个文件应该被忽略。
39
+ #### 1. 初始化配置
39
40
 
40
- 后续的使用会不可避免的使用 vercel 的 api,会在你的项目内生成一个或多个.vercel 文件夹,故.vercel 文件夹也应该被忽略。
41
+ ```bash
42
+ npx vercel-deploy-tool init
43
+ ```
44
+
45
+ 这将在项目根目录生成 `vercel-deploy-tool.config.ts` 配置文件,并自动更新 `package.json` 添加部署脚本。
46
+
47
+ #### 2. 配置 Vercel 凭据
48
+
49
+ 获取 Vercel 项目凭据(使用 `vc link` 命令):
41
50
 
42
51
  ```bash
43
- # 忽略vercel本地打包生成的文件
44
- .vercel
45
- # 忽略自动生成vercel部署配置文件
46
- vercel.null.def.json
52
+ npx vercel link
47
53
  ```
48
54
 
49
- 如果你使用环境变量文件,推荐你加上 dotenv 推荐的环境变量文件忽略。
55
+ 将凭据添加到环境变量(推荐)或配置文件:
50
56
 
51
57
  ```bash
52
- # dotenv environment variable files
53
- .env
54
- .env.development.local
55
- .env.test.local
56
- .env.production.local
57
- .env.local
58
+ # .env
59
+ VERCEL_TOKEN=your_vercel_token
60
+ VERCEL_ORG_ID=team_your_vercel_orgId
61
+ VERCEL_PROJECT_ID=prj_your_vercel_projectId
58
62
  ```
59
63
 
60
- ### 获取 vercel 提供的 id
64
+ #### 3. 编辑配置文件
61
65
 
62
- 用 [`vc link`](https://vercel.com/guides/how-can-i-use-github-actions-with-vercel) 命令获取 vercelOrgId 和 vercelProjectId。
66
+ ```typescript
67
+ // vercel-deploy-tool.config.ts
68
+ import { defineConfig } from "@ruan-cat/vercel-deploy-tool";
63
69
 
64
- 你可以明文地写在配置上面,也可以考虑放在环境变量,放在 github secrets 环境变量内。
70
+ export default defineConfig({
71
+ vercelProjectName: "my-awesome-project",
72
+ vercelToken: process.env.VERCEL_TOKEN || "",
73
+ vercelOrgId: process.env.VERCEL_ORG_ID || "",
74
+ vercelProjectId: process.env.VERCEL_PROJECT_ID || "",
65
75
 
66
- ### 放在项目内的 .env 环境变量文件内(可选)
76
+ deployTargets: [
77
+ {
78
+ type: "userCommands",
79
+ targetCWD: "./packages/docs",
80
+ url: ["docs.example.com"],
81
+ userCommands: ["pnpm build:docs"],
82
+ outputDirectory: "docs/.vitepress/dist",
83
+ isCopyDist: true, // 默认为 true
84
+ },
85
+ ],
67
86
 
68
- 举例如下:
87
+ // 可选:在所有构建完成后执行的全局任务
88
+ afterBuildTasks: [
89
+ // "echo 'All builds completed!'",
90
+ ],
91
+ });
92
+ ```
93
+
94
+ #### 4. 运行部署
69
95
 
70
96
  ```bash
71
- VERCEL_PROJECT_ID=prj_your_vercel_projectId
72
- VERCEL_ORG_ID=team_your_vercel_orgId
73
- VERCEL_TOKEN=your_vercel_token
97
+ pnpm run deploy-vercel
98
+ # 或直接使用 CLI
99
+ npx vercel-deploy-tool deploy
100
+ # 或使用短别名
101
+ npx vdt deploy
102
+ ```
103
+
104
+ ### 方式二:使用 API
105
+
106
+ 适用于需要在代码中编程式调用部署功能的场景。
107
+
108
+ ```typescript
109
+ import { defineConfig, executeDeploymentWorkflow } from "@ruan-cat/vercel-deploy-tool";
110
+
111
+ const config = defineConfig({
112
+ vercelProjectName: "my-project",
113
+ vercelToken: process.env.VERCEL_TOKEN || "",
114
+ vercelOrgId: process.env.VERCEL_ORG_ID || "",
115
+ vercelProjectId: process.env.VERCEL_PROJECT_ID || "",
116
+ deployTargets: [
117
+ {
118
+ type: "userCommands",
119
+ targetCWD: "./apps/web",
120
+ url: ["app.example.com"],
121
+ userCommands: ["pnpm build"],
122
+ outputDirectory: "dist",
123
+ },
124
+ ],
125
+ });
126
+
127
+ // 执行部署工作流
128
+ await executeDeploymentWorkflow(config);
74
129
  ```
75
130
 
76
- 环境变量必须写成大写,名称很严格。如果你使用环境变量的方式提供这些值,请务必使用例子提供的变量名。
131
+ ## 📝 配置说明
132
+
133
+ ### 主配置项
134
+
135
+ ```typescript
136
+ interface VercelDeployConfig {
137
+ /** Vercel 项目名称 */
138
+ vercelProjectName: string;
139
+
140
+ /** Vercel Token(推荐使用环境变量) */
141
+ vercelToken: string;
142
+
143
+ /** Vercel 组织 ID */
144
+ vercelOrgId: string;
77
145
 
78
- ### 编写部署配置文件
146
+ /** Vercel 项目 ID */
147
+ vercelProjectId: string;
79
148
 
80
- ```ts
81
- // .config/vercel-deploy-tool.ts
82
- import { type Config } from "@ruan-cat/vercel-deploy-tool/src/config.ts";
149
+ /** 可选:自定义 Vercel 配置文件路径 */
150
+ vercelJsonPath?: string;
151
+
152
+ /** 可选:在所有构建完成后执行的全局任务 */
153
+ afterBuildTasks?: string[];
154
+
155
+ /** 部署目标列表 */
156
+ deployTargets: DeployTarget[];
157
+ }
158
+ ```
159
+
160
+ ### 部署目标配置
161
+
162
+ #### 基础配置
163
+
164
+ ```typescript
165
+ interface DeployTargetBase {
166
+ /** 目标类型 */
167
+ type: "static" | "userCommands";
168
+
169
+ /** 目标工作目录(相对于项目根目录) */
170
+ targetCWD: `./${string}`;
171
+
172
+ /** 部署后的自定义域名列表 */
173
+ url: string[];
174
+
175
+ /** 是否需要执行 vercel build(默认 true) */
176
+ isNeedVercelBuild?: boolean;
177
+ }
178
+ ```
83
179
 
84
- const config: Config = {
85
- vercelProjetName: "prj_your_vercel_projectName",
86
- vercelOrgId: "team_your_vercel_orgId",
87
- vercelProjectId: "prj_your_vercel_projectId",
88
- // 默认留空即可
89
- vercelToken: "",
180
+ #### 用户命令配置
181
+
182
+ 当 `type: "userCommands"` 时,额外支持:
183
+
184
+ ```typescript
185
+ interface DeployTargetWithUserCommands extends DeployTargetBase {
186
+ type: "userCommands";
187
+
188
+ /** 构建命令列表(按顺序执行) */
189
+ userCommands: string[];
190
+
191
+ /** 构建产物目录 */
192
+ outputDirectory: string;
193
+
194
+ /** 是否复制构建产物到部署目录(默认 true) */
195
+ isCopyDist?: boolean;
196
+ }
197
+ ```
198
+
199
+ ### 配置示例
200
+
201
+ #### Monorepo 多项目部署
202
+
203
+ ```typescript
204
+ import { defineConfig } from "@ruan-cat/vercel-deploy-tool";
205
+
206
+ export default defineConfig({
207
+ vercelProjectName: "my-monorepo",
208
+ vercelToken: process.env.VERCEL_TOKEN || "",
209
+ vercelOrgId: process.env.VERCEL_ORG_ID || "",
210
+ vercelProjectId: process.env.VERCEL_PROJECT_ID || "",
90
211
 
91
212
  deployTargets: [
213
+ // VitePress 文档站点
214
+ {
215
+ type: "userCommands",
216
+ targetCWD: "./packages/docs",
217
+ url: ["docs.example.com"],
218
+ userCommands: ["pnpm build:docs"],
219
+ outputDirectory: "docs/.vitepress/dist",
220
+ },
221
+
222
+ // VuePress 文档站点
92
223
  {
93
224
  type: "userCommands",
94
- outputDirectory: "docs/.vuepress/dist/**/*",
95
- targetCWD: "./",
96
- url: ["small-alice-web-dev.ruancat6312.top", "small-alice-web.ruan-cat.com"],
97
- userCommands: ["pnpm -C=./ vuepress-vite build docs"],
225
+ targetCWD: "./apps/blog",
226
+ url: ["blog.example.com"],
227
+ userCommands: ["pnpm build"],
228
+ outputDirectory: ".vuepress/dist",
229
+ },
230
+
231
+ // 静态站点(无需自定义构建命令)
232
+ {
233
+ type: "static",
234
+ targetCWD: "./apps/landing",
235
+ url: ["www.example.com"],
236
+ isNeedVercelBuild: true,
98
237
  },
99
238
  ],
100
- };
101
239
 
102
- export default config;
240
+ // 全局后置任务(在所有构建完成后执行)
241
+ afterBuildTasks: ["echo 'Deployment completed!'", "curl -X POST https://api.example.com/notify"],
242
+ });
103
243
  ```
104
244
 
105
- ### 编写运行文件
245
+ ## 🔄 部署工作流
106
246
 
107
- ```ts
108
- // bin/vercel-deploy-tool.ts
109
- // 执行部署任务
110
- import "@ruan-cat/vercel-deploy-tool/src/index.ts";
247
+ 工具会按以下顺序执行任务:
248
+
249
+ 1. **Link 阶段**(并行):将所有目标与 Vercel 项目关联
250
+ 2. **Build 阶段**(并行):执行所有需要构建的目标
251
+ 3. **AfterBuild 阶段**(串行):执行全局后置任务
252
+ 4. **UserCommands + CopyDist 阶段**(并行目标,串行步骤):
253
+ - 执行用户自定义构建命令
254
+ - 复制构建产物到部署目录
255
+ 5. **Deploy + Alias 阶段**(并行目标,串行步骤):
256
+ - 部署到 Vercel
257
+ - 设置自定义域名别名
258
+
259
+ ## 📋 .gitignore 配置
260
+
261
+ 添加以下内容到 `.gitignore`:
262
+
263
+ ```bash
264
+ # Vercel 本地文件
265
+ .vercel
266
+ vercel.null.def.json
267
+
268
+ # 环境变量文件(如果使用 .env)
269
+ .env
270
+ .env.local
271
+ .env.*.local
272
+ ```
273
+
274
+ ## 🎯 CLI 命令
275
+
276
+ ### `deploy`
277
+
278
+ 执行部署工作流:
279
+
280
+ ```bash
281
+ vercel-deploy-tool deploy
282
+ # 或
283
+ vdt deploy
284
+ # 或
285
+ @ruan-cat/vercel-deploy-tool deploy
286
+ ```
287
+
288
+ ### `init`
289
+
290
+ 初始化配置文件:
291
+
292
+ ```bash
293
+ vercel-deploy-tool init [options]
294
+
295
+ Options:
296
+ -f, --force 强制覆盖已存在的配置文件
297
+ ```
298
+
299
+ ## 📚 API 导出
300
+
301
+ ### 配置系统
302
+
303
+ ```typescript
304
+ import { defineConfig, loadConfig, getConfig } from "@ruan-cat/vercel-deploy-tool";
305
+
306
+ // 定义配置(提供类型提示)
307
+ export const config = defineConfig({
308
+ /* ... */
309
+ });
310
+
311
+ // 加载配置(异步工厂函数)
312
+ const config = await loadConfig();
313
+
314
+ // 获取配置(同步获取)
315
+ const config = getConfig();
111
316
  ```
112
317
 
113
- ### 封装运行命令
318
+ ### 类型定义
114
319
 
115
- 在 package.json 内提供命令
320
+ ```typescript
321
+ import type {
322
+ VercelDeployConfig,
323
+ DeployTarget,
324
+ DeployTargetBase,
325
+ DeployTargetWithUserCommands,
326
+ DeployTargetType,
327
+ } from "@ruan-cat/vercel-deploy-tool";
328
+ ```
329
+
330
+ ### 核心功能
331
+
332
+ ```typescript
333
+ import { executeDeploymentWorkflow } from "@ruan-cat/vercel-deploy-tool";
334
+
335
+ // 执行完整的部署工作流
336
+ await executeDeploymentWorkflow(config);
337
+ ```
338
+
339
+ ### 命令工厂(高级用法)
340
+
341
+ ```typescript
342
+ import { createDeployCommand, createInitCommand } from "@ruan-cat/vercel-deploy-tool";
343
+ import { Command } from "commander";
344
+
345
+ const program = new Command();
346
+ program.addCommand(createDeployCommand());
347
+ program.addCommand(createInitCommand());
348
+ program.parse();
349
+ ```
350
+
351
+ ## 🔧 环境变量
352
+
353
+ 工具会自动读取以下环境变量(优先级高于配置文件):
354
+
355
+ | 环境变量 | 说明 | 示例 |
356
+ | ------------------- | ---------------- | --------------------------- |
357
+ | `VERCEL_TOKEN` | Vercel API Token | `your_vercel_token` |
358
+ | `VERCEL_ORG_ID` | Vercel 组织 ID | `team_your_vercel_orgId` |
359
+ | `VERCEL_PROJECT_ID` | Vercel 项目 ID | `prj_your_vercel_projectId` |
360
+
361
+ 推荐使用 `.env` 文件管理环境变量(确保已添加到 `.gitignore`)。
362
+
363
+ ## 📖 从 v0.x 迁移到 v1.0
364
+
365
+ v1.0 是一个**破坏性更新**,请参考 [迁移指南](./src/docs/migration-guide.md) 了解详细的迁移步骤。
366
+
367
+ ### 主要变更
368
+
369
+ - ❌ 移除:直接运行 TypeScript 脚本的方式
370
+ - ✅ 新增:CLI 命令(`vercel-deploy-tool deploy`)
371
+ - ✅ 新增:`defineConfig` 类型安全配置
372
+ - ✅ 新增:`init` 命令生成配置模板
373
+ - 🔄 变更:配置字段重命名(`vercelProjetName` → `vercelProjectName`)
374
+ - 🔄 变更:API 导入路径
375
+
376
+ ### 快速迁移
377
+
378
+ **旧版本 (v0.x)**:
379
+
380
+ ```typescript
381
+ // bin/vercel-deploy-tool.ts
382
+ import "@ruan-cat/vercel-deploy-tool/src/index.ts";
383
+ ```
116
384
 
117
385
  ```json
386
+ // package.json
118
387
  {
119
- "engines": {
120
- "node": ">=22.6.0",
121
- "pnpm": ">=9"
122
- },
123
388
  "scripts": {
124
389
  "deploy-vercel": "tsx ./bin/vercel-deploy-tool.ts"
125
390
  }
126
391
  }
127
392
  ```
128
393
 
129
- 这里采用直接运行 typescript 文件的方案运行部署工具,你需要额外多安装 tsx 依赖:
394
+ **新版本 (v1.0)**:
130
395
 
131
396
  ```bash
132
- pnpm i -D tsx
397
+ # 初始化配置
398
+ npx vercel-deploy-tool init
399
+ ```
400
+
401
+ ```json
402
+ // package.json
403
+ {
404
+ "scripts": {
405
+ "deploy-vercel": "vercel-deploy-tool deploy"
406
+ }
407
+ }
133
408
  ```
134
409
 
135
- 运行 `pnpm run deploy-vercel` 命令就能部署项目到 vercel 了。
410
+ ## 🛠️ 设计初衷
411
+
412
+ - ✅ 优化冗长的 GitHub Actions 写法
413
+ - ✅ 同时支持 monorepo 和单体项目的部署
414
+ - ✅ 自动实现文件移动,避免用户手写文件操作命令
415
+ - ✅ 实现复杂部署任务的并行执行,提高运行性能
416
+ - ✅ 配置实现类型提示,对用户友好
417
+ - ✅ 实现单一 Vercel 项目的多目标部署,绕开 Vercel 针对 monorepo 的部署限制
418
+ - ✅ 提供 CLI 和 API 双模式,适应不同使用场景
419
+
420
+ ## 📜 许可证
421
+
422
+ ISC
136
423
 
137
- ## 路线图
424
+ ## 🔗 相关链接
138
425
 
139
- - [x] 封装打包命令,`vc deploy` 命令,并赋予生产环境 url。
140
- - [x] 拆分配置文件到项目根目录,并实现文件读取。
141
- - [x] github action 运行产物。
142
- - [x] github action 全局安装新开发的包,实现纯工作流的部署。
143
- - [x] 去其他项目,自主完成配置与部署。
426
+ - [Vercel CLI 文档](https://vercel.com/docs/cli)
427
+ - [Vercel Output API](https://vercel.com/docs/build-output-api)
428
+ - [tasuku - 任务执行器](https://github.com/privatenumber/tasuku)
429
+ - [c12 - 配置加载器](https://github.com/unjs/c12)
package/dist/cli.d.ts ADDED
@@ -0,0 +1 @@
1
+ #!/usr/bin/env node