feat: add silky cli tools

This commit is contained in:
2025-04-27 22:16:09 +08:00
parent 75d181ef43
commit 6867de838e
42 changed files with 3867 additions and 251 deletions

View File

@@ -0,0 +1,2 @@
//npm.xiongxiao.me/:_authToken=${ME_NPM_TOKEN}
//registry.npmjs.org/:_authToken=${NPM_TOKEN}

View File

@@ -0,0 +1,4 @@
#!/usr/bin/env node
import { runParser } from '../dist/silky.mjs';
runParser(process.argv);

View File

@@ -0,0 +1,26 @@
import path from 'node:path';
import pkg from './package.json';
import fs from 'node:fs';
// bun run src/index.ts --
import { fileURLToPath } from 'node:url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
/**
*
* @param {string} p
* @returns
*/
export const w = (p) => path.join(__dirname, p);
await Bun.build({
target: 'node',
format: 'esm',
entrypoints: [w('./src/index.ts')],
outdir: w('./dist'),
naming: {
entry: 'silky.mjs',
},
external: ['pm2'],
define: {
ENVISION_VERSION: JSON.stringify(pkg.version),
},
env: 'ENVISION_*',
});

View File

@@ -0,0 +1,40 @@
{
"name": "@kevisual/silky-cli",
"version": "0.0.3",
"description": "",
"main": "index.js",
"scripts": {
"dev": "bun run src/run.ts ",
"build": "bun run bun.config.mjs"
},
"bin": {
"silky": "./bin/silky.js"
},
"keywords": [],
"author": "abearxiong <xiongxiao@xiongxiao.me> (https://www.xiongxiao.me)",
"license": "MIT",
"packageManager": "pnpm@10.9.0",
"type": "module",
"publishConfig": {
"access": "public"
},
"files": [
"bin",
"dist"
],
"devDependencies": {
"@kevisual/assistant-cli": "workspace:*",
"@kevisual/task-command": "^0.0.7",
"chalk": "^5.4.1",
"commander": "^13.1.0",
"cross-env": "^7.0.3",
"dayjs": "^1.11.13",
"dotenv": "^16.5.0",
"inquirer": "^12.6.0",
"lodash-es": "^4.17.21",
"nanoid": "^5.1.5"
},
"dependencies": {
"pm2": "^6.0.5"
}
}

View File

@@ -0,0 +1,2 @@
import './init.ts'
import './init-env.ts'

View 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);

View 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);

View 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,
});

View 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 };

View 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();

View 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]);
};

View File

@@ -0,0 +1,6 @@
import { runParser } from './index.ts';
/**
* test run parser
*/
runParser(process.argv);

View 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;
}
}

View File

@@ -0,0 +1,14 @@
{
"extends": "@kevisual/types/json/backend.json",
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": [
"src/*"
]
},
},
"include": [
"src/**/*",
],
}

View File

@@ -1,74 +0,0 @@
import { TasksCommand } from 'https://esm.xiongxiao.me/@kevisual/task-command/mod.ts';
// import { TasksCommand } from '../../task-command/mod.ts';
const init = {
description: '安装依赖',
command: 'npm install -g @kevisual/cli --registry=https://registry.npmmirror.com',
afterCheck: 'added',
};
const init1 = {
description: '安装 pm2',
type: 'npm-install',
command: 'npm install -g pm2 --registry=https://registry.npmmirror.com',
before: 'pm2 -v',
};
const init2 = {
description: '安装 deno',
command: 'npm install -g deno --registry=https://registry.npmmirror.com',
type: 'npm-install',
before: 'deno -v',
beforeCheck: 'deno',
};
const init3 = {
description: '安装 bun',
type: 'npm-install',
command: 'npm install -g bun --registry=https://registry.npmmirror.com',
beforeCheck: 'bun -v',
};
// ===========================
// 安装talkshow的程序到本地
const talk = {
description: '设置默认的 base registry 地址 为 https://kevisual.silkyai.cn',
command: 'ev base -s https://kevisual.silkyai.cn',
};
const talkInit = {
description: '初始化一个助手客户端,生成配置文件。',
command: 'asst init',
};
const task1 = {
description: '下载前端应用 root/center 应用',
command: 'ev app download -i root/center -o assistant-app/page',
};
const task2 = {
description: '下载前端应用 root/talkshow-admin 应用',
command: 'ev app download -i root/talkshow-admin -o assistant-app/page',
};
const task3 = {
description: '安装后端应用 root/talkshow-code-center 应用',
command: 'ev app download -i root/talkshow-code-center -t app -o assistant-app/apps/talkshow-code-center',
};
// ===========================
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();
const task = {
description: 'test',
command: 'npm i -g rollup --registry=https://registry.npmmirror.com',
type: 'npm-install',
};
tasksCommand.addTask(task);
const res = tasksCommand.runTask(task.description);
console.log(res);
return res;
};
runTestTask();