feat: add silky cli tools
This commit is contained in:
@@ -2,7 +2,7 @@ import { execSync } from 'node:child_process';
|
||||
|
||||
export const TaskCommandType = ['npm-install'] as const;
|
||||
export type TaskCommand = {
|
||||
key?: string;
|
||||
key?: any;
|
||||
/**
|
||||
* 任务描述
|
||||
*/
|
||||
@@ -30,10 +30,38 @@ export type TaskCommand = {
|
||||
* 如果没有这个文本,表示任务没有安装
|
||||
*/
|
||||
beforeCheck?: string;
|
||||
tags?: string[];
|
||||
meta?: any;
|
||||
};
|
||||
type RunTaskResult = {
|
||||
code?: number;
|
||||
message?: string;
|
||||
data?: any;
|
||||
output?: string;
|
||||
task?: TaskCommand;
|
||||
};
|
||||
type TaskCommandOptions = {
|
||||
isDebug?: boolean;
|
||||
isLog?: boolean;
|
||||
};
|
||||
export class TasksCommand {
|
||||
tasks: Map<string, TaskCommand> = new Map();
|
||||
constructor() {}
|
||||
tasks: Map<string | number, TaskCommand> = new Map();
|
||||
isDebug: boolean = false;
|
||||
isLog: boolean = false;
|
||||
constructor(opts?: TaskCommandOptions) {
|
||||
this.isDebug = opts?.isDebug ?? false;
|
||||
this.isLog = opts?.isLog ?? false;
|
||||
}
|
||||
log(...args: any[]) {
|
||||
if (this.isLog) {
|
||||
console.log(...args);
|
||||
}
|
||||
}
|
||||
debug(...args: any[]) {
|
||||
if (this.isDebug) {
|
||||
console.log(...args);
|
||||
}
|
||||
}
|
||||
addTask(task: TaskCommand, run?: boolean) {
|
||||
const key = task?.key || task?.description;
|
||||
if (!key) {
|
||||
@@ -45,12 +73,17 @@ export class TasksCommand {
|
||||
}
|
||||
return this;
|
||||
}
|
||||
getTask(name: string) {
|
||||
getTask(name: string | number) {
|
||||
return this.tasks.get(name);
|
||||
}
|
||||
runTask(name: string) {
|
||||
const task = this.getTask(name);
|
||||
const end = (data?: { code: number; [key: string]: any }) => {
|
||||
runTask(taskName: string | number | TaskCommand): RunTaskResult {
|
||||
let task: TaskCommand | undefined;
|
||||
if (typeof taskName === 'string' || typeof taskName === 'number') {
|
||||
task = this.getTask(taskName);
|
||||
} else if (typeof taskName === 'object') {
|
||||
task = taskName;
|
||||
}
|
||||
const end = (data?: RunTaskResult) => {
|
||||
return {
|
||||
...data,
|
||||
task,
|
||||
@@ -59,7 +92,8 @@ export class TasksCommand {
|
||||
if (!task) {
|
||||
return {
|
||||
code: 500,
|
||||
message: `没有找到 ${name} 这个任务`,
|
||||
message: `没有找到这个任务`,
|
||||
task: task,
|
||||
};
|
||||
}
|
||||
let { command, before, after, afterCheck, beforeCheck, type } = task;
|
||||
@@ -68,8 +102,9 @@ export class TasksCommand {
|
||||
}
|
||||
if (before) {
|
||||
const res = this.runCommand(before);
|
||||
console.log('before', res, beforeCheck, this.checkForContainue(res, beforeCheck));
|
||||
if (!this.checkForContainue(res, beforeCheck)) {
|
||||
const checkForContainue = this.checkForContainue(res, beforeCheck);
|
||||
this.debug('运行前检测', { runCommandResult: res, beforeCheck, checkForContainue });
|
||||
if (!checkForContainue) {
|
||||
return end({
|
||||
code: 200,
|
||||
message: `当前任务不需要执行, ${command}`,
|
||||
@@ -77,12 +112,14 @@ export class TasksCommand {
|
||||
}
|
||||
}
|
||||
const res = this.runCommand(command);
|
||||
console.log('runCommand', res);
|
||||
this.debug(`执行任务:${command}`, { runCommandResult: res });
|
||||
if (res.code !== 200) {
|
||||
this.debug('当前任务执行失败:', { runCommandResult: res });
|
||||
return end(res);
|
||||
}
|
||||
let checkText = res.data || '';
|
||||
if (after) {
|
||||
this.debug('执行后检测是否成功:', { after });
|
||||
const res = this.runCommand(after);
|
||||
if (res.code !== 200) {
|
||||
return end(res);
|
||||
@@ -91,10 +128,11 @@ export class TasksCommand {
|
||||
}
|
||||
if (afterCheck) {
|
||||
const isSuccess = checkText?.includes?.(afterCheck);
|
||||
this.debug('执行后检测是否成功:', { afterCheck, checkText, isSuccess });
|
||||
return end({
|
||||
code: isSuccess ? 200 : 500,
|
||||
output: res.data,
|
||||
check: afterCheck,
|
||||
data: afterCheck,
|
||||
message: isSuccess ? `当前任务执行成功, ${command}` : `当前任务执行失败, ${command}`,
|
||||
});
|
||||
}
|
||||
@@ -139,4 +177,16 @@ export class TasksCommand {
|
||||
};
|
||||
}
|
||||
}
|
||||
getTasksArray() {
|
||||
const tasks = Array.from(this.tasks.values());
|
||||
return tasks;
|
||||
}
|
||||
/**
|
||||
* 根据标签获取任务
|
||||
* @param tag
|
||||
*/
|
||||
getTasksArrayByTag(tag?: string) {
|
||||
const tasks = Array.from(this.tasks.values());
|
||||
return tasks.filter((task) => !tag || task.tags?.includes(tag) || task.meta?.tags?.includes(tag) || task.key === tag);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user