-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcli.js
More file actions
113 lines (97 loc) · 4.2 KB
/
cli.js
File metadata and controls
113 lines (97 loc) · 4.2 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
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env node
import { Command } from "commander"
import { packageMetadata } from "./builder/utils/loadPackageMetadata.js"
import { isValidDirectory } from "./builder/utils/loadConfig.js"
import languageSelector from "./builder/utils/languageSelector.js"
/** @param {string[]} commandScripts */
function commandScriptRunner(...commandScripts) {
/**
* @description scripts that should runs in valid directory
* @type {string[]}
*/
const scriptBlackList = [
scripts.preview,
scripts.upgrade,
scripts.watch,
scripts.build,
scripts.ssr,
scripts.indexing,
scripts.count,
scripts.backup,
scripts.restore,
]
/** @param {string[]} args */
return async function(...args) {
if (!isValidDirectory) {
for (const script of commandScripts) {
const isBlockedScript = scriptBlackList.includes(script)
if (isBlockedScript) {
console.error(languageSelector(
"不合法的 BaSB 目录。",
"Not a valid BaSB directory.",
))
return
}
}
}
const moduleImportTasks = commandScripts.map(script => import(script))
const modules = await Promise.all(moduleImportTasks)
const moduleProcessTasks = modules.map(module => module.default(...args))
await Promise.all(moduleProcessTasks)
}
}
const program = new Command()
const scripts = {
create: "./builder/scripts/create.js",
upgrade: "./builder/scripts/upgrade.js",
preview: "./builder/scripts/preview.js",
watch: "./builder/scripts/watch.js",
build: "./builder/scripts/build.js",
ssr: "./builder/scripts/ssr/index.js",
indexing: "./builder/scripts/indexing/index.js",
count: "./builder/scripts/count/index.js",
backup: "./builder/scripts/backup.js",
restore: "./builder/scripts/restore.js",
mcp: "./builder/scripts/mcp.js",
}
// --- --- --- --- --- ---
program.name(packageMetadata.name)
.version(packageMetadata.version)
.description(packageMetadata.description)
program.command("create")
.description(languageSelector("创建 BaSB 项目", "Create BaSB repository"))
.argument("<name>", languageSelector("项目名称", "Repository name"))
.action(commandScriptRunner(scripts.create))
program.command("upgrade")
.description(languageSelector("升级 BaSB 项目", "Upgrade BaSB repository"))
.action(commandScriptRunner(scripts.upgrade))
program.command("preview")
.description(languageSelector("启动预览服务器", "Launch preview server"))
.action(commandScriptRunner(scripts.preview))
program.command("watch")
.description(languageSelector("开始监听静态文件夹", "Start to watch static directory"))
.action(commandScriptRunner(scripts.watch))
program.command("build")
.description(languageSelector("构建", "Build your blogs"))
.action(commandScriptRunner(scripts.indexing, scripts.ssr, scripts.build))
program.command("ssr")
.description(languageSelector("服务端渲染", "Server side rendering"))
.action(commandScriptRunner(scripts.ssr))
program.command("indexing")
.description(languageSelector("构建索引文件", "Build indexing files"))
.action(commandScriptRunner(scripts.indexing))
program.command("count")
.description(languageSelector("字数计数并生成统计页面文件", "Count blog words and generate statistic page file"))
.action(commandScriptRunner(scripts.count))
program.command("backup")
.description(languageSelector("备份博客元数据", "Backup blog metadata"))
.action(commandScriptRunner(scripts.backup))
program.command("restore")
.description(languageSelector("恢复博客元数据", "Restore blog metadata"))
.action(commandScriptRunner(scripts.restore))
program.command("mcp")
.description(languageSelector("启动 MCP 服务器", "Start MCP server"))
.argument("<endpoint>", languageSelector("目标预览服务器地址", "Target preview server endpoint"))
.argument("<port>", languageSelector("MCP 服务器端口", "MCP server port"))
.action(commandScriptRunner(scripts.mcp))
program.parse(process.argv)