feat: 升级midway,支持esm

This commit is contained in:
xiaojunnuo
2024-07-15 00:30:33 +08:00
parent 970c7fd8a0
commit 485e603b51
246 changed files with 3821 additions and 1532 deletions
+2 -2
View File
@@ -1,5 +1,5 @@
import { Registrable } from "../registry";
import { FormItemProps } from "../d.ts";
import { Registrable } from "../registry/index.js";
import { FormItemProps } from "../dt/index.js";
export type AccessInputDefine = FormItemProps & {
title: string;
@@ -1,8 +1,8 @@
// src/decorator/memoryCache.decorator.ts
import { AccessDefine, AccessInputDefine } from "./api";
import { Decorator } from "../decorator";
import _ from "lodash";
import { accessRegistry } from "./registry";
import { AccessDefine, AccessInputDefine } from "./api.js";
import { Decorator } from "../decorator/index.js";
import _ from "lodash-es";
import { accessRegistry } from "./registry.js";
// 提供一个唯一 key
export const ACCESS_CLASS_KEY = "pipeline:access";
+3 -3
View File
@@ -1,3 +1,3 @@
export * from "./api";
export * from "./registry";
export * from "./decorator";
export * from "./api.js";
export * from "./registry.js";
export * from "./decorator.js";
@@ -1,4 +1,4 @@
import { Registry } from "../registry";
import { Registry } from "../registry/index.js";
// @ts-ignore
export const accessRegistry = new Registry("access");
+1 -1
View File
@@ -1,5 +1,5 @@
import { AxiosInstance } from "axios";
import { IContext } from "../core";
import { IContext } from "../core/index.js";
export type HttpClient = AxiosInstance;
export type UserContext = IContext;
+1 -1
View File
@@ -1,4 +1,4 @@
import { IStorage, MemoryStorage } from "./storage";
import { IStorage, MemoryStorage } from "./storage.js";
const CONTEXT_VERSION_KEY = "contextVersion";
export interface IContext {
getInt(key: string): Promise<number>;
+31 -24
View File
@@ -1,18 +1,18 @@
import { ConcurrencyStrategy, NotificationWhen, Pipeline, ResultType, Runnable, RunStrategy, Stage, Step, Task } from "../d.ts";
import _ from "lodash";
import { RunHistory, RunnableCollection } from "./run-history";
import { AbstractTaskPlugin, PluginDefine, pluginRegistry, TaskInstanceContext } from "../plugin";
import { ContextFactory, IContext } from "./context";
import { IStorage } from "./storage";
import { logger } from "../utils/util.log";
import { ConcurrencyStrategy, NotificationWhen, Pipeline, ResultType, Runnable, RunStrategy, Stage, Step, Task } from "../dt/index.js";
import _ from "lodash-es";
import { RunHistory, RunnableCollection } from "./run-history.js";
import { AbstractTaskPlugin, PluginDefine, pluginRegistry, TaskInstanceContext } from "../plugin/index.js";
import { ContextFactory, IContext } from "./context.js";
import { IStorage } from "./storage.js";
import { logger } from "../utils/util.log.js";
import { Logger } from "log4js";
import { createAxiosService } from "../utils/util.request";
import { IAccessService } from "../access";
import { RegistryItem } from "../registry";
import { Decorator } from "../decorator";
import { IEmailService } from "../service";
import { FileStore } from "./file-store";
import { TimeoutPromise } from "../utils/util.promise";
import { createAxiosService } from "../utils/util.request.js";
import { IAccessService } from "../access/index.js";
import { RegistryItem } from "../registry/index.js";
import { Decorator } from "../decorator/index.js";
import { IEmailService } from "../service/index.js";
import { FileStore } from "./file-store.js";
// import { TimeoutPromise } from "../utils/util.promise.js";
export type ExecutorOptions = {
userId: any;
@@ -33,7 +33,7 @@ export class Executor {
lastRuntime!: RunHistory;
options: ExecutorOptions;
canceled = false;
onChanged: (history: RunHistory) => void;
onChanged: (history: RunHistory) => Promise<void>;
constructor(options: ExecutorOptions) {
this.options = options;
this.pipeline = _.cloneDeep(options.pipeline);
@@ -110,12 +110,13 @@ export class Executor {
const intervalFlushLogId = setInterval(async () => {
await this.onChanged(this.runtime);
}, 5000);
const timeout = runnable.timeout ?? 20 * 60 * 1000;
// const timeout = runnable.timeout ?? 20 * 60 * 1000;
try {
if (this.canceled) {
throw new Error("task canceled");
return ResultType.canceled;
}
await TimeoutPromise(run, timeout);
await run();
this.runtime.success(runnable);
return ResultType.success;
} catch (e: any) {
@@ -142,19 +143,25 @@ export class Executor {
async runStage(stage: Stage) {
const runnerList = [];
for (const task of stage.tasks) {
const runner = this.runWithHistory(task, "task", async () => {
await this.runTask(task);
});
const runner = async () => {
return this.runWithHistory(task, "task", async () => {
await this.runTask(task);
});
};
runnerList.push(runner);
}
let resList: ResultType[] = [];
if (stage.concurrency === ConcurrencyStrategy.Parallel) {
resList = await Promise.all(runnerList);
const pList = [];
for (const item of runnerList) {
pList.push(item());
}
resList = await Promise.all(pList);
} else {
for (let i = 0; i < runnerList.length; i++) {
const runner = runnerList[i];
resList[i] = await runner;
resList[i] = await runner();
}
}
return this.compositionResultType(resList);
@@ -239,7 +246,7 @@ export class Executor {
this.lastStatusMap.clear();
}
//输出到output context
_.forEach(define.output, (item, key) => {
_.forEach(define.output, (item: any, key: any) => {
step.status!.output[key] = instance[key];
const stepOutputKey = `step.${step.id}.${key}`;
this.runtime.context[stepOutputKey] = instance[key];
@@ -1,8 +1,8 @@
import { fileUtils } from "../utils";
import { fileUtils } from "../utils/index.js";
import dayjs from "dayjs";
import path from "path";
import fs from "fs";
import { logger } from "../utils";
import { logger } from "../utils/index.js";
export type FileStoreOptions = {
rootDir?: string;
+5 -5
View File
@@ -1,5 +1,5 @@
export * from "./executor";
export * from "./run-history";
export * from "./context";
export * from "./storage";
export * from "./file-store";
export * from "./executor.js";
export * from "./run-history.js";
export * from "./context.js";
export * from "./storage.js";
export * from "./file-store.js";
@@ -1,6 +1,6 @@
import { Context, HistoryResult, Pipeline, ResultType, Runnable, RunnableMap, Stage, Step, Task } from "../d.ts";
import _ from "lodash";
import { buildLogger } from "../utils/util.log";
import { Context, HistoryResult, Pipeline, ResultType, Runnable, RunnableMap, Stage, Step, Task } from "../dt/index.js";
import _ from "lodash-es";
import { buildLogger } from "../utils/util.log.js";
import { Logger } from "log4js";
export type HistoryStatus = {
+1 -1
View File
@@ -1,6 +1,6 @@
import fs from "fs";
import path from "path";
import { fileUtils } from "../utils/util.file";
import { fileUtils } from "../utils/util.file.js";
export interface IStorage {
get(scope: string, namespace: string, version: string, key: string): Promise<string | null>;
@@ -70,6 +70,7 @@ export type Runnable = {
default?: {
[key: string]: any;
};
context?: Context;
};
export type EmailOptions = {
@@ -1,4 +1,4 @@
import { Decorator } from "./index";
import { Decorator } from "./index.js";
export type AutowireProp = {
name?: string;
@@ -1,2 +1,2 @@
export * from "./utils";
export * from "./common";
export * from "./utils.js";
export * from "./common.js";
@@ -1,4 +1,4 @@
import _ from "lodash";
import _ from "lodash-es";
const propertyMap: any = {};
function attachProperty(target: any, propertyKey: string | symbol) {
@@ -25,7 +25,7 @@ function target(target: any, propertyKey?: string | symbol) {
}
function inject(define: any, instance: any, context: any, preHandler?: (item: any, key: string, instance: any, context: any) => void) {
_.forEach(define, (item, key) => {
_.forEach(define, (item: any, key: any) => {
if (preHandler) {
preHandler(item, key, instance, context);
}
+115
View File
@@ -0,0 +1,115 @@
/**
* [x]-col的配置
*/
export type ColProps = {
span?: number;
[props: string]: any;
};
export type FormItemProps = {
/**
* 字段label
*/
title?: string;
/**
* 表单字段组件配置
*/
component?: ComponentProps;
/**
* 表单字段 [a|el|n]-col的配置
* 一般用来配置跨列:{span:24} 占满一行
*/
col?: ColProps;
/**
* 默认值
*/
value?: any;
/**
* 帮助提示配置
*/
helper?: string | FormItemHelperProps;
/**
* 排序号
*/
order?: number;
/**
* 是否显示此字段
*/
show?: boolean;
/**
* 是否是空白占位栏
*/
blank?: boolean;
[key: string]: any;
};
/**
* 表单字段帮助说明配置
*/
export type FormItemHelperProps = {
/**
* 自定义渲染帮助说明
* @param scope
*/
render?: (scope: any) => any;
/**
* 帮助文本
*/
text?: string;
/**
* 帮助说明所在的位置,[ undefined | label]
*/
position?: string;
/**
* [a|el|n]-tooltip配置
*/
tooltip?: object;
[key: string]: any;
};
/**
* 组件配置
*/
export type ComponentProps = {
/**
* 组件的名称
*/
name?: string | object;
/**
* vmodel绑定的目标属性名
*/
vModel?: string;
/**
* 当原始组件名的参数被以上属性名占用时,可以配置在这里
* 例如:原始组件有一个叫name的属性,你想要配置它,则可以按如下配置
* ```
* component:{
* name:"组件的名称"
* props:{
* name:"组件的name属性" <-----------
* }
* }
* ```
*/
props?: {
[key: string]: any;
};
/**
* 组件事件监听
*/
on?: {
[key: string]: (context?: any) => void;
};
/**
* 组件其他参数
* 事件:onXxx:(event)=>void 组件原始事件监听
* on.onXxx:(context)=>void 组件事件监听(对原始事件包装)
* 样式:style、class等
*/
[key: string]: any;
};
+2
View File
@@ -0,0 +1,2 @@
export * from "./pipeline.js";
export * from "./fast-crud.js";
+139
View File
@@ -0,0 +1,139 @@
export enum RunStrategy {
AlwaysRun,
SkipWhenSucceed,
}
export enum ConcurrencyStrategy {
Serial,
Parallel,
}
export enum NextStrategy {
AllSuccess,
OneSuccess,
}
export enum HandlerType {
//清空后续任务的状态
ClearFollowStatus,
SendEmail,
}
export type EventHandler = {
type: HandlerType;
params: {
[key: string]: any;
};
};
export type RunnableStrategy = {
runStrategy?: RunStrategy;
onSuccess?: EventHandler[];
onError?: EventHandler[];
};
export type Step = Runnable & {
type: string; //插件类型
input: {
[key: string]: any;
};
};
export type Task = Runnable & {
steps: Step[];
};
export type Stage = Runnable & {
tasks: Task[];
concurrency: ConcurrencyStrategy;
next: NextStrategy;
};
export type Trigger = {
id: string;
title: string;
cron: string;
type: string;
};
export type FileItem = {
id: string;
filename: string;
path: string;
};
export type Runnable = {
id: string;
title: string;
strategy?: RunnableStrategy;
runnableType?: string; // pipeline, stage, task , step
status?: HistoryResult;
timeout?: number;
default?: {
[key: string]: any;
};
};
export type EmailOptions = {
receivers: string[];
};
export type NotificationWhen = "error" | "success" | "turnToSuccess" | "start";
export type NotificationType = "email" | "url";
export type Notification = {
type: NotificationType;
when: NotificationWhen[];
options: EmailOptions;
};
export type Pipeline = Runnable & {
version?: number;
userId: any;
stages: Stage[];
triggers: Trigger[];
notifications?: Notification[];
};
export type Context = {
[key: string]: any;
};
export type Log = {
title: string;
time: number;
level: string;
text: string;
};
export enum ResultType {
start = "start",
success = "success",
error = "error",
canceled = "canceled",
skip = "skip",
none = "none",
}
export type HistoryResultGroup = {
[key: string]: {
runnable: Runnable;
res: HistoryResult;
};
};
export type HistoryResult = {
input: any;
output: any;
files?: FileItem[];
/**
* 任务状态
*/
status: ResultType;
startTime: number;
endTime?: number;
/**
* 处理结果
*/
result?: ResultType; //success, error,skip
message?: string;
};
export type RunnableMap = {
[id: string]: Runnable;
};
+9 -9
View File
@@ -1,10 +1,10 @@
import "util";
export * from "./core";
export * from "./d.ts";
export * from "./access";
export * from "./registry";
export * from "./plugin";
export * from "./utils";
export * from "./context";
export * from "./decorator";
export * from "./service";
export * from "./core/index.js";
export * from "./dt/index.js";
export * from "./access/index.js";
export * from "./registry/index.js";
export * from "./plugin/index.js";
export * from "./utils/index.js";
export * from "./context/index.js";
export * from "./decorator/index.js";
export * from "./service/index.js";
+16 -9
View File
@@ -1,12 +1,12 @@
import { Registrable } from "../registry";
import { FileItem, FormItemProps, Pipeline, Runnable, Step } from "../d.ts";
import { FileStore } from "../core/file-store";
import { Registrable } from "../registry/index.js";
import { FileItem, FormItemProps, Pipeline, Runnable, Step } from "../dt/index.js";
import { FileStore } from "../core/file-store.js";
import { Logger } from "log4js";
import { IAccessService } from "../access";
import { IEmailService } from "../service";
import { IContext } from "../core";
import { IAccessService } from "../access/index.js";
import { IEmailService } from "../service/index.js";
import { IContext } from "../core/index.js";
import { AxiosInstance } from "axios";
import { logger } from "../utils";
import { logger } from "../utils/index.js";
export enum ContextScope {
global,
@@ -83,7 +83,7 @@ export abstract class AbstractTaskPlugin implements ITaskPlugin {
return Math.random().toString(36).substring(2, 9);
}
linkFile(file: FileItem) {
this._result.files!.push({
this._result.files?.push({
...file,
id: this.randomFileId(),
});
@@ -91,13 +91,20 @@ export abstract class AbstractTaskPlugin implements ITaskPlugin {
saveFile(filename: string, file: Buffer) {
const filePath = this.ctx.fileStore.writeFile(filename, file);
logger.info(`saveFile:${filePath}`);
this._result.files!.push({
this._result.files?.push({
id: this.randomFileId(),
filename,
path: filePath,
});
}
extendsFiles() {
if (this._result.files == null) {
this._result.files = [];
}
this._result.files.push(...(this.ctx.lastStatus?.status?.files || []));
}
get pipeline() {
return this.ctx.pipeline;
}
@@ -1,8 +1,8 @@
import _ from "lodash";
import { pluginRegistry } from "./registry";
import { PluginDefine, TaskInputDefine, TaskOutputDefine } from "./api";
import { Decorator } from "../decorator";
import { AUTOWIRE_KEY } from "../decorator";
import _ from "lodash-es";
import { pluginRegistry } from "./registry.js";
import { PluginDefine, TaskInputDefine, TaskOutputDefine } from "./api.js";
import { Decorator } from "../decorator/index.js";
import { AUTOWIRE_KEY } from "../decorator/index.js";
import "reflect-metadata";
// 提供一个唯一 key
export const PLUGIN_CLASS_KEY = "pipeline:plugin";
+3 -3
View File
@@ -1,3 +1,3 @@
export * from "./api";
export * from "./registry";
export * from "./decorator";
export * from "./api.js";
export * from "./registry.js";
export * from "./decorator.js";
@@ -1,4 +1,4 @@
import { Registry } from "../registry";
import { AbstractTaskPlugin } from "./api";
import { Registry } from "../registry/index.js";
import { AbstractTaskPlugin } from "./api.js";
export const pluginRegistry = new Registry<AbstractTaskPlugin>("plugin");
@@ -1,6 +1,5 @@
import { ITaskPlugin } from "../api";
import { IsTaskPlugin, TaskInput } from "../decorator";
import { Autowire } from "../../decorator";
import { ITaskPlugin } from "../api.js";
import { IsTaskPlugin, TaskInput } from "../decorator.js";
@IsTaskPlugin({
name: "EchoPlugin",
+1 -1
View File
@@ -1 +1 @@
export * from "./registry";
export * from "./registry.js";
@@ -1,4 +1,4 @@
import { logger } from "../utils";
import { logger } from "../utils/index.js";
export type Registrable = {
name: string;
+1 -1
View File
@@ -1 +1 @@
export * from "./email";
export * from "./email.js";
+4 -4
View File
@@ -1,7 +1,7 @@
import sleep from "./util.sleep";
import { request } from "./util.request";
export * from "./util.log";
export * from "./util.file";
import sleep from "./util.sleep.js";
import { request } from "./util.request.js";
export * from "./util.log.js";
export * from "./util.file.js";
export const utils = {
sleep,
http: request,
@@ -1,7 +1,7 @@
import axios from "axios";
// @ts-ignore
import qs from "qs";
import { logger } from "./util.log";
import { logger } from "./util.log.js";
import { Logger } from "log4js";
/**
* @description 创建请求实例