feat: add silky cli tools
This commit is contained in:
		
							
								
								
									
										2
									
								
								assistant/tasks/silkyai-cli/src/command/check/index.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								assistant/tasks/silkyai-cli/src/command/check/index.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,2 @@ | ||||
| import './init.ts' | ||||
| import './init-env.ts' | ||||
							
								
								
									
										68
									
								
								assistant/tasks/silkyai-cli/src/command/check/init-env.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										68
									
								
								assistant/tasks/silkyai-cli/src/command/check/init-env.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,68 @@ | ||||
| import { program, Command } from '@/program.ts'; | ||||
|  | ||||
| import { TaskKey, silkyCommand } from '@/mdoules/talkshow.ts'; | ||||
|  | ||||
| const description = `初始化运行环境,安装 | ||||
| 1. @kevisual/cli | ||||
| 2. pm2 | ||||
|  | ||||
| 可选安装: | ||||
| deno  | ||||
| bun | ||||
|  | ||||
| `; | ||||
| const isDev = process.env.NODE_ENV === 'development'; | ||||
| const testEnv = new Command('init-env') | ||||
|   .description(description) | ||||
|   .option('-a --all') | ||||
|   .action((options) => { | ||||
|     let tag = options.all ? 'env' : 'must-env'; | ||||
|     silkyCommand.isDebug = isDev ?? false; | ||||
|     const envTask = silkyCommand.getTasksArrayByTag(tag); | ||||
|  | ||||
|     for (const value of envTask) { | ||||
|       try { | ||||
|         const res = silkyCommand.runTask(value); | ||||
|         if (res.code === 200) { | ||||
|           console.log('执行结果:', res.code, res.message); | ||||
|         } else { | ||||
|           console.log('执行结果错误:', res.task?.description, res.message); | ||||
|         } | ||||
|       } catch (error) { | ||||
|         console.log('error', error); | ||||
|         continue; | ||||
|       } | ||||
|     } | ||||
|   }); | ||||
|  | ||||
| program.addCommand(testEnv); | ||||
|  | ||||
| const appDownloadDesc = `下载安装talkshow的应用模块, | ||||
| 1. root/talkshow-admin | ||||
| 2. root/talkshow-code-center | ||||
| 3. root/center | ||||
| `; | ||||
| const appDownloadTask = new Command('init-talkshow') | ||||
|   .description(appDownloadDesc) | ||||
|   .option('-a --all', '下载所有应用模块或者只下载talkshow相关的') | ||||
|   .action((options) => { | ||||
|     // | ||||
|     let tag = options.all ? 'app' : 'talkshow'; | ||||
|     const appTask = silkyCommand.getTasksArrayByTag(tag); | ||||
|     silkyCommand.isDebug = true; | ||||
|     for (const value of appTask) { | ||||
|       try { | ||||
|         const res = silkyCommand.runTask(value); | ||||
|         if (res.code === 200) { | ||||
|           console.log('执行结果:', res.task?.description, res.code); | ||||
|         } else { | ||||
|           console.log('执行结果错误:', res.task?.description, res.message); | ||||
|         } | ||||
|       } catch (error) { | ||||
|         console.log('error', error); | ||||
|         continue; | ||||
|       } | ||||
|     } | ||||
|   }); | ||||
|  | ||||
| program.addCommand(appDownloadTask); | ||||
							
								
								
									
										25
									
								
								assistant/tasks/silkyai-cli/src/command/check/init.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								assistant/tasks/silkyai-cli/src/command/check/init.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,25 @@ | ||||
| import { program, Command } from '@/program.ts'; | ||||
| import path from 'node:path'; | ||||
| import { AssistantInit } from '@/services/init/index.ts'; | ||||
|  | ||||
| type InitCommandOptions = { | ||||
|   path?: string; | ||||
| }; | ||||
| const Init = new Command('init') | ||||
|   .description('初始化一个助手客户端,生成配置文件。') | ||||
|   .option('-p --path <path>', '助手路径,默认为执行命令的目录,如果助手路径不存在则创建。') | ||||
|   .action((opts: InitCommandOptions) => { | ||||
|     // 如果path参数存在,检测path是否是相对路径,如果是相对路径,则转换为绝对路径 | ||||
|     if (opts.path && !opts.path.startsWith('/')) { | ||||
|       opts.path = path.join(process.cwd(), opts.path); | ||||
|     } else if (opts.path) { | ||||
|       opts.path = path.resolve(opts.path); | ||||
|     } | ||||
|     const configDir = AssistantInit.detectConfigDir(opts.path); | ||||
|     const assistantInit = new AssistantInit({ | ||||
|       path: configDir, | ||||
|     }); | ||||
|     assistantInit.init(); | ||||
|   }); | ||||
|  | ||||
| program.addCommand(Init); | ||||
							
								
								
									
										7
									
								
								assistant/tasks/silkyai-cli/src/config.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								assistant/tasks/silkyai-cli/src/config.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,7 @@ | ||||
| import { AssistantConfig } from '@kevisual/assistant-cli/lib'; | ||||
|  | ||||
| export const configDir = AssistantConfig.detectConfigDir(null, 1); | ||||
| export const assistantConfig = new AssistantConfig({ | ||||
|   configDir, | ||||
|   init: false, | ||||
| }); | ||||
							
								
								
									
										19
									
								
								assistant/tasks/silkyai-cli/src/index.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								assistant/tasks/silkyai-cli/src/index.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,19 @@ | ||||
| import { program, runProgram } from '@/program.ts'; | ||||
|  | ||||
| /** | ||||
|  * 通过命令行解析器解析参数 | ||||
|  * args[0] 是执行的命令, example: node | ||||
|  * args[1] 是执行的脚本, example: index.ts | ||||
|  * @param argv | ||||
|  */ | ||||
| export const runParser = async (argv: string[]) => { | ||||
|   // program.parse(process.argv); | ||||
|   // console.log('argv', argv); | ||||
|   try { | ||||
|     program.parse(argv); | ||||
|   } catch (error) { | ||||
|     console.error('执行错误:', error.message); | ||||
|   } | ||||
| }; | ||||
|  | ||||
| export { runProgram }; | ||||
							
								
								
									
										116
									
								
								assistant/tasks/silkyai-cli/src/mdoules/talkshow.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										116
									
								
								assistant/tasks/silkyai-cli/src/mdoules/talkshow.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,116 @@ | ||||
| import { TasksCommand } from '@kevisual/task-command/mod.ts'; | ||||
| export const silkyCommand = new TasksCommand(); | ||||
| export const enum TaskKey { | ||||
|   ev = 1, | ||||
|   pm2, | ||||
|   deno, | ||||
|   bun, | ||||
|   silkyInit, | ||||
|   appCenter, | ||||
|   appTalkshowAdmin, | ||||
|   appTalkshow, | ||||
| } | ||||
| export const init = { | ||||
|   description: '安装依赖', | ||||
|   key: TaskKey.ev, | ||||
|   command: 'npm install -g @kevisual/cli --registry=https://registry.npmmirror.com', | ||||
|   type: 'npm-install', | ||||
|   before: 'ev -v', | ||||
|   beforeCheck: '.', | ||||
|   tags: ['env', 'must-env'], | ||||
| }; | ||||
| silkyCommand.addTask(init); | ||||
|  | ||||
| const init1 = { | ||||
|   description: '安装 pm2', | ||||
|   type: 'npm-install', | ||||
|   key: TaskKey.pm2, | ||||
|   command: 'npm install -g pm2 --registry=https://registry.npmmirror.com', | ||||
|   before: 'pm2 -v', | ||||
|   beforeCheck: '.', | ||||
|   tags: ['env', 'must-env'], | ||||
| }; | ||||
| silkyCommand.addTask(init1); | ||||
|  | ||||
| const init2 = { | ||||
|   description: '安装 deno', | ||||
|   command: 'npm install -g deno --registry=https://registry.npmmirror.com', | ||||
|   key: TaskKey.deno, | ||||
|   type: 'npm-install', | ||||
|   before: 'deno -v', | ||||
|   beforeCheck: 'deno', | ||||
|   tags: ['env'], | ||||
| }; | ||||
| silkyCommand.addTask(init2); | ||||
|  | ||||
| const init3 = { | ||||
|   description: '安装 bun', | ||||
|   type: 'npm-install', | ||||
|   key: TaskKey.bun, | ||||
|   command: 'npm install -g bun --registry=https://registry.npmmirror.com', | ||||
|   before: 'bun -v', | ||||
|   beforeCheck: '.', | ||||
|   tags: ['env'], | ||||
| }; | ||||
| silkyCommand.addTask(init3); | ||||
|  | ||||
| // =========================== | ||||
| // 安装talkshow的程序到本地 | ||||
|  | ||||
| const talkInit = { | ||||
|   description: '初始化一个助手客户端,生成配置文件。', | ||||
|   key: TaskKey.silkyInit, | ||||
|   command: 'silky init', | ||||
| }; | ||||
| silkyCommand.addTask(talkInit); | ||||
|  | ||||
| const task1 = { | ||||
|   description: '下载前端应用 root/center 应用', | ||||
|   key: TaskKey.appCenter, | ||||
|   command: 'asst app download -i root/center', | ||||
|   tags: ['app', 'root'], | ||||
| }; | ||||
| silkyCommand.addTask(task1); | ||||
|  | ||||
| const task2 = { | ||||
|   description: '下载前端应用 root/talkshow-admin 应用', | ||||
|   key: TaskKey.appTalkshowAdmin, | ||||
|   command: 'asst app download -i root/talkshow-admin', | ||||
|   tags: ['app', 'talkshow'], | ||||
| }; | ||||
| silkyCommand.addTask(task2); | ||||
|  | ||||
| const task3 = { | ||||
|   description: '安装后端应用 root/talkshow-code-center 应用', | ||||
|   key: TaskKey.appTalkshow, | ||||
|   command: 'asst app download -i root/talkshow-code-center -t app', | ||||
|   tags: ['app', 'talkshow'], | ||||
| }; | ||||
| silkyCommand.addTask(task3); | ||||
|  | ||||
| // =========================== | ||||
| const testTask = { | ||||
|   description: '测试', | ||||
|   command: 'npm i -g nodemon --registry=https://registry.npmmirror.com', | ||||
|   type: 'npm-install', | ||||
|   before: 'nodemon -v', | ||||
|   beforeCheck: '.', | ||||
|   key: 'test-task', | ||||
| }; | ||||
| silkyCommand.addTask(testTask); | ||||
|  | ||||
| const runTask1 = async () => { | ||||
|   const tasksCommand = new TasksCommand(); | ||||
|   tasksCommand.addTask(init2); | ||||
|   const res = tasksCommand.runTask(init2.description); | ||||
|   console.log(res); | ||||
| }; | ||||
| // runTask1(); | ||||
| const runTestTask = async () => { | ||||
|   const tasksCommand = new TasksCommand(); | ||||
|   tasksCommand.addTask(testTask); | ||||
|   const res = tasksCommand.runTask(testTask); | ||||
|   console.log(res); | ||||
|   return res; | ||||
| }; | ||||
| // runTestTask(); | ||||
							
								
								
									
										24
									
								
								assistant/tasks/silkyai-cli/src/program.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								assistant/tasks/silkyai-cli/src/program.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,24 @@ | ||||
| import { program, Command } from 'commander'; | ||||
| import fs from 'fs'; | ||||
| import './command/check/index.ts'; | ||||
|  | ||||
| // 将多个子命令加入主程序中 | ||||
| let version = '0.0.1'; | ||||
| try { | ||||
|   // @ts-ignore | ||||
|   if (ENVISION_VERSION) version = ENVISION_VERSION; | ||||
| } catch (e) {} | ||||
| // @ts-ignore | ||||
| program.name('silky').description('A CLI tool with silky').version(version, '-v, --version', 'output the current version'); | ||||
|  | ||||
| export { program, Command }; | ||||
|  | ||||
| /** | ||||
|  * 在命令行中运行程序 | ||||
|  * 当前命令参数去执行 其他的应用模块 | ||||
|  * @param args | ||||
|  */ | ||||
| export const runProgram = (args: string[]) => { | ||||
|   const [_app, _command] = process.argv; | ||||
|   program.parse([_app, _command, ...args]); | ||||
| }; | ||||
							
								
								
									
										6
									
								
								assistant/tasks/silkyai-cli/src/run.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								assistant/tasks/silkyai-cli/src/run.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,6 @@ | ||||
| import { runParser } from './index.ts'; | ||||
|  | ||||
| /** | ||||
|  * test run parser | ||||
|  */ | ||||
| runParser(process.argv); | ||||
							
								
								
									
										12
									
								
								assistant/tasks/silkyai-cli/src/services/init/index.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								assistant/tasks/silkyai-cli/src/services/init/index.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,12 @@ | ||||
| import { AssistantInit as BaseAssistantInit } from '@kevisual/assistant-cli/lib'; | ||||
|  | ||||
| export class AssistantInit extends BaseAssistantInit { | ||||
|   constructor(options: { path?: string }) { | ||||
|     super(options); | ||||
|   } | ||||
|   protected getDefaultInitAssistantConfig() { | ||||
|     const config = super.getDefaultInitAssistantConfig(); | ||||
|     config.registry = 'https://kevisual.silkyai.cn'; | ||||
|     return config; | ||||
|   } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user