@ruan-cat/vercel-deploy-tool 0.3.0 → 0.4.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/package.json +5 -14
- package/src/config.ts +7 -0
- package/src/index.ts +56 -43
- package/tsconfig.json +6 -1
- package/src/utils/define-promise-tasks.ts +0 -138
- package/src/utils/simple-promise-tools.ts +0 -94
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ruan-cat/vercel-deploy-tool",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "阮喵喵自用的vercel部署工具,用于实现复杂项目的部署。",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.ts",
|
|
7
7
|
"types": "./src/index.ts",
|
|
8
|
-
"homepage": "https://github.com/ruan-cat/vercel-monorepo-test/tree/main/
|
|
8
|
+
"homepage": "https://github.com/ruan-cat/vercel-monorepo-test/tree/main/packages/vercel-deploy-tool",
|
|
9
9
|
"bugs": {
|
|
10
10
|
"url": "https://github.com/ruan-cat/vercel-monorepo-test/issues"
|
|
11
11
|
},
|
|
@@ -44,32 +44,23 @@
|
|
|
44
44
|
"c12": "^1.11.2",
|
|
45
45
|
"commander": "^12.0.0",
|
|
46
46
|
"consola": "^3.2.3",
|
|
47
|
-
"cpx": "^1.5.0",
|
|
48
|
-
"cpy": "^11.1.0",
|
|
49
|
-
"del": "^8.0.0",
|
|
50
|
-
"execa": "^9.3.1",
|
|
51
47
|
"lodash-es": "4.17.21",
|
|
52
|
-
"mkdirp": "^3.0.1",
|
|
53
48
|
"pathe": "^1.1.2",
|
|
54
49
|
"rimraf": "^6.0.1",
|
|
55
50
|
"shx": "^0.3.4",
|
|
56
|
-
"vercel": "^
|
|
57
|
-
"@ruan-cat/utils": "^1.
|
|
51
|
+
"vercel": "^39.1.2",
|
|
52
|
+
"@ruan-cat/utils": "^1.3.0"
|
|
58
53
|
},
|
|
59
54
|
"devDependencies": {
|
|
60
|
-
"@types/cpx": "^1.5.5",
|
|
61
55
|
"@types/gulp": "^4.0.17",
|
|
62
56
|
"@types/lodash-es": "^4.17.12",
|
|
63
|
-
"@types/node": "^22.5.1"
|
|
64
|
-
"@vitest/ui": "^2.0.5",
|
|
65
|
-
"vitest": "^2.0.5"
|
|
57
|
+
"@types/node": "^22.5.1"
|
|
66
58
|
},
|
|
67
59
|
"scripts": {
|
|
68
60
|
"—build-not-use-for-now": "tsc",
|
|
69
61
|
"start": "node ./dist/index.js",
|
|
70
62
|
"run": "node --import=tsx ./src/index.ts --env-path=.env.test",
|
|
71
63
|
"test:config": "node --import=tsx ./tests/config.test.ts --env-path=.env.test",
|
|
72
|
-
"test:vitest": "vitest --ui --watch",
|
|
73
64
|
"rm:node_modules": "rimraf node_modules"
|
|
74
65
|
}
|
|
75
66
|
}
|
package/src/config.ts
CHANGED
|
@@ -57,10 +57,17 @@ export interface WithUserCommands extends Base {
|
|
|
57
57
|
|
|
58
58
|
/**
|
|
59
59
|
* 部署输出路径
|
|
60
|
+
*
|
|
61
|
+
* @version 1
|
|
60
62
|
* @description
|
|
61
63
|
* 这里要填写满足 cpx 库能够识别glob语法的路径
|
|
62
64
|
* @example docs/.vitepress/dist/**\/*
|
|
63
65
|
* @example src/.vuepress/dist/**\/*
|
|
66
|
+
*
|
|
67
|
+
* @version 2
|
|
68
|
+
* @description
|
|
69
|
+
* 填写打包目录的路径即可。不包含glob语法。
|
|
70
|
+
* @example docs/.vitepress/dist
|
|
64
71
|
*/
|
|
65
72
|
outputDirectory: string;
|
|
66
73
|
|
package/src/index.ts
CHANGED
|
@@ -1,35 +1,33 @@
|
|
|
1
1
|
// 学习一下如何使用 https://github.com/sindresorhus/execa/blob/main/readme.md
|
|
2
|
-
import fs from "node:fs";
|
|
3
2
|
import { dirname, resolve } from "node:path";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
3
|
+
import { spawnSync } from "node:child_process";
|
|
4
|
+
import fs, {
|
|
5
|
+
// 文件是否存在
|
|
6
|
+
existsSync,
|
|
7
|
+
// 复制文件
|
|
8
|
+
copyFileSync,
|
|
9
|
+
// 复制目录
|
|
10
|
+
cpSync,
|
|
11
|
+
// 删除目录
|
|
12
|
+
rmSync,
|
|
13
|
+
// 新建文件夹
|
|
14
|
+
mkdir,
|
|
15
|
+
} from "node:fs";
|
|
16
|
+
|
|
6
17
|
import { concat, isEmpty, isUndefined } from "lodash-es";
|
|
7
18
|
import { consola } from "consola";
|
|
8
|
-
import { isConditionsEvery, isConditionsSome } from "@ruan-cat/utils";
|
|
9
|
-
import { deleteAsync } from "del";
|
|
10
|
-
import { mkdirpSync } from "mkdirp";
|
|
11
|
-
import cpy from "cpy";
|
|
12
|
-
import cpx from "cpx";
|
|
13
|
-
|
|
14
|
-
import {
|
|
15
|
-
initVercelConfig,
|
|
16
|
-
config,
|
|
17
|
-
getConfig,
|
|
18
|
-
type Config,
|
|
19
|
-
type Base,
|
|
20
|
-
type DeployTarget,
|
|
21
|
-
type WithUserCommands,
|
|
22
|
-
} from "./config";
|
|
23
|
-
import { generateSimpleAsyncTask } from "./utils/simple-promise-tools";
|
|
24
19
|
|
|
25
20
|
import {
|
|
21
|
+
isConditionsEvery,
|
|
22
|
+
isConditionsSome,
|
|
23
|
+
generateSimpleAsyncTask,
|
|
26
24
|
definePromiseTasks,
|
|
27
25
|
executePromiseTasks,
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
} from "./
|
|
26
|
+
} from "@ruan-cat/utils";
|
|
27
|
+
import type { Task } from "@ruan-cat/utils";
|
|
28
|
+
|
|
29
|
+
import { config, getConfig } from "./config";
|
|
30
|
+
import type { Config, Base, DeployTarget, WithUserCommands } from "./config";
|
|
33
31
|
|
|
34
32
|
/**
|
|
35
33
|
* vercel 的空配置
|
|
@@ -49,6 +47,18 @@ export const vercelNullConfig = <const>{
|
|
|
49
47
|
outputDirectory: null,
|
|
50
48
|
devCommand: null,
|
|
51
49
|
public: false,
|
|
50
|
+
/**
|
|
51
|
+
* 部署后提供干净的链接
|
|
52
|
+
* @see https://vercel.com/docs/projects/project-configuration#cleanurls
|
|
53
|
+
*
|
|
54
|
+
* @description
|
|
55
|
+
* 暂无效果
|
|
56
|
+
*
|
|
57
|
+
* 目前在 build-output-api 中,实现cleanUrls需要手动地写入配置文件
|
|
58
|
+
*
|
|
59
|
+
* 成本较大,目前不做投入。
|
|
60
|
+
*/
|
|
61
|
+
cleanUrls: true,
|
|
52
62
|
git: {
|
|
53
63
|
deploymentEnabled: {
|
|
54
64
|
main: false,
|
|
@@ -150,13 +160,16 @@ function getTargetCWDCommandArgument(deployTarget: DeployTarget) {
|
|
|
150
160
|
}
|
|
151
161
|
|
|
152
162
|
/**
|
|
153
|
-
*
|
|
163
|
+
* 生成简单的执行命令函数
|
|
154
164
|
* @description
|
|
155
165
|
* 对 execa 做简单的包装
|
|
166
|
+
*
|
|
167
|
+
* 封装 spawnSync 函数
|
|
168
|
+
* @version 2
|
|
156
169
|
*/
|
|
157
170
|
function generateExeca(execaSimpleParams: { command: string; parameters: string[] }) {
|
|
158
171
|
const { command, parameters } = execaSimpleParams;
|
|
159
|
-
return generateSimpleAsyncTask(() =>
|
|
172
|
+
return generateSimpleAsyncTask(() => spawnSync(command, parameters, { shell: true }));
|
|
160
173
|
}
|
|
161
174
|
|
|
162
175
|
/**
|
|
@@ -261,7 +274,7 @@ function generateCopyDistTasks_v1(deployTarget: WithUserCommands) {
|
|
|
261
274
|
command,
|
|
262
275
|
parameters: [],
|
|
263
276
|
});
|
|
264
|
-
const {
|
|
277
|
+
const { stdout } = await commandFunction();
|
|
265
278
|
consola.info(` 执行了命令 🐓: `, command);
|
|
266
279
|
// consola.box(stdout);
|
|
267
280
|
});
|
|
@@ -291,7 +304,7 @@ function generateCopyDistTasks(deployTarget: WithUserCommands) {
|
|
|
291
304
|
* 本函数仅仅拼接部分路径
|
|
292
305
|
*/
|
|
293
306
|
function joinPath<T extends string>(dir: T) {
|
|
294
|
-
const resPath = resolve(targetCWD, dir);
|
|
307
|
+
const resPath = resolve(process.cwd(), targetCWD, dir);
|
|
295
308
|
// console.log(" in joinPath => ", resPath);
|
|
296
309
|
return <`${string}${typeof targetCWD}/${T}`>resPath;
|
|
297
310
|
}
|
|
@@ -301,13 +314,13 @@ function generateCopyDistTasks(deployTarget: WithUserCommands) {
|
|
|
301
314
|
|
|
302
315
|
async function delVercelOutputStatic() {
|
|
303
316
|
consola.start(` 开始删除文件任务 `);
|
|
304
|
-
|
|
317
|
+
rmSync(pathVercelOutputStatic, { recursive: true });
|
|
305
318
|
consola.success(` 删除该路径的文件: ${pathVercelOutputStatic} `);
|
|
306
319
|
}
|
|
307
320
|
|
|
308
321
|
async function createVercelOutputStatic() {
|
|
309
322
|
consola.start(` 开始创建文件夹任务 `);
|
|
310
|
-
|
|
323
|
+
mkdir(pathVercelOutputStatic, () => {});
|
|
311
324
|
consola.success(` 创建的新目录为: ${pathVercelOutputStatic} `);
|
|
312
325
|
}
|
|
313
326
|
|
|
@@ -315,10 +328,7 @@ function generateCopyDistTasks(deployTarget: WithUserCommands) {
|
|
|
315
328
|
consola.start(` 开始文件复制任务 `);
|
|
316
329
|
consola.info(` 从 ${pathOutputDirectory} 开始 `);
|
|
317
330
|
consola.info(` 复制到 ${pathVercelOutputStatic} 内`);
|
|
318
|
-
|
|
319
|
-
await cpy(pathOutputDirectory, pathVercelOutputStatic);
|
|
320
|
-
// await cp(pathOutputDirectory, pathVercelOutputStatic, { recursive: true });
|
|
321
|
-
// await cpx.copy(pathOutputDirectory, pathVercelOutputStatic);
|
|
331
|
+
cpSync(pathOutputDirectory, pathVercelOutputStatic, { recursive: true });
|
|
322
332
|
consola.success(` 完成文件复制任务 `);
|
|
323
333
|
}
|
|
324
334
|
|
|
@@ -386,8 +396,9 @@ function generateAfterBuildTasksConfig(config: Config): Task {
|
|
|
386
396
|
parameters: [],
|
|
387
397
|
});
|
|
388
398
|
consola.start(` 开始用户 afterBuildTasks 命令任务 `);
|
|
389
|
-
const {
|
|
390
|
-
consola.success(` 完成用户 afterBuildTasks 命令任务 ${code} `);
|
|
399
|
+
const { stdout } = await userCommand();
|
|
400
|
+
// consola.success(` 完成用户 afterBuildTasks 命令任务 ${code} `);
|
|
401
|
+
consola.success(` 完成用户 afterBuildTasks 命令任务 `);
|
|
391
402
|
// consola.box(stdout);
|
|
392
403
|
});
|
|
393
404
|
}),
|
|
@@ -429,9 +440,9 @@ async function main() {
|
|
|
429
440
|
return generateSimpleAsyncTask(async () => {
|
|
430
441
|
const build = generateBuildTask(deployTarget);
|
|
431
442
|
consola.start(` 开始build任务 `);
|
|
432
|
-
const {
|
|
443
|
+
const { stdout } = await build();
|
|
433
444
|
consola.success(` 完成build任务 `);
|
|
434
|
-
consola.info(` 完成命令 ${code} `);
|
|
445
|
+
// consola.info(` 完成命令 ${code} `);
|
|
435
446
|
// consola.box(stdout);
|
|
436
447
|
});
|
|
437
448
|
}),
|
|
@@ -463,8 +474,9 @@ async function main() {
|
|
|
463
474
|
parameters: [],
|
|
464
475
|
});
|
|
465
476
|
consola.start(` 开始用户命令任务 `);
|
|
466
|
-
const {
|
|
467
|
-
consola.success(` 完成用户命令任务 ${code} `);
|
|
477
|
+
const { stdout } = await userCommand();
|
|
478
|
+
// consola.success(` 完成用户命令任务 ${code} `);
|
|
479
|
+
consola.success(` 完成用户命令任务 `);
|
|
468
480
|
// consola.box(stdout);
|
|
469
481
|
});
|
|
470
482
|
}),
|
|
@@ -499,7 +511,8 @@ async function main() {
|
|
|
499
511
|
generateSimpleAsyncTask(async () => {
|
|
500
512
|
const deploy = generateDeployTask(deployTarget);
|
|
501
513
|
consola.start(` 开始部署任务 `);
|
|
502
|
-
const { stdout
|
|
514
|
+
const { stdout } = await deploy();
|
|
515
|
+
const vercelUrl = stdout.toString();
|
|
503
516
|
consola.success(` 完成部署任务 检查生成的url为 \n `);
|
|
504
517
|
consola.box(vercelUrl);
|
|
505
518
|
return vercelUrl;
|
|
@@ -512,8 +525,8 @@ async function main() {
|
|
|
512
525
|
return generateSimpleAsyncTask(async (vercelUrl: string) => {
|
|
513
526
|
const alias = generateAliasTask(vercelUrl, userUrl);
|
|
514
527
|
consola.start(` 开始别名任务 `);
|
|
515
|
-
const { stdout
|
|
516
|
-
consola.success(` 执行了: ${command} `);
|
|
528
|
+
const { stdout } = await alias();
|
|
529
|
+
// consola.success(` 执行了: ${command} `);
|
|
517
530
|
consola.success(` 完成别名任务 可用的别名地址为 \n`);
|
|
518
531
|
consola.box(`https://${userUrl}`);
|
|
519
532
|
});
|
package/tsconfig.json
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"extends": "../../tsconfig.base.json",
|
|
3
|
+
"references": [
|
|
4
|
+
{
|
|
5
|
+
"path": "../../tsconfig.md.json"
|
|
6
|
+
}
|
|
7
|
+
],
|
|
3
8
|
"compilerOptions": {
|
|
4
9
|
"target": "ESNext",
|
|
5
10
|
"module": "ESNext",
|
|
@@ -31,7 +36,7 @@
|
|
|
31
36
|
"include": [
|
|
32
37
|
"./src/**/*.ts",
|
|
33
38
|
"./tests/**/*.test.ts",
|
|
34
|
-
"./tests/**/*.ts"
|
|
39
|
+
"./tests/**/*.ts",
|
|
35
40
|
// "src",
|
|
36
41
|
// "tests"
|
|
37
42
|
]
|
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type SimpleAsyncTask,
|
|
3
|
-
type SimpleAsyncTaskWithType,
|
|
4
|
-
generateSimpleAsyncTask,
|
|
5
|
-
runPromiseByConcurrency,
|
|
6
|
-
runPromiseByQueue,
|
|
7
|
-
} from "./simple-promise-tools";
|
|
8
|
-
|
|
9
|
-
export const taskTypes = <const>["single", "parallel", "queue"];
|
|
10
|
-
|
|
11
|
-
export type TaskType = (typeof taskTypes)[number];
|
|
12
|
-
|
|
13
|
-
export interface BaseTask {
|
|
14
|
-
/** 任务类型 */
|
|
15
|
-
type: TaskType;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
// type Task = SimpleAsyncTaskWithType | TasksConfig;
|
|
19
|
-
export type Task = SimpleAsyncTask | TasksConfig;
|
|
20
|
-
|
|
21
|
-
export interface SingleTasks extends BaseTask {
|
|
22
|
-
type: "single";
|
|
23
|
-
tasks: Task;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export interface ParallelTasks extends BaseTask {
|
|
27
|
-
type: "parallel";
|
|
28
|
-
tasks: Task[];
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export interface QueueTasks extends BaseTask {
|
|
32
|
-
type: "queue";
|
|
33
|
-
tasks: Task[];
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export type TasksConfig = SingleTasks | ParallelTasks | QueueTasks;
|
|
37
|
-
|
|
38
|
-
export type PromiseTasksConfig = TasksConfig;
|
|
39
|
-
|
|
40
|
-
function isSingleTasks(config: TasksConfig): config is SingleTasks {
|
|
41
|
-
return config.type === "single";
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function isParallelTasks(config: TasksConfig): config is ParallelTasks {
|
|
45
|
-
return config.type === "parallel";
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
function isQueueTasks(config: TasksConfig): config is QueueTasks {
|
|
49
|
-
return config.type === "queue";
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function isSimpleAsyncTask(config: Task): config is SimpleAsyncTask {
|
|
53
|
-
return typeof config === "function";
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
function isTasksConfig(config: Task): config is TasksConfig {
|
|
57
|
-
return typeof config === "object";
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* 定义异步任务对象
|
|
62
|
-
* @description
|
|
63
|
-
* 这个对象是一揽子异步任务的配置
|
|
64
|
-
*/
|
|
65
|
-
export function definePromiseTasks(config: TasksConfig) {
|
|
66
|
-
return config;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* @private 一个工具函数 用于生成异步函数数组
|
|
71
|
-
* @deprecated 在处理串行任务时 疑似有故障
|
|
72
|
-
*/
|
|
73
|
-
function getPromises(tasks: Task[]): ((...args: any) => Promise<any>)[] {
|
|
74
|
-
return tasks.map((task) => {
|
|
75
|
-
return async function (...args: any) {
|
|
76
|
-
if (isSimpleAsyncTask(task)) {
|
|
77
|
-
return await task(...args);
|
|
78
|
-
} else {
|
|
79
|
-
return await executePromiseTasks(task);
|
|
80
|
-
}
|
|
81
|
-
};
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* 执行异步函数对象
|
|
87
|
-
*/
|
|
88
|
-
export async function executePromiseTasks(
|
|
89
|
-
config: TasksConfig,
|
|
90
|
-
/**
|
|
91
|
-
* 上一次递归执行时提供的参数
|
|
92
|
-
* @description
|
|
93
|
-
* 考虑到递归函数 这里提供了一个参数 用于传递上一次递归执行的结果
|
|
94
|
-
*/
|
|
95
|
-
lastParams: any = null,
|
|
96
|
-
): Promise<any> {
|
|
97
|
-
if (isSingleTasks(config)) {
|
|
98
|
-
if (isSimpleAsyncTask(config.tasks)) {
|
|
99
|
-
// 实际执行的 tasks 往往是无参函数 这里为了保险,故主动传递参数
|
|
100
|
-
return await config.tasks(lastParams);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
return await executePromiseTasks(config.tasks, lastParams);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
if (isParallelTasks(config)) {
|
|
107
|
-
return await Promise.all(
|
|
108
|
-
config.tasks.map((task) => {
|
|
109
|
-
if (isSimpleAsyncTask(task)) {
|
|
110
|
-
// console.log(` 并行任务遇到单独的异步函数 `);
|
|
111
|
-
return task(lastParams);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// console.log(` 并行任务遇到嵌套结构 `);
|
|
115
|
-
return executePromiseTasks(task, lastParams);
|
|
116
|
-
}),
|
|
117
|
-
);
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
if (isQueueTasks(config)) {
|
|
121
|
-
let res: Awaited<any>;
|
|
122
|
-
for await (const task of config.tasks) {
|
|
123
|
-
if (isSimpleAsyncTask(task)) {
|
|
124
|
-
// console.log(` 串行任务遇到单独的异步函数 `);
|
|
125
|
-
|
|
126
|
-
res = await task(lastParams);
|
|
127
|
-
lastParams = res;
|
|
128
|
-
// console.log(` 串行任务 单独 res `, res);
|
|
129
|
-
} else {
|
|
130
|
-
res = await executePromiseTasks(task, lastParams);
|
|
131
|
-
lastParams = res;
|
|
132
|
-
// console.log(` 串行任务 配置 res `, res);
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
return res;
|
|
137
|
-
}
|
|
138
|
-
}
|
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
import { consola } from "consola";
|
|
2
|
-
import { uniqueId } from "lodash-es";
|
|
3
|
-
|
|
4
|
-
export function wait(time: number) {
|
|
5
|
-
return new Promise<void>((resolve) => {
|
|
6
|
-
setTimeout(() => {
|
|
7
|
-
resolve();
|
|
8
|
-
}, time);
|
|
9
|
-
});
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
const getCounter = () => uniqueId();
|
|
13
|
-
|
|
14
|
-
/** 创建简单的异步任务 */
|
|
15
|
-
export function generateSimpleAsyncTask<T extends (...args: any) => any>(func: T) {
|
|
16
|
-
const taskId = getCounter();
|
|
17
|
-
|
|
18
|
-
return function (...args: any) {
|
|
19
|
-
// consola.info(` 这是第 ${taskId} 个异步任务 `);
|
|
20
|
-
// consola.start(" 这里是新创建的异步函数 检查参数: ", ...args);
|
|
21
|
-
|
|
22
|
-
return new Promise<ReturnType<T>>((resolve, reject) => {
|
|
23
|
-
// consola.start(" 内部promise 检查参数: ", ...args);
|
|
24
|
-
resolve(func(...args));
|
|
25
|
-
});
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export type SimpleAsyncTask = ReturnType<typeof generateSimpleAsyncTask>;
|
|
30
|
-
|
|
31
|
-
/** @deprecated */
|
|
32
|
-
export type SimpleAsyncTaskWithType = <T = any>(...args: any) => Promise<T>;
|
|
33
|
-
|
|
34
|
-
export const initFlag = <const>"initFlag";
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* 以队列串行的形式 串行运行异步函数
|
|
38
|
-
* @see https://github.com/ascoders/weekly/blob/master/前沿技术/77.精读《用%20Reduce%20实现%20Promise%20串行执行》.md
|
|
39
|
-
* @version 1
|
|
40
|
-
*/
|
|
41
|
-
async function runPromiseByQueueV1<T>(promises: ((...args: any) => Promise<T>)[]) {
|
|
42
|
-
promises.reduce(
|
|
43
|
-
async function (previousPromise, nextPromise, currentIndex) {
|
|
44
|
-
const response = await previousPromise;
|
|
45
|
-
// consola.log(` reduce串行函数 currentIndex= ${currentIndex} res =`, response);
|
|
46
|
-
return await nextPromise(response);
|
|
47
|
-
},
|
|
48
|
-
Promise.resolve(initFlag) as Promise<any>,
|
|
49
|
-
);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* 以队列串行的形式 串行运行异步函数
|
|
54
|
-
* @version 2
|
|
55
|
-
*/
|
|
56
|
-
export async function runPromiseByQueue<T>(promises: ((...args: any) => Promise<T>)[]) {
|
|
57
|
-
let response: typeof initFlag | Awaited<T> = initFlag;
|
|
58
|
-
for await (const promise of promises) {
|
|
59
|
-
response = await promise(response);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* 以并行的形式 并发运行异步函数
|
|
65
|
-
*/
|
|
66
|
-
export async function runPromiseByConcurrency<T>(promises: ((...args: any) => Promise<T>)[]) {
|
|
67
|
-
await Promise.all(promises.map((promise) => promise()));
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export const testPromises = [
|
|
71
|
-
generateSimpleAsyncTask(async function (params) {
|
|
72
|
-
await wait(400);
|
|
73
|
-
consola.log(" 这里是 1 号函数 ");
|
|
74
|
-
consola.log(" 查看上一个函数返回过来的参数: ", params);
|
|
75
|
-
return 1;
|
|
76
|
-
}),
|
|
77
|
-
|
|
78
|
-
generateSimpleAsyncTask(async function (params) {
|
|
79
|
-
await wait(500);
|
|
80
|
-
consola.log(" 这里是 2 号函数 ");
|
|
81
|
-
consola.log(" 查看上一个函数返回过来的参数: ", params);
|
|
82
|
-
return 2;
|
|
83
|
-
}),
|
|
84
|
-
|
|
85
|
-
generateSimpleAsyncTask(async function (params) {
|
|
86
|
-
await wait(500);
|
|
87
|
-
consola.log(" 这里是 3 号函数 ");
|
|
88
|
-
consola.log(" 查看上一个函数返回过来的参数: ", params);
|
|
89
|
-
return 3;
|
|
90
|
-
}),
|
|
91
|
-
];
|
|
92
|
-
|
|
93
|
-
// 测试队列函数的传参能力 发现这里是可以实现传参的
|
|
94
|
-
// runPromiseByQueue(testPromises);
|