@ruan-cat/utils 4.1.1 → 4.2.0

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/README.md CHANGED
@@ -1,13 +1 @@
1
- ---
2
- order: 10
3
- dir:
4
- order: 10
5
- ---
6
-
7
- # 工具包
8
-
9
- 这是阮喵喵开发的工具包,提供了一些工具函数。
10
-
11
- ## TODO: 尝试开发一个 cli
12
-
13
- - https://cloud.tencent.com/developer/article/2033881
1
+ 详情看[此文件](./src/index.md)。
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/conditions.ts","../src/define-promise-tasks.ts","../src/simple-promise-tools.ts"],"sourcesContent":["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 { type SimpleAsyncTask } from \"./simple-promise-tools.ts\";\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","// 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":";AAmBO,SAAS,kBAAkB,YAAyB;AAC1D,SAAO,WAAW,MAAM,CAAC,cAAc,UAAU,CAAC;AACnD;AAEO,SAAS,iBAAiB,YAAyB;AACzD,SAAO,WAAW,KAAK,CAAC,cAAc,UAAU,CAAC;AAClD;;;ACvBO,IAAM,YAAmB,CAAC,UAAU,YAAY,OAAO;AA8B9D,SAAS,cAAc,QAA4C;AAClE,SAAO,OAAO,SAAS;AACxB;AAEA,SAAS,gBAAgB,QAA8C;AACtE,SAAO,OAAO,SAAS;AACxB;AAEA,SAAS,aAAa,QAA2C;AAChE,SAAO,OAAO,SAAS;AACxB;AAEA,SAAS,kBAAkB,QAAyC;AACnE,SAAO,OAAO,WAAW;AAC1B;AAWO,SAAS,mBAAmB,QAAqB;AACvD,SAAO;AACR;AAqBA,eAAsB,oBACrB,QAMA,aAAkB,MACH;AACf,MAAI,cAAc,MAAM,GAAG;AAC1B,QAAI,kBAAkB,OAAO,KAAK,GAAG;AAEpC,aAAO,MAAM,OAAO,MAAM,UAAU;AAAA,IACrC;AAEA,WAAO,MAAM,oBAAoB,OAAO,OAAO,UAAU;AAAA,EAC1D;AAEA,MAAI,gBAAgB,MAAM,GAAG;AAC5B,WAAO,MAAM,QAAQ;AAAA,MACpB,OAAO,MAAM,IAAI,CAAC,SAAS;AAC1B,YAAI,kBAAkB,IAAI,GAAG;AAE5B,iBAAO,KAAK,UAAU;AAAA,QACvB;AAGA,eAAO,oBAAoB,MAAM,UAAU;AAAA,MAC5C,CAAC;AAAA,IACF;AAAA,EACD;AAEA,MAAI,aAAa,MAAM,GAAG;AACzB,QAAI;AACJ,qBAAiB,QAAQ,OAAO,OAAO;AACtC,UAAI,kBAAkB,IAAI,GAAG;AAG5B,cAAM,MAAM,KAAK,UAAU;AAC3B,qBAAa;AAAA,MAEd,OAAO;AACN,cAAM,MAAM,oBAAoB,MAAM,UAAU;AAChD,qBAAa;AAAA,MAEd;AAAA,IACD;AAEA,WAAO;AAAA,EACR;AACD;;;AC9HO,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;AAIO,IAAM,WAAkB;AAsB/B,eAAsB,kBAAqB,UAA4C;AACtF,MAAI,WAAyC;AAC7C,mBAAiB,WAAW,UAAU;AACrC,eAAW,MAAM,QAAQ,QAAQ;AAAA,EAClC;AACD;AAKA,eAAsB,wBAA2B,UAA4C;AAC5F,QAAM,QAAQ,IAAI,SAAS,IAAI,CAAC,YAAY,QAAQ,CAAC,CAAC;AACvD;","names":[]}
1
+ {"version":3,"sources":["../src/conditions.ts","../src/define-promise-tasks.ts","../src/simple-promise-tools.ts"],"sourcesContent":["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 { 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","// 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":";AAmBO,SAAS,kBAAkB,YAAyB;AAC1D,SAAO,WAAW,MAAM,CAAC,cAAc,UAAU,CAAC;AACnD;AAEO,SAAS,iBAAiB,YAAyB;AACzD,SAAO,WAAW,KAAK,CAAC,cAAc,UAAU,CAAC;AAClD;;;ACvBO,IAAM,YAAmB,CAAC,UAAU,YAAY,OAAO;AA8B9D,SAAS,cAAc,QAA4C;AAClE,SAAO,OAAO,SAAS;AACxB;AAEA,SAAS,gBAAgB,QAA8C;AACtE,SAAO,OAAO,SAAS;AACxB;AAEA,SAAS,aAAa,QAA2C;AAChE,SAAO,OAAO,SAAS;AACxB;AAEA,SAAS,kBAAkB,QAAyC;AACnE,SAAO,OAAO,WAAW;AAC1B;AAWO,SAAS,mBAAmB,QAAqB;AACvD,SAAO;AACR;AAqBA,eAAsB,oBACrB,QAMA,aAAkB,MACH;AACf,MAAI,cAAc,MAAM,GAAG;AAC1B,QAAI,kBAAkB,OAAO,KAAK,GAAG;AAEpC,aAAO,MAAM,OAAO,MAAM,UAAU;AAAA,IACrC;AAEA,WAAO,MAAM,oBAAoB,OAAO,OAAO,UAAU;AAAA,EAC1D;AAEA,MAAI,gBAAgB,MAAM,GAAG;AAC5B,WAAO,MAAM,QAAQ;AAAA,MACpB,OAAO,MAAM,IAAI,CAAC,SAAS;AAC1B,YAAI,kBAAkB,IAAI,GAAG;AAE5B,iBAAO,KAAK,UAAU;AAAA,QACvB;AAGA,eAAO,oBAAoB,MAAM,UAAU;AAAA,MAC5C,CAAC;AAAA,IACF;AAAA,EACD;AAEA,MAAI,aAAa,MAAM,GAAG;AACzB,QAAI;AACJ,qBAAiB,QAAQ,OAAO,OAAO;AACtC,UAAI,kBAAkB,IAAI,GAAG;AAG5B,cAAM,MAAM,KAAK,UAAU;AAC3B,qBAAa;AAAA,MAEd,OAAO;AACN,cAAM,MAAM,oBAAoB,MAAM,UAAU;AAChD,qBAAa;AAAA,MAEd;AAAA,IACD;AAEA,WAAO;AAAA,EACR;AACD;;;AC9HO,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;AAIO,IAAM,WAAkB;AAsB/B,eAAsB,kBAAqB,UAA4C;AACtF,MAAI,WAAyC;AAC7C,mBAAiB,WAAW,UAAU;AACrC,eAAW,MAAM,QAAQ,QAAQ;AAAA,EAClC;AACD;AAKA,eAAsB,wBAA2B,UAA4C;AAC5F,QAAM,QAAQ,IAAI,SAAS,IAAI,CAAC,YAAY,QAAQ,CAAC,CAAC;AACvD;","names":[]}
@@ -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.ts\";\n","/**\n * 一些node环境下的工具函数\n */\n\nimport { normalize } from \"node:path\";\nimport { spawnSync, type SpawnOptions } from \"node:child_process\";\n\nimport { generateSimpleAsyncTask } from \"@ruan-cat/utils/src/simple-promise-tools.ts\";\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"],"sourcesContent":["export * from \"./tools\";","/**\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"]}