@ruan-cat/utils 4.7.0 → 4.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/node-cjs/index.cjs
CHANGED
|
@@ -31,8 +31,18 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
31
31
|
var index_exports = {};
|
|
32
32
|
__export(index_exports, {
|
|
33
33
|
defPrintCurrentCommand: () => defPrintCurrentCommand,
|
|
34
|
+
definePromiseTasks: () => definePromiseTasks,
|
|
35
|
+
executePromiseTasks: () => executePromiseTasks,
|
|
36
|
+
generateSimpleAsyncTask: () => generateSimpleAsyncTask,
|
|
34
37
|
generateSpawnSync: () => generateSpawnSync,
|
|
35
|
-
|
|
38
|
+
initFlag: () => initFlag,
|
|
39
|
+
isConditionsEvery: () => isConditionsEvery,
|
|
40
|
+
isConditionsSome: () => isConditionsSome,
|
|
41
|
+
pathChange: () => pathChange,
|
|
42
|
+
printFormat: () => printFormat,
|
|
43
|
+
runPromiseByConcurrency: () => runPromiseByConcurrency,
|
|
44
|
+
runPromiseByQueue: () => runPromiseByQueue,
|
|
45
|
+
taskTypes: () => taskTypes
|
|
36
46
|
});
|
|
37
47
|
module.exports = __toCommonJS(index_exports);
|
|
38
48
|
|
|
@@ -47,6 +57,16 @@ function generateSimpleAsyncTask(func) {
|
|
|
47
57
|
});
|
|
48
58
|
};
|
|
49
59
|
}
|
|
60
|
+
var initFlag = "initFlag";
|
|
61
|
+
async function runPromiseByQueue(promises) {
|
|
62
|
+
let response = initFlag;
|
|
63
|
+
for await (const promise of promises) {
|
|
64
|
+
response = await promise(response);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
async function runPromiseByConcurrency(promises) {
|
|
68
|
+
await Promise.all(promises.map((promise) => promise()));
|
|
69
|
+
}
|
|
50
70
|
|
|
51
71
|
// src/node-cjs/tools.ts
|
|
52
72
|
var import_consola = __toESM(require("consola"), 1);
|
|
@@ -91,10 +111,90 @@ function generateSpawnSync(spawnSyncSimpleParams) {
|
|
|
91
111
|
return result;
|
|
92
112
|
});
|
|
93
113
|
}
|
|
114
|
+
|
|
115
|
+
// src/conditions.ts
|
|
116
|
+
function isConditionsEvery(conditions) {
|
|
117
|
+
return conditions.every((condition) => condition());
|
|
118
|
+
}
|
|
119
|
+
function isConditionsSome(conditions) {
|
|
120
|
+
return conditions.some((condition) => condition());
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// src/print.ts
|
|
124
|
+
var import_lodash_es = require("lodash-es");
|
|
125
|
+
function printFormat(params) {
|
|
126
|
+
const boolRes = isConditionsSome([
|
|
127
|
+
// 是不是纯对象?
|
|
128
|
+
() => (0, import_lodash_es.isPlainObject)(params),
|
|
129
|
+
// 或者是不是单纯的数组?
|
|
130
|
+
() => (0, import_lodash_es.isArray)(params)
|
|
131
|
+
]);
|
|
132
|
+
const res = boolRes ? JSON.stringify(params, null, 2) : params;
|
|
133
|
+
return res;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// src/define-promise-tasks.ts
|
|
137
|
+
var taskTypes = ["single", "parallel", "queue"];
|
|
138
|
+
function isSingleTasks(config) {
|
|
139
|
+
return config.type === "single";
|
|
140
|
+
}
|
|
141
|
+
function isParallelTasks(config) {
|
|
142
|
+
return config.type === "parallel";
|
|
143
|
+
}
|
|
144
|
+
function isQueueTasks(config) {
|
|
145
|
+
return config.type === "queue";
|
|
146
|
+
}
|
|
147
|
+
function isSimpleAsyncTask(config) {
|
|
148
|
+
return typeof config === "function";
|
|
149
|
+
}
|
|
150
|
+
function definePromiseTasks(config) {
|
|
151
|
+
return config;
|
|
152
|
+
}
|
|
153
|
+
async function executePromiseTasks(config, lastParams = null) {
|
|
154
|
+
if (isSingleTasks(config)) {
|
|
155
|
+
if (isSimpleAsyncTask(config.tasks)) {
|
|
156
|
+
return await config.tasks(lastParams);
|
|
157
|
+
}
|
|
158
|
+
return await executePromiseTasks(config.tasks, lastParams);
|
|
159
|
+
}
|
|
160
|
+
if (isParallelTasks(config)) {
|
|
161
|
+
return await Promise.all(
|
|
162
|
+
config.tasks.map((task) => {
|
|
163
|
+
if (isSimpleAsyncTask(task)) {
|
|
164
|
+
return task(lastParams);
|
|
165
|
+
}
|
|
166
|
+
return executePromiseTasks(task, lastParams);
|
|
167
|
+
})
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
if (isQueueTasks(config)) {
|
|
171
|
+
let res;
|
|
172
|
+
for await (const task of config.tasks) {
|
|
173
|
+
if (isSimpleAsyncTask(task)) {
|
|
174
|
+
res = await task(lastParams);
|
|
175
|
+
lastParams = res;
|
|
176
|
+
} else {
|
|
177
|
+
res = await executePromiseTasks(task, lastParams);
|
|
178
|
+
lastParams = res;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return res;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
94
184
|
// Annotate the CommonJS export names for ESM import in node:
|
|
95
185
|
0 && (module.exports = {
|
|
96
186
|
defPrintCurrentCommand,
|
|
187
|
+
definePromiseTasks,
|
|
188
|
+
executePromiseTasks,
|
|
189
|
+
generateSimpleAsyncTask,
|
|
97
190
|
generateSpawnSync,
|
|
98
|
-
|
|
191
|
+
initFlag,
|
|
192
|
+
isConditionsEvery,
|
|
193
|
+
isConditionsSome,
|
|
194
|
+
pathChange,
|
|
195
|
+
printFormat,
|
|
196
|
+
runPromiseByConcurrency,
|
|
197
|
+
runPromiseByQueue,
|
|
198
|
+
taskTypes
|
|
99
199
|
});
|
|
100
200
|
//# sourceMappingURL=index.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/node-cjs/index.ts","../../src/node-cjs/tools.ts","../../src/simple-promise-tools.ts"],"sourcesContent":["export * from \"./tools\";\n","/**\n * 一些node环境下的工具函数\n */\n\nimport { normalize } from \"node:path\";\nimport { spawnSync, type SpawnOptions } from \"node:child_process\";\n\nimport { generateSimpleAsyncTask } from \"../simple-promise-tools\";\nimport consola from \"consola\";\n// import { normalizePath } from \"vite\";\n\n/**\n * 路径转换工具\n */\nexport function pathChange(path: string) {\n\treturn path.replace(/\\\\/g, \"/\");\n\t// FIXME: 无法有效地实现解析路径 测试用例不通过\n\t// return normalize(path);\n\t// FIXME: tsup打包时,无法处理好vite的依赖 会导致打包失败 不知道怎么单独使用并打包该函数\n\t// return normalizePath(path);\n}\n\nexport interface SpawnSyncSimpleParams {\n\tcommand: string;\n\tparameters: string[];\n\t/**\n\t * 是否流式输出内容\n\t * @description 默认输出的命令数据全部以流式的方式输出\n\t * @default true\n\t */\n\tisFlow?: boolean;\n\n\t/**\n\t * 是否显示命令?\n\t * @description\n\t * 是否打印目前正在执行的命令?\n\t * @default true\n\t */\n\tisShowCommand?: boolean;\n\tspawnOptions?: SpawnOptions;\n\n\t/** 打印当前运行的命令 */\n\tprintCurrentCommand?: (params: Pick<SpawnSyncSimpleParams, \"command\" | \"parameters\">) => void;\n}\n\n/**\n * 默认的打印当前运行命令 函数\n */\nexport const defPrintCurrentCommand: SpawnSyncSimpleParams[\"printCurrentCommand\"] = function (params) {\n\tconst { command, parameters } = params;\n\tconsola.info(` 当前运行的命令为: ${command} ${parameters.join(\" \")} \\n`);\n};\n\n/**\n * 生成简单的执行命令函数\n * @description\n * 对 spawnSync 做简单的包装\n *\n * 之前封装的是 execa 函数\n * @version 2\n */\nexport function generateSpawnSync(spawnSyncSimpleParams: SpawnSyncSimpleParams) {\n\tconst {\n\t\tcommand,\n\t\tparameters,\n\t\tisFlow = true,\n\t\tisShowCommand = true,\n\t\tspawnOptions = {},\n\t\tprintCurrentCommand = defPrintCurrentCommand,\n\t} = spawnSyncSimpleParams;\n\n\tif (isShowCommand) {\n\t\tprintCurrentCommand?.({ command, parameters });\n\t}\n\n\treturn generateSimpleAsyncTask(() => {\n\t\tconst result = spawnSync(command, parameters, {\n\t\t\t/**\n\t\t\t * 是否流式输出?\n\t\t\t * 是流式输出就是继承父进程的流式输出\n\t\t\t * 否则就使用默认值\n\t\t\t * @see https://nodejs.org/api/child_process.html#optionsstdio\n\t\t\t */\n\t\t\tstdio: isFlow ? \"inherit\" : \"pipe\",\n\t\t\tshell: true,\n\t\t\t...spawnOptions,\n\t\t});\n\n\t\t// 如果不是流式输出 就直接返回返回值即可\n\t\tif (!isFlow) {\n\t\t\treturn result;\n\t\t}\n\n\t\tif (result.error) {\n\t\t\tthrow result.error;\n\t\t}\n\n\t\treturn result;\n\t});\n}\n","// import { uniqueId } from \"lodash-es\";\n// const getCounter = () => uniqueId();\n\n/** 创建简单的异步任务 */\nexport function generateSimpleAsyncTask<T extends (...args: any) => any>(func: T) {\n\t// const taskId = getCounter();\n\n\treturn function (...args: any) {\n\t\t// consola.info(` 这是第 ${taskId} 个异步任务 `);\n\t\t// consola.start(\" 这里是新创建的异步函数 检查参数: \", ...args);\n\n\t\treturn new Promise<ReturnType<T>>((resolve, reject) => {\n\t\t\t// consola.start(\" 内部promise 检查参数: \", ...args);\n\t\t\tresolve(func(...args));\n\t\t});\n\t};\n}\n\nexport type SimpleAsyncTask = ReturnType<typeof generateSimpleAsyncTask>;\n\nexport const initFlag = <const>\"initFlag\";\n\n/**\n * 以队列串行的形式 串行运行异步函数\n * @see https://github.com/ascoders/weekly/blob/master/前沿技术/77.精读《用%20Reduce%20实现%20Promise%20串行执行》.md\n * @version 1\n */\nasync function runPromiseByQueueV1<T>(promises: ((...args: any) => Promise<T>)[]) {\n\tpromises.reduce(\n\t\tasync function (previousPromise, nextPromise, currentIndex) {\n\t\t\tconst response = await previousPromise;\n\t\t\t// consola.log(` reduce串行函数 currentIndex= ${currentIndex} res =`, response);\n\t\t\treturn await nextPromise(response);\n\t\t},\n\t\tPromise.resolve(initFlag) as Promise<any>,\n\t);\n}\n\n/**\n * 以队列串行的形式 串行运行异步函数\n * @version 2\n */\nexport async function runPromiseByQueue<T>(promises: ((...args: any) => Promise<T>)[]) {\n\tlet response: typeof initFlag | Awaited<T> = initFlag;\n\tfor await (const promise of promises) {\n\t\tresponse = await promise(response);\n\t}\n}\n\n/**\n * 以并行的形式 并发运行异步函数\n */\nexport async function runPromiseByConcurrency<T>(promises: ((...args: any) => Promise<T>)[]) {\n\tawait Promise.all(promises.map((promise) => promise()));\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACKA,gCAA6C;;;ACDtC,SAAS,wBAAyD,MAAS;AAGjF,SAAO,YAAa,MAAW;AAI9B,WAAO,IAAI,QAAuB,CAAC,SAAS,WAAW;AAEtD,cAAQ,KAAK,GAAG,IAAI,CAAC;AAAA,IACtB,CAAC;AAAA,EACF;AACD;;;ADRA,qBAAoB;AAMb,SAAS,WAAW,MAAc;AACxC,SAAO,KAAK,QAAQ,OAAO,GAAG;AAK/B;AA4BO,IAAM,yBAAuE,SAAU,QAAQ;AACrG,QAAM,EAAE,SAAS,WAAW,IAAI;AAChC,iBAAAA,QAAQ,KAAK,2DAAc,OAAO,IAAI,WAAW,KAAK,GAAG,CAAC;AAAA,CAAK;AAChE;AAUO,SAAS,kBAAkB,uBAA8C;AAC/E,QAAM;AAAA,IACL;AAAA,IACA;AAAA,IACA,SAAS;AAAA,IACT,gBAAgB;AAAA,IAChB,eAAe,CAAC;AAAA,IAChB,sBAAsB;AAAA,EACvB,IAAI;AAEJ,MAAI,eAAe;AAClB,0BAAsB,EAAE,SAAS,WAAW,CAAC;AAAA,EAC9C;AAEA,SAAO,wBAAwB,MAAM;AACpC,UAAM,aAAS,qCAAU,SAAS,YAAY;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,MAO7C,OAAO,SAAS,YAAY;AAAA,MAC5B,OAAO;AAAA,MACP,GAAG;AAAA,IACJ,CAAC;AAGD,QAAI,CAAC,QAAQ;AACZ,aAAO;AAAA,IACR;AAEA,QAAI,OAAO,OAAO;AACjB,YAAM,OAAO;AAAA,IACd;AAEA,WAAO;AAAA,EACR,CAAC;AACF;","names":["consola"]}
|
|
1
|
+
{"version":3,"sources":["../../src/node-cjs/index.ts","../../src/node-cjs/tools.ts","../../src/simple-promise-tools.ts","../../src/conditions.ts","../../src/print.ts","../../src/define-promise-tasks.ts"],"sourcesContent":["export * from \"./tools\";\n\n// 将绝大多数的工具函数 以cjs的方式导出\nexport * from \"../index\";\n","/**\n * 一些node环境下的工具函数\n */\n\nimport { normalize } from \"node:path\";\nimport { spawnSync, type SpawnOptions } from \"node:child_process\";\n\nimport { generateSimpleAsyncTask } from \"../simple-promise-tools\";\nimport consola from \"consola\";\n// import { normalizePath } from \"vite\";\n\n/**\n * 路径转换工具\n */\nexport function pathChange(path: string) {\n\treturn path.replace(/\\\\/g, \"/\");\n\t// FIXME: 无法有效地实现解析路径 测试用例不通过\n\t// return normalize(path);\n\t// FIXME: tsup打包时,无法处理好vite的依赖 会导致打包失败 不知道怎么单独使用并打包该函数\n\t// return normalizePath(path);\n}\n\nexport interface SpawnSyncSimpleParams {\n\tcommand: string;\n\tparameters: string[];\n\t/**\n\t * 是否流式输出内容\n\t * @description 默认输出的命令数据全部以流式的方式输出\n\t * @default true\n\t */\n\tisFlow?: boolean;\n\n\t/**\n\t * 是否显示命令?\n\t * @description\n\t * 是否打印目前正在执行的命令?\n\t * @default true\n\t */\n\tisShowCommand?: boolean;\n\tspawnOptions?: SpawnOptions;\n\n\t/** 打印当前运行的命令 */\n\tprintCurrentCommand?: (params: Pick<SpawnSyncSimpleParams, \"command\" | \"parameters\">) => void;\n}\n\n/**\n * 默认的打印当前运行命令 函数\n */\nexport const defPrintCurrentCommand: SpawnSyncSimpleParams[\"printCurrentCommand\"] = function (params) {\n\tconst { command, parameters } = params;\n\tconsola.info(` 当前运行的命令为: ${command} ${parameters.join(\" \")} \\n`);\n};\n\n/**\n * 生成简单的执行命令函数\n * @description\n * 对 spawnSync 做简单的包装\n *\n * 之前封装的是 execa 函数\n * @version 2\n */\nexport function generateSpawnSync(spawnSyncSimpleParams: SpawnSyncSimpleParams) {\n\tconst {\n\t\tcommand,\n\t\tparameters,\n\t\tisFlow = true,\n\t\tisShowCommand = true,\n\t\tspawnOptions = {},\n\t\tprintCurrentCommand = defPrintCurrentCommand,\n\t} = spawnSyncSimpleParams;\n\n\tif (isShowCommand) {\n\t\tprintCurrentCommand?.({ command, parameters });\n\t}\n\n\treturn generateSimpleAsyncTask(() => {\n\t\tconst result = spawnSync(command, parameters, {\n\t\t\t/**\n\t\t\t * 是否流式输出?\n\t\t\t * 是流式输出就是继承父进程的流式输出\n\t\t\t * 否则就使用默认值\n\t\t\t * @see https://nodejs.org/api/child_process.html#optionsstdio\n\t\t\t */\n\t\t\tstdio: isFlow ? \"inherit\" : \"pipe\",\n\t\t\tshell: true,\n\t\t\t...spawnOptions,\n\t\t});\n\n\t\t// 如果不是流式输出 就直接返回返回值即可\n\t\tif (!isFlow) {\n\t\t\treturn result;\n\t\t}\n\n\t\tif (result.error) {\n\t\t\tthrow result.error;\n\t\t}\n\n\t\treturn result;\n\t});\n}\n","// import { uniqueId } from \"lodash-es\";\n// const getCounter = () => uniqueId();\n\n/** 创建简单的异步任务 */\nexport function generateSimpleAsyncTask<T extends (...args: any) => any>(func: T) {\n\t// const taskId = getCounter();\n\n\treturn function (...args: any) {\n\t\t// consola.info(` 这是第 ${taskId} 个异步任务 `);\n\t\t// consola.start(\" 这里是新创建的异步函数 检查参数: \", ...args);\n\n\t\treturn new Promise<ReturnType<T>>((resolve, reject) => {\n\t\t\t// consola.start(\" 内部promise 检查参数: \", ...args);\n\t\t\tresolve(func(...args));\n\t\t});\n\t};\n}\n\nexport type SimpleAsyncTask = ReturnType<typeof generateSimpleAsyncTask>;\n\nexport const initFlag = <const>\"initFlag\";\n\n/**\n * 以队列串行的形式 串行运行异步函数\n * @see https://github.com/ascoders/weekly/blob/master/前沿技术/77.精读《用%20Reduce%20实现%20Promise%20串行执行》.md\n * @version 1\n */\nasync function runPromiseByQueueV1<T>(promises: ((...args: any) => Promise<T>)[]) {\n\tpromises.reduce(\n\t\tasync function (previousPromise, nextPromise, currentIndex) {\n\t\t\tconst response = await previousPromise;\n\t\t\t// consola.log(` reduce串行函数 currentIndex= ${currentIndex} res =`, response);\n\t\t\treturn await nextPromise(response);\n\t\t},\n\t\tPromise.resolve(initFlag) as Promise<any>,\n\t);\n}\n\n/**\n * 以队列串行的形式 串行运行异步函数\n * @version 2\n */\nexport async function runPromiseByQueue<T>(promises: ((...args: any) => Promise<T>)[]) {\n\tlet response: typeof initFlag | Awaited<T> = initFlag;\n\tfor await (const promise of promises) {\n\t\tresponse = await promise(response);\n\t}\n}\n\n/**\n * 以并行的形式 并发运行异步函数\n */\nexport async function runPromiseByConcurrency<T>(promises: ((...args: any) => Promise<T>)[]) {\n\tawait Promise.all(promises.map((promise) => promise()));\n}\n","export type Condition = (...args: unknown[]) => boolean;\n\n/** @deprecated 没必要 */\nexport type Conditions = Condition[];\n\n/**\n * 是否每一个条件函数都满足?\n * @description\n * ### 设计理由\n * 旨在于封装这样的代码段\n * ```js\n * const conditions = [\n * \t() => !isEqual(nAssetRecord, oAssetRecord),\n * \t() => !isEqual(nAssetRecord, defPropsAssets),\n * \t() => isEdit.value || isInfo.value,\n * ];\n * conditions.every((condition) => condition())\n * ```\n */\nexport function isConditionsEvery(conditions: Condition[]) {\n\treturn conditions.every((condition) => condition());\n}\n\nexport function isConditionsSome(conditions: Condition[]) {\n\treturn conditions.some((condition) => condition());\n}\n","import { isPlainObject, isArray } from \"lodash-es\";\nimport { isConditionsSome } from \"./conditions\";\n\n/**\n * 一个简单的格式化函数\n * @description\n * 用于将复杂对象变成纯文本 打印出来\n *\n * 主要用于测试框架打印完整的内容\n */\nexport function printFormat(params: any) {\n\tconst boolRes = isConditionsSome([\n\t\t// 是不是纯对象?\n\t\t() => isPlainObject(params),\n\t\t// 或者是不是单纯的数组?\n\t\t() => isArray(params),\n\t]);\n\n\t// 如果是纯对象或者数组,则使用 JSON.stringify 格式化输出 否则原样返回\n\tconst res = boolRes ? JSON.stringify(params, null, 2) : params;\n\n\treturn res;\n}\n","import { type SimpleAsyncTask } from \"./simple-promise-tools\";\n\nexport const taskTypes = <const>[\"single\", \"parallel\", \"queue\"];\n\nexport type TaskType = (typeof taskTypes)[number];\n\nexport interface BaseTask {\n\t/** 任务类型 */\n\ttype: TaskType;\n}\n\nexport type Task = SimpleAsyncTask | TasksConfig;\n\nexport interface SingleTasks extends BaseTask {\n\ttype: \"single\";\n\ttasks: Task;\n}\n\nexport interface ParallelTasks extends BaseTask {\n\ttype: \"parallel\";\n\ttasks: Task[];\n}\n\nexport interface QueueTasks extends BaseTask {\n\ttype: \"queue\";\n\ttasks: Task[];\n}\n\nexport type TasksConfig = SingleTasks | ParallelTasks | QueueTasks;\n\nexport type PromiseTasksConfig = TasksConfig;\n\nfunction isSingleTasks(config: TasksConfig): config is SingleTasks {\n\treturn config.type === \"single\";\n}\n\nfunction isParallelTasks(config: TasksConfig): config is ParallelTasks {\n\treturn config.type === \"parallel\";\n}\n\nfunction isQueueTasks(config: TasksConfig): config is QueueTasks {\n\treturn config.type === \"queue\";\n}\n\nfunction isSimpleAsyncTask(config: Task): config is SimpleAsyncTask {\n\treturn typeof config === \"function\";\n}\n\nfunction isTasksConfig(config: Task): config is TasksConfig {\n\treturn typeof config === \"object\";\n}\n\n/**\n * 定义异步任务对象\n * @description\n * 这个对象是一揽子异步任务的配置\n */\nexport function definePromiseTasks(config: TasksConfig) {\n\treturn config;\n}\n\n/**\n * @private 一个工具函数 用于生成异步函数数组\n * @deprecated 在处理串行任务时 疑似有故障\n */\nfunction getPromises(tasks: Task[]): ((...args: any) => Promise<any>)[] {\n\treturn tasks.map((task) => {\n\t\treturn async function (...args: any) {\n\t\t\tif (isSimpleAsyncTask(task)) {\n\t\t\t\treturn await task(...args);\n\t\t\t} else {\n\t\t\t\treturn await executePromiseTasks(task);\n\t\t\t}\n\t\t};\n\t});\n}\n\n/**\n * 执行异步函数对象\n */\nexport async function executePromiseTasks(\n\tconfig: TasksConfig,\n\t/**\n\t * 上一次递归执行时提供的参数\n\t * @description\n\t * 考虑到递归函数 这里提供了一个参数 用于传递上一次递归执行的结果\n\t */\n\tlastParams: any = null,\n): Promise<any> {\n\tif (isSingleTasks(config)) {\n\t\tif (isSimpleAsyncTask(config.tasks)) {\n\t\t\t// 实际执行的 tasks 往往是无参函数 这里为了保险,故主动传递参数\n\t\t\treturn await config.tasks(lastParams);\n\t\t}\n\n\t\treturn await executePromiseTasks(config.tasks, lastParams);\n\t}\n\n\tif (isParallelTasks(config)) {\n\t\treturn await Promise.all(\n\t\t\tconfig.tasks.map((task) => {\n\t\t\t\tif (isSimpleAsyncTask(task)) {\n\t\t\t\t\t// console.log(` 并行任务遇到单独的异步函数 `);\n\t\t\t\t\treturn task(lastParams);\n\t\t\t\t}\n\n\t\t\t\t// console.log(` 并行任务遇到嵌套结构 `);\n\t\t\t\treturn executePromiseTasks(task, lastParams);\n\t\t\t}),\n\t\t);\n\t}\n\n\tif (isQueueTasks(config)) {\n\t\tlet res: Awaited<any>;\n\t\tfor await (const task of config.tasks) {\n\t\t\tif (isSimpleAsyncTask(task)) {\n\t\t\t\t// console.log(` 串行任务遇到单独的异步函数 `);\n\n\t\t\t\tres = await task(lastParams);\n\t\t\t\tlastParams = res;\n\t\t\t\t// console.log(` 串行任务 单独 res `, res);\n\t\t\t} else {\n\t\t\t\tres = await executePromiseTasks(task, lastParams);\n\t\t\t\tlastParams = res;\n\t\t\t\t// console.log(` 串行任务 配置 res `, res);\n\t\t\t}\n\t\t}\n\n\t\treturn res;\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACKA,gCAA6C;;;ACDtC,SAAS,wBAAyD,MAAS;AAGjF,SAAO,YAAa,MAAW;AAI9B,WAAO,IAAI,QAAuB,CAAC,SAAS,WAAW;AAEtD,cAAQ,KAAK,GAAG,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;AACN,cAAM,MAAM,oBAAoB,MAAM,UAAU;AAChD,qBAAa;AAAA,MAEd;AAAA,IACD;AAEA,WAAO;AAAA,EACR;AACD;","names":["consola"]}
|
|
@@ -39,4 +39,122 @@ declare const defPrintCurrentCommand: SpawnSyncSimpleParams["printCurrentCommand
|
|
|
39
39
|
*/
|
|
40
40
|
declare function generateSpawnSync(spawnSyncSimpleParams: SpawnSyncSimpleParams): (...args: any) => Promise<child_process.SpawnSyncReturns<Buffer<ArrayBufferLike>>>;
|
|
41
41
|
|
|
42
|
-
|
|
42
|
+
type Condition = (...args: unknown[]) => boolean;
|
|
43
|
+
/** @deprecated 没必要 */
|
|
44
|
+
type Conditions = Condition[];
|
|
45
|
+
/**
|
|
46
|
+
* 是否每一个条件函数都满足?
|
|
47
|
+
* @description
|
|
48
|
+
* ### 设计理由
|
|
49
|
+
* 旨在于封装这样的代码段
|
|
50
|
+
* ```js
|
|
51
|
+
* const conditions = [
|
|
52
|
+
* () => !isEqual(nAssetRecord, oAssetRecord),
|
|
53
|
+
* () => !isEqual(nAssetRecord, defPropsAssets),
|
|
54
|
+
* () => isEdit.value || isInfo.value,
|
|
55
|
+
* ];
|
|
56
|
+
* conditions.every((condition) => condition())
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
declare function isConditionsEvery(conditions: Condition[]): boolean;
|
|
60
|
+
declare function isConditionsSome(conditions: Condition[]): boolean;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 一个简单的格式化函数
|
|
64
|
+
* @description
|
|
65
|
+
* 用于将复杂对象变成纯文本 打印出来
|
|
66
|
+
*
|
|
67
|
+
* 主要用于测试框架打印完整的内容
|
|
68
|
+
*/
|
|
69
|
+
declare function printFormat(params: any): any;
|
|
70
|
+
|
|
71
|
+
/** 创建简单的异步任务 */
|
|
72
|
+
declare function generateSimpleAsyncTask<T extends (...args: any) => any>(func: T): (...args: any) => Promise<ReturnType<T>>;
|
|
73
|
+
type SimpleAsyncTask = ReturnType<typeof generateSimpleAsyncTask>;
|
|
74
|
+
declare const initFlag: "initFlag";
|
|
75
|
+
/**
|
|
76
|
+
* 以队列串行的形式 串行运行异步函数
|
|
77
|
+
* @version 2
|
|
78
|
+
*/
|
|
79
|
+
declare function runPromiseByQueue<T>(promises: ((...args: any) => Promise<T>)[]): Promise<void>;
|
|
80
|
+
/**
|
|
81
|
+
* 以并行的形式 并发运行异步函数
|
|
82
|
+
*/
|
|
83
|
+
declare function runPromiseByConcurrency<T>(promises: ((...args: any) => Promise<T>)[]): Promise<void>;
|
|
84
|
+
|
|
85
|
+
declare const taskTypes: readonly ["single", "parallel", "queue"];
|
|
86
|
+
type TaskType = (typeof taskTypes)[number];
|
|
87
|
+
interface BaseTask {
|
|
88
|
+
/** 任务类型 */
|
|
89
|
+
type: TaskType;
|
|
90
|
+
}
|
|
91
|
+
type Task = SimpleAsyncTask | TasksConfig;
|
|
92
|
+
interface SingleTasks extends BaseTask {
|
|
93
|
+
type: "single";
|
|
94
|
+
tasks: Task;
|
|
95
|
+
}
|
|
96
|
+
interface ParallelTasks extends BaseTask {
|
|
97
|
+
type: "parallel";
|
|
98
|
+
tasks: Task[];
|
|
99
|
+
}
|
|
100
|
+
interface QueueTasks extends BaseTask {
|
|
101
|
+
type: "queue";
|
|
102
|
+
tasks: Task[];
|
|
103
|
+
}
|
|
104
|
+
type TasksConfig = SingleTasks | ParallelTasks | QueueTasks;
|
|
105
|
+
type PromiseTasksConfig = TasksConfig;
|
|
106
|
+
/**
|
|
107
|
+
* 定义异步任务对象
|
|
108
|
+
* @description
|
|
109
|
+
* 这个对象是一揽子异步任务的配置
|
|
110
|
+
*/
|
|
111
|
+
declare function definePromiseTasks(config: TasksConfig): TasksConfig;
|
|
112
|
+
/**
|
|
113
|
+
* 执行异步函数对象
|
|
114
|
+
*/
|
|
115
|
+
declare function executePromiseTasks(config: TasksConfig,
|
|
116
|
+
/**
|
|
117
|
+
* 上一次递归执行时提供的参数
|
|
118
|
+
* @description
|
|
119
|
+
* 考虑到递归函数 这里提供了一个参数 用于传递上一次递归执行的结果
|
|
120
|
+
*/
|
|
121
|
+
lastParams?: any): Promise<any>;
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* pnpm-workspace.yaml 文件的类型声明
|
|
125
|
+
* @description
|
|
126
|
+
* 设计理由
|
|
127
|
+
*
|
|
128
|
+
* 主要是为了让该文件被解析后,能够有一个基础的类型声明
|
|
129
|
+
*
|
|
130
|
+
* 按理说这个东西应该有别人封装好的类型的,肯定因为我没找到。
|
|
131
|
+
*
|
|
132
|
+
* 未来应该找到这样的类型声明库,直接复用别人的就好了,不要自己写了。
|
|
133
|
+
*/
|
|
134
|
+
interface PnpmWorkspace {
|
|
135
|
+
/**
|
|
136
|
+
* 包的匹配语法字符串
|
|
137
|
+
*
|
|
138
|
+
* @example
|
|
139
|
+
* ["packages/**", "demos/**", "utils"]
|
|
140
|
+
*/
|
|
141
|
+
packages?: string[];
|
|
142
|
+
catalog?: string[];
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* `Prettify` 帮助程序是一种实用程序类型,它采用对象类型并使悬停叠加更具可读性。
|
|
147
|
+
* @see https://www.totaltypescript.com/concepts/the-prettify-helper
|
|
148
|
+
*/
|
|
149
|
+
type Prettify<T> = {
|
|
150
|
+
[K in keyof T]: T[K];
|
|
151
|
+
} & {};
|
|
152
|
+
/**
|
|
153
|
+
* 转换成 NumberLike 类型
|
|
154
|
+
* @description
|
|
155
|
+
* ### *设计理由*
|
|
156
|
+
* 期望让一个数值类型的联合类型 变成`NumberLike`形式的类型
|
|
157
|
+
*/
|
|
158
|
+
type ToNumberLike<T extends number> = T | `${T}`;
|
|
159
|
+
|
|
160
|
+
export { type BaseTask, type Condition, type Conditions, type ParallelTasks, type PnpmWorkspace, type Prettify, type PromiseTasksConfig, type QueueTasks, type SimpleAsyncTask, type SingleTasks, type SpawnSyncSimpleParams, type Task, type TaskType, type TasksConfig, type ToNumberLike, defPrintCurrentCommand, definePromiseTasks, executePromiseTasks, generateSimpleAsyncTask, generateSpawnSync, initFlag, isConditionsEvery, isConditionsSome, pathChange, printFormat, runPromiseByConcurrency, runPromiseByQueue, taskTypes };
|
package/dist/node-esm/index.js
CHANGED