-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocs-build.js
More file actions
102 lines (89 loc) · 2.42 KB
/
docs-build.js
File metadata and controls
102 lines (89 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* 文档构建脚本
* 1.移除根目录 docs 文件夹
* 2.生成 CHANGELOG
* 3.用 typedoc 生成文档docs 文件夹
* 4.用 pagefind 生成搜索
* 5.构建和复制 guide 文件夹到 docs 文件夹
*/
const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
// 定义命令列表
const cmds = [
{
name: '[1/5] 生成日志文件 CHANGELOG.md',
cmd: 'conventional-changelog -p angular -i CHANGELOG.md -s -r 0',
required: true
},
{
name: '[2/5] 生成 API 文档',
cmd: 'typedoc --searchInComments',
required: true
},
{
name: '[3/5] 生成文档内详细搜索',
cmd: 'pagefind --source docs',
required: true
},
{
name: '[4/5] 构建 guide 对象搜索',
cmd: 'pnpm --filter app2 build',
required: true
},
{
name: '[5/5] 复制 guide',
cmd: 'xcopy demos\\app2\\guide\\ docs\\guide\\ /s /e /y',
required: true
}
];
// 日志输出函数
function log(message, type = 'info') {
const colors = {
info: '\x1b[36m', // 青色
success: '\x1b[32m', // 绿色
error: '\x1b[31m', // 红色
warn: '\x1b[33m' // 黄色
};
const reset = '\x1b[0m';
console.log(`${colors[type]}[${type.toUpperCase()}]${reset} ${message}`);
}
// 删除文件夹
function removeDir(dirPath) {
if (fs.existsSync(dirPath)) {
fs.rmSync(dirPath, { recursive: true, force: true });
} else {
log(`目录不存在,跳过删除: ${dirPath}`, 'warn');
}
}
// 主流程
function buildDocs() {
log('开始构建文档', 'info');
// 移除 docs 文件夹
const docsPath = path.join(__dirname, 'docs');
removeDir(docsPath);
// 执行命令列表
for (const { name, cmd, required } of cmds) {
try {
log(name);
execSync(cmd, { stdio: 'inherit', shell: true });
} catch (error) {
console.error(error);
if (required) {
log(`${name} 失败`, 'error');
process.exit(1);
} else {
log(`${name} 失败,继续执行`, 'warn');
}
}
}
log('文档构建完成', 'success');
}
// 执行构建
try {
buildDocs();
} catch (error) {
log('构建过程发生错误', 'error');
log(error.stack, 'error');
process.exit(1);
}