Skip to content

Commit 099ad8d

Browse files
author
ws-wangjg
committed
doc: doc & test
1 parent 81a5679 commit 099ad8d

2 files changed

Lines changed: 151 additions & 0 deletions

File tree

docs/PUBLISHING.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# 发布与部署指南
2+
3+
本文档记录了 Baize CLI 的本地调试、构建及发布到 npm 的完整流程。
4+
5+
## 1. 本地开发与调试
6+
7+
在发布之前,建议在本地进行全局安装测试,以确保 CLI 工具运行正常。
8+
9+
### 方法 A: 使用 `npm link` (推荐)
10+
11+
`npm link` 会在全局 `node_modules` 中创建一个指向本地项目的软链接。任何代码修改都会立即生效,无需重新安装。
12+
13+
```bash
14+
# 1. 在项目根目录下建立链接
15+
npm link
16+
17+
# 2. 验证命令是否生效
18+
baize -v
19+
20+
# 3. 在任意目录下测试创建项目
21+
cd ~/Desktop
22+
baize init
23+
```
24+
25+
**取消链接:**
26+
```bash
27+
npm unlink -g baize-cli
28+
```
29+
30+
### 方法 B: 本地全局安装
31+
32+
模拟真实用户的安装过程,将当前目录打包并安装到全局。
33+
34+
```bash
35+
npm install -g .
36+
```
37+
38+
---
39+
40+
## 2. 发布前检查
41+
42+
在发布新版本前,请确保代码质量符合规范。
43+
44+
```bash
45+
# 1. 运行代码风格检查
46+
npm run lint
47+
48+
# 2. 运行单元测试
49+
npm run test
50+
```
51+
52+
---
53+
54+
## 3. 构建与发布到 npm
55+
56+
### 前置条件
57+
58+
1. 拥有 [npm](https://www.npmjs.com/) 账号。
59+
2. 在终端登录 npm:
60+
```bash
61+
npm login
62+
# 按提示输入用户名、密码、邮箱和 OTP
63+
```
64+
3. 确保 `package.json` 中的 `name` 字段在 npm 上是唯一的(未被占用)。
65+
66+
### 发布流程
67+
68+
本项目使用 `standard-version` 自动化管理版本号和变更日志。
69+
70+
#### 步骤 1: 生成新版本
71+
72+
根据修改内容的类型(Patch/Minor/Major),运行相应的命令:
73+
74+
```bash
75+
# 发布补丁版本 (例如: 1.0.0 -> 1.0.1) - 用于 bug 修复
76+
npm run release
77+
78+
# 发布次版本 (例如: 1.0.0 -> 1.1.0) - 用于新功能
79+
npm run release -- --release-as minor
80+
81+
# 发布主版本 (例如: 1.0.0 -> 2.0.0) - 用于破坏性更新
82+
npm run release -- --release-as major
83+
```
84+
85+
此命令会自动执行以下操作:
86+
1. 更新 `package.json` 中的版本号。
87+
2. 更新 `CHANGELOG.md`
88+
3. 提交代码并打上 Git Tag。
89+
90+
#### 步骤 2: 推送代码到仓库
91+
92+
将代码和标签推送到 GitHub:
93+
94+
```bash
95+
git push --follow-tags origin main
96+
```
97+
98+
#### 步骤 3: 发布到 npm
99+
100+
```bash
101+
npm publish
102+
```
103+
104+
> **注意**:如果是首次发布 Scoped Package (例如 `@wkylin/baize-cli`),需要添加访问参数:
105+
> ```bash
106+
> npm publish --access public
107+
> ```
108+
109+
---
110+
111+
## 4. 常用命令速查
112+
113+
| 命令 | 说明 |
114+
|Data | Description |
115+
| `npm link` | 本地软链调试 |
116+
| `npm run lint` | 代码检查 |
117+
| `npm run test` | 运行测试 |
118+
| `npm run release` | 生成新版本 (Patch) |
119+
| `npm publish` | 发布到 npm |

test/utils/questions.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { jest } from '@jest/globals';
2+
3+
jest.unstable_mockModule('fs', () => ({
4+
default: {
5+
existsSync: jest.fn(),
6+
},
7+
existsSync: jest.fn(),
8+
}));
9+
10+
const fs = await import('fs');
11+
const questions = (await import('../../src/utils/questions.js')).default;
12+
13+
describe('Questions Utils', () => {
14+
const projectNameQuestion = questions.find(q => q.name === 'projectName');
15+
16+
test('should validate valid project name', () => {
17+
fs.default.existsSync.mockReturnValue(false);
18+
const result = projectNameQuestion.validate('my-project');
19+
expect(result).toBe(true);
20+
});
21+
22+
test('should reject invalid project name', () => {
23+
const result = projectNameQuestion.validate('my project');
24+
expect(result).toBe('Project name may only include letters, numbers, underscores and hashes.');
25+
});
26+
27+
test('should reject existing directory', () => {
28+
fs.default.existsSync.mockReturnValue(true);
29+
const result = projectNameQuestion.validate('existing-project');
30+
expect(result).toBe('The directory already exists. Please choose another name.');
31+
});
32+
});

0 commit comments

Comments
 (0)