@zhin.js/cli 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 +120 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +21 -0
- package/dist/cli.js.map +1 -0
- package/dist/commands/build.d.ts +3 -0
- package/dist/commands/build.d.ts.map +1 -0
- package/dist/commands/build.js +61 -0
- package/dist/commands/build.js.map +1 -0
- package/dist/commands/dev.d.ts +3 -0
- package/dist/commands/dev.d.ts.map +1 -0
- package/dist/commands/dev.js +162 -0
- package/dist/commands/dev.js.map +1 -0
- package/dist/commands/init.d.ts +3 -0
- package/dist/commands/init.d.ts.map +1 -0
- package/dist/commands/init.js +784 -0
- package/dist/commands/init.js.map +1 -0
- package/dist/commands/start.d.ts +4 -0
- package/dist/commands/start.d.ts.map +1 -0
- package/dist/commands/start.js +212 -0
- package/dist/commands/start.js.map +1 -0
- package/dist/commands/stop.d.ts +3 -0
- package/dist/commands/stop.d.ts.map +1 -0
- package/dist/commands/stop.js +24 -0
- package/dist/commands/stop.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/utils/env.d.ts +11 -0
- package/dist/utils/env.d.ts.map +1 -0
- package/dist/utils/env.js +64 -0
- package/dist/utils/env.js.map +1 -0
- package/dist/utils/logger.d.ts +8 -0
- package/dist/utils/logger.d.ts.map +1 -0
- package/dist/utils/logger.js +18 -0
- package/dist/utils/logger.js.map +1 -0
- package/dist/utils/process.d.ts +29 -0
- package/dist/utils/process.d.ts.map +1 -0
- package/dist/utils/process.js +308 -0
- package/dist/utils/process.js.map +1 -0
- package/package.json +36 -0
- package/src/cli.ts +25 -0
- package/src/commands/build.ts +68 -0
- package/src/commands/dev.ts +188 -0
- package/src/commands/init.ts +826 -0
- package/src/commands/start.ts +236 -0
- package/src/commands/stop.ts +27 -0
- package/src/index.ts +1 -0
- package/src/utils/env.ts +70 -0
- package/src/utils/logger.ts +21 -0
- package/src/utils/process.ts +348 -0
- package/tsconfig.json +24 -0
- package/tsconfig.tsbuildinfo +1 -0
|
@@ -0,0 +1,784 @@
|
|
|
1
|
+
import { Command } from 'commander';
|
|
2
|
+
import { logger } from '../utils/logger.js';
|
|
3
|
+
import fs from 'fs-extra';
|
|
4
|
+
import path from 'path';
|
|
5
|
+
import inquirer from 'inquirer';
|
|
6
|
+
export const initCommand = new Command('init')
|
|
7
|
+
.description('初始化新的Zhin机器人项目')
|
|
8
|
+
.argument('[project-name]', '项目名称')
|
|
9
|
+
.option('-c, --config <format>', '配置文件格式 (json|yaml|toml|ts|js)', 'js')
|
|
10
|
+
.option('-p, --package-manager <manager>', '包管理器 (npm|yarn|pnpm)', 'pnpm')
|
|
11
|
+
.option('-r, --runtime <runtime>', '运行时 (node|bun)', 'bun')
|
|
12
|
+
.option('-y, --yes', '自动回答所有问题')
|
|
13
|
+
.action(async (projectName, options) => {
|
|
14
|
+
if (options.yes) {
|
|
15
|
+
options.config = 'js';
|
|
16
|
+
options.packageManager = 'pnpm';
|
|
17
|
+
options.runtime = 'bun';
|
|
18
|
+
}
|
|
19
|
+
try {
|
|
20
|
+
let name = projectName;
|
|
21
|
+
if (!name) {
|
|
22
|
+
const { projectName: inputName } = await inquirer.prompt([
|
|
23
|
+
{
|
|
24
|
+
type: 'input',
|
|
25
|
+
name: 'projectName',
|
|
26
|
+
message: '请输入项目名称:',
|
|
27
|
+
default: 'my-zhin-bot',
|
|
28
|
+
validate: (input) => {
|
|
29
|
+
if (!input.trim()) {
|
|
30
|
+
return '项目名称不能为空';
|
|
31
|
+
}
|
|
32
|
+
if (!/^[a-zA-Z0-9-_]+$/.test(input)) {
|
|
33
|
+
return '项目名称只能包含字母、数字、横线和下划线';
|
|
34
|
+
}
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
]);
|
|
39
|
+
name = inputName;
|
|
40
|
+
}
|
|
41
|
+
if (!options.runtime) {
|
|
42
|
+
const { runtime: inputRuntime } = await inquirer.prompt([
|
|
43
|
+
{
|
|
44
|
+
type: 'list',
|
|
45
|
+
name: 'runtime',
|
|
46
|
+
message: '选择运行时:',
|
|
47
|
+
choices: [
|
|
48
|
+
{ name: 'Bun (推荐)', value: 'bun' },
|
|
49
|
+
{ name: 'Node.js', value: 'node' }
|
|
50
|
+
],
|
|
51
|
+
default: options.runtime || 'bun'
|
|
52
|
+
},
|
|
53
|
+
]);
|
|
54
|
+
options.runtime = inputRuntime;
|
|
55
|
+
}
|
|
56
|
+
if (!options.packageManager) {
|
|
57
|
+
const { packageManager: inputPackageManager } = await inquirer.prompt([
|
|
58
|
+
{
|
|
59
|
+
type: 'list',
|
|
60
|
+
name: 'packageManager',
|
|
61
|
+
message: '选择包管理器:',
|
|
62
|
+
choices: [
|
|
63
|
+
{ name: 'pnpm (推荐)', value: 'pnpm' },
|
|
64
|
+
{ name: 'npm', value: 'npm' },
|
|
65
|
+
{ name: 'yarn', value: 'yarn' }
|
|
66
|
+
],
|
|
67
|
+
default: options.packageManager || 'pnpm'
|
|
68
|
+
}
|
|
69
|
+
]);
|
|
70
|
+
options.packageManager = inputPackageManager;
|
|
71
|
+
}
|
|
72
|
+
if (!options.config) {
|
|
73
|
+
const { configFormat: inputConfigFormat } = await inquirer.prompt([
|
|
74
|
+
{
|
|
75
|
+
type: 'list',
|
|
76
|
+
name: 'configFormat',
|
|
77
|
+
message: '选择配置文件格式:',
|
|
78
|
+
choices: [
|
|
79
|
+
{ name: 'JavaScript (推荐)', value: 'js' },
|
|
80
|
+
{ name: 'TypeScript', value: 'ts' },
|
|
81
|
+
{ name: 'YAML', value: 'yaml' },
|
|
82
|
+
{ name: 'JSON', value: 'json' },
|
|
83
|
+
{ name: 'TOML', value: 'toml' }
|
|
84
|
+
],
|
|
85
|
+
default: options.config || 'js'
|
|
86
|
+
}
|
|
87
|
+
]);
|
|
88
|
+
options.config = inputConfigFormat;
|
|
89
|
+
}
|
|
90
|
+
const projectPath = path.resolve(process.cwd(), name);
|
|
91
|
+
const realName = path.basename(projectPath);
|
|
92
|
+
// 检查目录是否已存在
|
|
93
|
+
if (fs.existsSync(projectPath)) {
|
|
94
|
+
logger.error(`目录 ${realName} 已存在`);
|
|
95
|
+
process.exit(1);
|
|
96
|
+
}
|
|
97
|
+
logger.info(`正在创建项目 ${realName}...`);
|
|
98
|
+
// 创建项目目录结构
|
|
99
|
+
await createProjectStructure(projectPath, realName, options);
|
|
100
|
+
logger.success(`项目 ${realName} 创建成功!`);
|
|
101
|
+
logger.log('');
|
|
102
|
+
logger.log('🎉 下一步操作:');
|
|
103
|
+
logger.log(` cd ${realName}`);
|
|
104
|
+
const installCommand = getInstallCommand(options.packageManager);
|
|
105
|
+
logger.log(` ${installCommand} # 安装依赖`);
|
|
106
|
+
logger.log(` npm run dev # 开发环境启动`);
|
|
107
|
+
logger.log(` npm run build # 构建项目`);
|
|
108
|
+
logger.log(` npm run start # 生产环境前台启动`);
|
|
109
|
+
logger.log(` npm run daemon # 生产环境后台启动`);
|
|
110
|
+
logger.log(` npm run stop # 停止机器人`);
|
|
111
|
+
logger.log('');
|
|
112
|
+
logger.log('📚 相关文档:');
|
|
113
|
+
logger.log(' https://github.com/zhinjs/zhin - 项目主页');
|
|
114
|
+
logger.log(' https://zhinjs.github.io - 官方文档');
|
|
115
|
+
}
|
|
116
|
+
catch (error) {
|
|
117
|
+
logger.error(`创建项目失败: ${error}`);
|
|
118
|
+
process.exit(1);
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
function getInstallCommand(packageManager) {
|
|
122
|
+
switch (packageManager) {
|
|
123
|
+
case 'yarn': return 'yarn install';
|
|
124
|
+
case 'pnpm': return 'pnpm install';
|
|
125
|
+
default: return 'npm install';
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
async function createProjectStructure(projectPath, projectName, options) {
|
|
129
|
+
// 创建目录结构
|
|
130
|
+
await fs.ensureDir(projectPath);
|
|
131
|
+
await fs.ensureDir(path.join(projectPath, 'src'));
|
|
132
|
+
await fs.ensureDir(path.join(projectPath, 'src', 'plugins'));
|
|
133
|
+
await fs.ensureDir(path.join(projectPath, 'dist'));
|
|
134
|
+
await fs.ensureDir(path.join(projectPath, 'data'));
|
|
135
|
+
// 创建 package.json
|
|
136
|
+
const packageJson = {
|
|
137
|
+
name: projectName,
|
|
138
|
+
version: '0.1.0',
|
|
139
|
+
description: `${projectName} 机器人`,
|
|
140
|
+
type: 'module',
|
|
141
|
+
main: 'src/index.ts',
|
|
142
|
+
scripts: {
|
|
143
|
+
dev: options.runtime === 'bun' ? 'zhin dev --bun' : 'zhin dev',
|
|
144
|
+
start: options.runtime === 'bun' ? 'zhin start --bun' : 'zhin start',
|
|
145
|
+
daemon: options.runtime === 'bun' ? 'zhin start --bun --daemon' : 'zhin start --daemon',
|
|
146
|
+
build: 'zhin build',
|
|
147
|
+
stop: 'zhin stop'
|
|
148
|
+
},
|
|
149
|
+
dependencies: {
|
|
150
|
+
'@zhin.js/core': 'workspace:*'
|
|
151
|
+
},
|
|
152
|
+
devDependencies: {
|
|
153
|
+
'@zhin.js/cli': 'workspace:*',
|
|
154
|
+
'typescript': '^5.0.0',
|
|
155
|
+
...(options.runtime === 'node' && { 'tsx': '^4.0.0' })
|
|
156
|
+
},
|
|
157
|
+
engines: {
|
|
158
|
+
node: '>=18.0.0'
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
await fs.writeJson(path.join(projectPath, 'package.json'), packageJson, { spaces: 2 });
|
|
162
|
+
// 创建 tsconfig.json
|
|
163
|
+
const tsConfig = {
|
|
164
|
+
compilerOptions: {
|
|
165
|
+
target: 'ES2022',
|
|
166
|
+
module: 'ESNext',
|
|
167
|
+
moduleResolution: 'bundler',
|
|
168
|
+
outDir: './dist',
|
|
169
|
+
strict: true,
|
|
170
|
+
esModuleInterop: true,
|
|
171
|
+
skipLibCheck: true,
|
|
172
|
+
forceConsistentCasingInFileNames: true,
|
|
173
|
+
resolveJsonModule: true,
|
|
174
|
+
isolatedModules: true,
|
|
175
|
+
allowSyntheticDefaultImports: true,
|
|
176
|
+
experimentalDecorators: true,
|
|
177
|
+
emitDecoratorMetadata: true,
|
|
178
|
+
declaration: false,
|
|
179
|
+
sourceMap: true,
|
|
180
|
+
baseUrl: './src',
|
|
181
|
+
paths: {
|
|
182
|
+
'@zhin.js/core': ['../../packages/core/src/index.ts'],
|
|
183
|
+
'@zhin.js/core/*': ['../../packages/core/src/*']
|
|
184
|
+
}
|
|
185
|
+
},
|
|
186
|
+
include: ['src/**/*'],
|
|
187
|
+
exclude: ['dist', 'node_modules']
|
|
188
|
+
};
|
|
189
|
+
await fs.writeJson(path.join(projectPath, 'tsconfig.json'), tsConfig, { spaces: 2 });
|
|
190
|
+
// 创建配置文件
|
|
191
|
+
await createConfigFile(projectPath, options.config);
|
|
192
|
+
// 创建主入口文件
|
|
193
|
+
const indexContent = `import { createApp } from '@zhin.js/core';
|
|
194
|
+
|
|
195
|
+
// 启动机器人
|
|
196
|
+
async function main() {
|
|
197
|
+
try {
|
|
198
|
+
// 异步创建机器人实例 (自动从配置文件加载)
|
|
199
|
+
const app = await createApp();
|
|
200
|
+
await app.start();
|
|
201
|
+
|
|
202
|
+
// 优雅退出处理
|
|
203
|
+
const shutdown = async (signal: string) => {
|
|
204
|
+
await app.stop();
|
|
205
|
+
process.exit(0);
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
process.on('SIGINT', shutdown);
|
|
209
|
+
process.on('SIGTERM', shutdown);
|
|
210
|
+
} catch (error) {
|
|
211
|
+
console.error('机器人启动失败:', error);
|
|
212
|
+
process.exit(1);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// 启动应用
|
|
217
|
+
main().catch(console.error);
|
|
218
|
+
`;
|
|
219
|
+
await fs.writeFile(path.join(projectPath, 'src', 'index.ts'), indexContent);
|
|
220
|
+
// 创建示例插件
|
|
221
|
+
const pluginContent = `import {
|
|
222
|
+
onDispose,
|
|
223
|
+
addMiddleware, useContext, sendMessage, beforeSend, onGroupMessage,
|
|
224
|
+
} from '@zhin.js/core';
|
|
225
|
+
import * as process from "node:process";
|
|
226
|
+
|
|
227
|
+
onDispose(async ()=>{
|
|
228
|
+
console.log('插件已销毁')
|
|
229
|
+
})
|
|
230
|
+
|
|
231
|
+
addMiddleware(async (message, next)=>{ // 添加中间件到插件
|
|
232
|
+
// 在这里处理消息
|
|
233
|
+
return next()
|
|
234
|
+
})
|
|
235
|
+
|
|
236
|
+
let hasChanged=false
|
|
237
|
+
beforeSend((options)=>{
|
|
238
|
+
if(!hasChanged){
|
|
239
|
+
options.content='bar'
|
|
240
|
+
hasChanged=true
|
|
241
|
+
}
|
|
242
|
+
return options
|
|
243
|
+
})
|
|
244
|
+
|
|
245
|
+
onGroupMessage((m)=>{
|
|
246
|
+
if(m.channel.id==='629336764'){
|
|
247
|
+
m.reply('hello')
|
|
248
|
+
}
|
|
249
|
+
})
|
|
250
|
+
|
|
251
|
+
// 依赖process上下文
|
|
252
|
+
useContext('process',()=>{
|
|
253
|
+
sendMessage({
|
|
254
|
+
context:'process',
|
|
255
|
+
bot:\`\${process.pid}\`,
|
|
256
|
+
id:process.title,
|
|
257
|
+
type:'private',
|
|
258
|
+
content:'foo'
|
|
259
|
+
})
|
|
260
|
+
})
|
|
261
|
+
`;
|
|
262
|
+
await fs.writeFile(path.join(projectPath, 'src', 'plugins', 'test-plugin.ts'), pluginContent);
|
|
263
|
+
// 创建 .gitignore
|
|
264
|
+
const gitignoreContent = `# Dependencies
|
|
265
|
+
node_modules/
|
|
266
|
+
|
|
267
|
+
# Production builds
|
|
268
|
+
dist/
|
|
269
|
+
|
|
270
|
+
# Environment variables
|
|
271
|
+
.env
|
|
272
|
+
.env.local
|
|
273
|
+
.env.development
|
|
274
|
+
.env.production
|
|
275
|
+
|
|
276
|
+
# IDE
|
|
277
|
+
.vscode/
|
|
278
|
+
.idea/
|
|
279
|
+
|
|
280
|
+
# OS
|
|
281
|
+
.DS_Store
|
|
282
|
+
Thumbs.db
|
|
283
|
+
|
|
284
|
+
# Logs
|
|
285
|
+
*.log
|
|
286
|
+
npm-debug.log*
|
|
287
|
+
yarn-debug.log*
|
|
288
|
+
yarn-error.log*
|
|
289
|
+
pnpm-debug.log*
|
|
290
|
+
|
|
291
|
+
# PID files
|
|
292
|
+
.zhin.pid
|
|
293
|
+
.zhin-dev.pid
|
|
294
|
+
|
|
295
|
+
# Runtime data
|
|
296
|
+
pids
|
|
297
|
+
*.pid
|
|
298
|
+
*.seed
|
|
299
|
+
*.pid.lock
|
|
300
|
+
|
|
301
|
+
# Coverage directory used by tools like istanbul
|
|
302
|
+
coverage/
|
|
303
|
+
*.lcov
|
|
304
|
+
|
|
305
|
+
# nyc test coverage
|
|
306
|
+
.nyc_output
|
|
307
|
+
|
|
308
|
+
# Dependency directories
|
|
309
|
+
jspm_packages/
|
|
310
|
+
|
|
311
|
+
# Optional npm cache directory
|
|
312
|
+
.npm
|
|
313
|
+
|
|
314
|
+
# Optional eslint cache
|
|
315
|
+
.eslintcache
|
|
316
|
+
|
|
317
|
+
# Microbundle cache
|
|
318
|
+
.rpt2_cache/
|
|
319
|
+
.rts2_cache_cjs/
|
|
320
|
+
.rts2_cache_es/
|
|
321
|
+
.rts2_cache_umd/
|
|
322
|
+
|
|
323
|
+
# Optional REPL history
|
|
324
|
+
.node_repl_history
|
|
325
|
+
|
|
326
|
+
# Output of 'npm pack'
|
|
327
|
+
*.tgz
|
|
328
|
+
|
|
329
|
+
# Yarn Integrity file
|
|
330
|
+
.yarn-integrity
|
|
331
|
+
|
|
332
|
+
# parcel-bundler cache (https://parceljs.org/)
|
|
333
|
+
.cache
|
|
334
|
+
.parcel-cache
|
|
335
|
+
|
|
336
|
+
# Next.js build output
|
|
337
|
+
.next
|
|
338
|
+
|
|
339
|
+
# Nuxt.js build / generate output
|
|
340
|
+
.nuxt
|
|
341
|
+
dist
|
|
342
|
+
|
|
343
|
+
# Gatsby files
|
|
344
|
+
.cache/
|
|
345
|
+
public
|
|
346
|
+
|
|
347
|
+
# Storybook build outputs
|
|
348
|
+
.out
|
|
349
|
+
.storybook-out
|
|
350
|
+
|
|
351
|
+
# Temporary folders
|
|
352
|
+
tmp/
|
|
353
|
+
temp/
|
|
354
|
+
`;
|
|
355
|
+
await fs.writeFile(path.join(projectPath, '.gitignore'), gitignoreContent);
|
|
356
|
+
// 创建 README.md
|
|
357
|
+
const readmeContent = `# ${projectName}
|
|
358
|
+
|
|
359
|
+
使用 Zhin 框架创建的机器人项目。
|
|
360
|
+
|
|
361
|
+
## 🚀 快速开始
|
|
362
|
+
|
|
363
|
+
### 安装依赖
|
|
364
|
+
|
|
365
|
+
\`\`\`bash
|
|
366
|
+
${getInstallCommand(options.packageManager)}
|
|
367
|
+
\`\`\`
|
|
368
|
+
|
|
369
|
+
### 开发环境
|
|
370
|
+
|
|
371
|
+
\`\`\`bash
|
|
372
|
+
npm run dev
|
|
373
|
+
\`\`\`
|
|
374
|
+
|
|
375
|
+
### 生产环境
|
|
376
|
+
|
|
377
|
+
\`\`\`bash
|
|
378
|
+
# 构建项目
|
|
379
|
+
npm run build
|
|
380
|
+
|
|
381
|
+
# 前台启动
|
|
382
|
+
npm run start
|
|
383
|
+
|
|
384
|
+
# 后台启动
|
|
385
|
+
npm run daemon
|
|
386
|
+
\`\`\`
|
|
387
|
+
|
|
388
|
+
### 停止机器人
|
|
389
|
+
|
|
390
|
+
\`\`\`bash
|
|
391
|
+
npm run stop
|
|
392
|
+
\`\`\`
|
|
393
|
+
|
|
394
|
+
## 📁 项目结构
|
|
395
|
+
|
|
396
|
+
\`\`\`
|
|
397
|
+
${projectName}/
|
|
398
|
+
├── src/
|
|
399
|
+
│ ├── index.ts # 主入口文件
|
|
400
|
+
│ └── plugins/ # 插件目录
|
|
401
|
+
│ └── test-plugin.ts # 示例插件
|
|
402
|
+
├── dist/ # 构建输出目录
|
|
403
|
+
├── data/ # 数据目录
|
|
404
|
+
├── zhin.config.${options.config} # 配置文件
|
|
405
|
+
├── package.json # 项目配置
|
|
406
|
+
└── tsconfig.json # TypeScript配置
|
|
407
|
+
\`\`\`
|
|
408
|
+
|
|
409
|
+
## ⚙️ 配置
|
|
410
|
+
|
|
411
|
+
### 机器人配置
|
|
412
|
+
|
|
413
|
+
编辑 \`zhin.config.${options.config}\` 来配置你的机器人:
|
|
414
|
+
|
|
415
|
+
${getConfigExample(options.config)}
|
|
416
|
+
|
|
417
|
+
## 🔌 插件开发
|
|
418
|
+
|
|
419
|
+
在 \`src/plugins/\` 目录下创建你的插件文件。参考 \`test-plugin.ts\` 了解插件开发方式。
|
|
420
|
+
|
|
421
|
+
### 插件示例
|
|
422
|
+
|
|
423
|
+
\`\`\`typescript
|
|
424
|
+
import { usePlugin, useLogger, addCommand } from '@zhin.js/core';
|
|
425
|
+
|
|
426
|
+
const plugin = usePlugin();
|
|
427
|
+
const logger = useLogger();
|
|
428
|
+
|
|
429
|
+
// 添加命令
|
|
430
|
+
addCommand('hello', (message, args) => {
|
|
431
|
+
logger.info('Hello command called:', args);
|
|
432
|
+
});
|
|
433
|
+
\`\`\`
|
|
434
|
+
|
|
435
|
+
## 📚 相关链接
|
|
436
|
+
|
|
437
|
+
- [Zhin 官方文档](https://zhinjs.github.io)
|
|
438
|
+
- [插件开发指南](https://zhinjs.github.io/plugins)
|
|
439
|
+
- [GitHub 仓库](https://github.com/zhinjs/zhin)
|
|
440
|
+
|
|
441
|
+
## 🤝 贡献
|
|
442
|
+
|
|
443
|
+
欢迎提交 Issue 和 Pull Request!
|
|
444
|
+
|
|
445
|
+
## 📄 许可证
|
|
446
|
+
|
|
447
|
+
MIT License
|
|
448
|
+
`;
|
|
449
|
+
await fs.writeFile(path.join(projectPath, 'README.md'), readmeContent);
|
|
450
|
+
// 创建 pnpm-workspace.yaml (如果使用 pnpm)
|
|
451
|
+
if (options.packageManager === 'pnpm') {
|
|
452
|
+
const workspaceContent = `packages:
|
|
453
|
+
- '.'
|
|
454
|
+
`;
|
|
455
|
+
await fs.writeFile(path.join(projectPath, 'pnpm-workspace.yaml'), workspaceContent);
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
async function createConfigFile(projectPath, format) {
|
|
459
|
+
const configContent = getConfigContent(format);
|
|
460
|
+
let fileName;
|
|
461
|
+
switch (format) {
|
|
462
|
+
case 'ts':
|
|
463
|
+
fileName = 'zhin.config.ts';
|
|
464
|
+
break;
|
|
465
|
+
case 'js':
|
|
466
|
+
fileName = 'zhin.config.js';
|
|
467
|
+
break;
|
|
468
|
+
default:
|
|
469
|
+
fileName = `zhin.config.${format}`;
|
|
470
|
+
}
|
|
471
|
+
await fs.writeFile(path.join(projectPath, fileName), configContent);
|
|
472
|
+
}
|
|
473
|
+
function getConfigContent(format) {
|
|
474
|
+
switch (format) {
|
|
475
|
+
case 'json':
|
|
476
|
+
return JSON.stringify({
|
|
477
|
+
bots: [
|
|
478
|
+
{
|
|
479
|
+
name: `${process.pid}`,
|
|
480
|
+
context: 'process'
|
|
481
|
+
},
|
|
482
|
+
{
|
|
483
|
+
name: '1689919782',
|
|
484
|
+
context: 'icqq',
|
|
485
|
+
log_level: 'off',
|
|
486
|
+
platform: 4
|
|
487
|
+
}
|
|
488
|
+
],
|
|
489
|
+
plugin_dirs: [
|
|
490
|
+
'./src/plugins',
|
|
491
|
+
'node_modules'
|
|
492
|
+
],
|
|
493
|
+
plugins: [
|
|
494
|
+
'icqq',
|
|
495
|
+
'process',
|
|
496
|
+
'test-plugin'
|
|
497
|
+
],
|
|
498
|
+
debug: false
|
|
499
|
+
}, null, 2);
|
|
500
|
+
case 'yaml':
|
|
501
|
+
return `# Zhin Bot 配置文件
|
|
502
|
+
|
|
503
|
+
# 机器人配置
|
|
504
|
+
bots:
|
|
505
|
+
- name: \${process.pid}
|
|
506
|
+
context: process
|
|
507
|
+
- name: '1689919782'
|
|
508
|
+
context: icqq
|
|
509
|
+
log_level: off
|
|
510
|
+
platform: 4
|
|
511
|
+
|
|
512
|
+
# 插件目录
|
|
513
|
+
plugin_dirs:
|
|
514
|
+
- ./src/plugins
|
|
515
|
+
- node_modules
|
|
516
|
+
|
|
517
|
+
# 要加载的插件列表
|
|
518
|
+
plugins:
|
|
519
|
+
- icqq
|
|
520
|
+
- process
|
|
521
|
+
- test-plugin
|
|
522
|
+
|
|
523
|
+
# 调试模式
|
|
524
|
+
debug: false
|
|
525
|
+
`;
|
|
526
|
+
case 'toml':
|
|
527
|
+
return `# Zhin Bot 配置文件
|
|
528
|
+
|
|
529
|
+
# 机器人配置
|
|
530
|
+
[[bots]]
|
|
531
|
+
name = "\${process.pid}"
|
|
532
|
+
context = "process"
|
|
533
|
+
|
|
534
|
+
[[bots]]
|
|
535
|
+
name = "1689919782"
|
|
536
|
+
context = "icqq"
|
|
537
|
+
log_level = "off"
|
|
538
|
+
platform = 4
|
|
539
|
+
|
|
540
|
+
# 插件目录
|
|
541
|
+
plugin_dirs = ["./src/plugins", "node_modules"]
|
|
542
|
+
|
|
543
|
+
# 要加载的插件列表
|
|
544
|
+
plugins = ["icqq", "process", "test-plugin"]
|
|
545
|
+
|
|
546
|
+
# 调试模式
|
|
547
|
+
debug = false
|
|
548
|
+
`;
|
|
549
|
+
case 'ts':
|
|
550
|
+
return `import { defineConfig } from '@zhin.js/core';
|
|
551
|
+
|
|
552
|
+
export default defineConfig(async (env)=>{
|
|
553
|
+
return {
|
|
554
|
+
// 机器人配置
|
|
555
|
+
bots: [
|
|
556
|
+
{
|
|
557
|
+
name: \`\${process.pid}\`,
|
|
558
|
+
context: 'process'
|
|
559
|
+
},
|
|
560
|
+
{
|
|
561
|
+
name: '1689919782',
|
|
562
|
+
context: 'icqq',
|
|
563
|
+
log_level: 'off',
|
|
564
|
+
platform: 4
|
|
565
|
+
}
|
|
566
|
+
],
|
|
567
|
+
// 插件目录
|
|
568
|
+
plugin_dirs: [
|
|
569
|
+
env.PLUGIN_DIR || './src/plugins',
|
|
570
|
+
'node_modules'
|
|
571
|
+
],
|
|
572
|
+
// 要加载的插件列表
|
|
573
|
+
plugins: [
|
|
574
|
+
'icqq',
|
|
575
|
+
'process',
|
|
576
|
+
'test-plugin'
|
|
577
|
+
],
|
|
578
|
+
|
|
579
|
+
// 调试模式
|
|
580
|
+
debug: env.DEBUG === 'true'
|
|
581
|
+
}
|
|
582
|
+
})
|
|
583
|
+
`;
|
|
584
|
+
case 'js':
|
|
585
|
+
return `import { defineConfig } from '@zhin.js/core';
|
|
586
|
+
|
|
587
|
+
export default defineConfig(async (env)=>{
|
|
588
|
+
return {
|
|
589
|
+
// 机器人配置
|
|
590
|
+
bots: [
|
|
591
|
+
{
|
|
592
|
+
name: \`\${process.pid}\`,
|
|
593
|
+
context: 'process'
|
|
594
|
+
},
|
|
595
|
+
{
|
|
596
|
+
name: '1689919782',
|
|
597
|
+
context: 'icqq',
|
|
598
|
+
log_level: 'off',
|
|
599
|
+
platform: 4
|
|
600
|
+
}
|
|
601
|
+
],
|
|
602
|
+
// 插件目录
|
|
603
|
+
plugin_dirs: [
|
|
604
|
+
env.PLUGIN_DIR || './src/plugins',
|
|
605
|
+
'node_modules'
|
|
606
|
+
],
|
|
607
|
+
// 要加载的插件列表
|
|
608
|
+
plugins: [
|
|
609
|
+
'icqq',
|
|
610
|
+
'process',
|
|
611
|
+
'test-plugin'
|
|
612
|
+
],
|
|
613
|
+
|
|
614
|
+
// 调试模式
|
|
615
|
+
debug: env.DEBUG === 'true'
|
|
616
|
+
}
|
|
617
|
+
})
|
|
618
|
+
`;
|
|
619
|
+
default:
|
|
620
|
+
throw new Error(`不支持的配置格式: ${format}`);
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
function getConfigExample(format) {
|
|
624
|
+
switch (format) {
|
|
625
|
+
case 'json':
|
|
626
|
+
return `\`\`\`json
|
|
627
|
+
{
|
|
628
|
+
"bots": [
|
|
629
|
+
{
|
|
630
|
+
"name": "\${process.pid}",
|
|
631
|
+
"context": "process"
|
|
632
|
+
},
|
|
633
|
+
{
|
|
634
|
+
"name": "1689919782",
|
|
635
|
+
"context": "icqq",
|
|
636
|
+
"log_level": "off",
|
|
637
|
+
"platform": 4
|
|
638
|
+
}
|
|
639
|
+
],
|
|
640
|
+
"plugin_dirs": [
|
|
641
|
+
"./src/plugins",
|
|
642
|
+
"node_modules"
|
|
643
|
+
],
|
|
644
|
+
"plugins": [
|
|
645
|
+
"icqq",
|
|
646
|
+
"process",
|
|
647
|
+
"test-plugin"
|
|
648
|
+
],
|
|
649
|
+
"debug": false
|
|
650
|
+
}
|
|
651
|
+
\`\`\`
|
|
652
|
+
`;
|
|
653
|
+
case 'yaml':
|
|
654
|
+
return `\`\`\`yaml
|
|
655
|
+
# Zhin Bot 配置文件
|
|
656
|
+
|
|
657
|
+
# 机器人配置
|
|
658
|
+
bots:
|
|
659
|
+
- name: \${process.pid}
|
|
660
|
+
context: process
|
|
661
|
+
- name: '1689919782'
|
|
662
|
+
context: icqq
|
|
663
|
+
log_level: off
|
|
664
|
+
platform: 4
|
|
665
|
+
|
|
666
|
+
# 插件目录
|
|
667
|
+
plugin_dirs:
|
|
668
|
+
- ./src/plugins
|
|
669
|
+
- node_modules
|
|
670
|
+
|
|
671
|
+
# 要加载的插件列表
|
|
672
|
+
plugins:
|
|
673
|
+
- icqq
|
|
674
|
+
- process
|
|
675
|
+
- test-plugin
|
|
676
|
+
|
|
677
|
+
# 调试模式
|
|
678
|
+
debug: false
|
|
679
|
+
\`\`\`
|
|
680
|
+
`;
|
|
681
|
+
case 'toml':
|
|
682
|
+
return `\`\`\`toml
|
|
683
|
+
# Zhin Bot 配置文件
|
|
684
|
+
|
|
685
|
+
# 机器人配置
|
|
686
|
+
[[bots]]
|
|
687
|
+
name = "\${process.pid}"
|
|
688
|
+
context = "process"
|
|
689
|
+
|
|
690
|
+
[[bots]]
|
|
691
|
+
name = "1689919782"
|
|
692
|
+
context = "icqq"
|
|
693
|
+
log_level = "off"
|
|
694
|
+
platform = 4
|
|
695
|
+
|
|
696
|
+
# 插件目录
|
|
697
|
+
plugin_dirs = ["./src/plugins", "node_modules"]
|
|
698
|
+
|
|
699
|
+
# 要加载的插件列表
|
|
700
|
+
plugins = ["icqq", "process", "test-plugin"]
|
|
701
|
+
|
|
702
|
+
# 调试模式
|
|
703
|
+
debug = false
|
|
704
|
+
\`\`\`
|
|
705
|
+
`;
|
|
706
|
+
case 'ts':
|
|
707
|
+
return `\`\`\`typescript
|
|
708
|
+
import { defineConfig } from '@zhin.js/core';
|
|
709
|
+
|
|
710
|
+
export default defineConfig(async (env)=>{
|
|
711
|
+
return {
|
|
712
|
+
// 机器人配置
|
|
713
|
+
bots: [
|
|
714
|
+
{
|
|
715
|
+
name: \`\${process.pid}\`,
|
|
716
|
+
context: 'process'
|
|
717
|
+
},
|
|
718
|
+
{
|
|
719
|
+
name: '1689919782',
|
|
720
|
+
context: 'icqq',
|
|
721
|
+
log_level: 'off',
|
|
722
|
+
platform: 4
|
|
723
|
+
}
|
|
724
|
+
],
|
|
725
|
+
// 插件目录
|
|
726
|
+
plugin_dirs: [
|
|
727
|
+
env.PLUGIN_DIR || './src/plugins',
|
|
728
|
+
'node_modules'
|
|
729
|
+
],
|
|
730
|
+
// 要加载的插件列表
|
|
731
|
+
plugins: [
|
|
732
|
+
'icqq',
|
|
733
|
+
'process',
|
|
734
|
+
'test-plugin'
|
|
735
|
+
],
|
|
736
|
+
|
|
737
|
+
// 调试模式
|
|
738
|
+
debug: env.DEBUG === 'true'
|
|
739
|
+
}
|
|
740
|
+
})
|
|
741
|
+
\`\`\`
|
|
742
|
+
`;
|
|
743
|
+
case 'js':
|
|
744
|
+
return `\`\`\`javascript
|
|
745
|
+
import { defineConfig } from '@zhin.js/core';
|
|
746
|
+
|
|
747
|
+
export default defineConfig(async (env)=>{
|
|
748
|
+
return {
|
|
749
|
+
// 机器人配置
|
|
750
|
+
bots: [
|
|
751
|
+
{
|
|
752
|
+
name: \`\${process.pid}\`,
|
|
753
|
+
context: 'process'
|
|
754
|
+
},
|
|
755
|
+
{
|
|
756
|
+
name: '1689919782',
|
|
757
|
+
context: 'icqq',
|
|
758
|
+
log_level: 'off',
|
|
759
|
+
platform: 4
|
|
760
|
+
}
|
|
761
|
+
],
|
|
762
|
+
// 插件目录
|
|
763
|
+
plugin_dirs: [
|
|
764
|
+
env.PLUGIN_DIR || './src/plugins',
|
|
765
|
+
'node_modules'
|
|
766
|
+
],
|
|
767
|
+
// 要加载的插件列表
|
|
768
|
+
plugins: [
|
|
769
|
+
'icqq',
|
|
770
|
+
'process',
|
|
771
|
+
'test-plugin'
|
|
772
|
+
],
|
|
773
|
+
|
|
774
|
+
// 调试模式
|
|
775
|
+
debug: env.DEBUG === 'true'
|
|
776
|
+
}
|
|
777
|
+
})
|
|
778
|
+
\`\`\`
|
|
779
|
+
`;
|
|
780
|
+
default:
|
|
781
|
+
throw new Error(`不支持的配置格式: ${format}`);
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
//# sourceMappingURL=init.js.map
|