62 lines
1.9 KiB
TypeScript
62 lines
1.9 KiB
TypeScript
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import { checkFileExists, AssistantConfig } from '@/module/assistant/index.ts';
|
|
import { chalk } from '@/module/chalk.ts';
|
|
export type AssistantInitOptions = {
|
|
path?: string;
|
|
init?: boolean;
|
|
};
|
|
/**
|
|
* 助手初始化类
|
|
* @class AssistantInit
|
|
*/
|
|
export class AssistantInit extends AssistantConfig {
|
|
constructor(opts?: AssistantInitOptions) {
|
|
const configDir = opts?.path || process.cwd();
|
|
super({
|
|
configDir,
|
|
init: opts?.init ?? false,
|
|
});
|
|
}
|
|
|
|
async init() {
|
|
// 1. 检查助手路径是否存在
|
|
if (!this.checkConfigPath()) {
|
|
console.log(chalk.blue('助手路径不存在,正在创建...'));
|
|
super.init();
|
|
this.createAssistantConfig();
|
|
} else {
|
|
super.init();
|
|
console.log(chalk.yellow('助手路径已存在'));
|
|
return;
|
|
}
|
|
}
|
|
checkConfigPath() {
|
|
const assistantPath = path.join(this.configDir, 'assistant-app', 'assistant-config.json');
|
|
return checkFileExists(assistantPath);
|
|
}
|
|
createAssistantConfig() {
|
|
const assistantPath = this.configPath?.configPath;
|
|
// 创建助手配置文件 assistant-config.json
|
|
if (!checkFileExists(assistantPath, true)) {
|
|
this.setConfig({
|
|
description: '助手配置文件',
|
|
});
|
|
console.log(chalk.green('助手配置文件assistant-config.json创建成功'));
|
|
}
|
|
const env = this.configPath?.envConfigPath;
|
|
// 创建助手环境配置文件 env
|
|
if (!checkFileExists(env, true)) {
|
|
fs.writeFileSync(env, '# 环境配置文件\n');
|
|
console.log(chalk.green('助手环境配置.env文件创建成功'));
|
|
}
|
|
|
|
const appsConfig = this.configPath?.appsConfigPath;
|
|
// 创建助手应用配置文件 apps
|
|
if (!checkFileExists(appsConfig, true)) {
|
|
fs.writeFileSync(appsConfig, JSON.stringify({ description: 'apps manager.', list: [] }));
|
|
console.log(chalk.green('助手应用配置文件apps.json创建成功'));
|
|
}
|
|
}
|
|
}
|