Skip to content

Commit f145f2c

Browse files
committed
Limit spawn concurrency
1 parent bcfea97 commit f145f2c

File tree

1 file changed

+41
-19
lines changed

1 file changed

+41
-19
lines changed

packages/cmake-rn/src/cli.ts

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import assert from "node:assert/strict";
22
import path from "node:path";
33
import fs from "node:fs";
44
import { EventEmitter } from "node:events";
5+
import os from "node:os";
56

67
import {
78
chalk,
@@ -11,6 +12,7 @@ import {
1112
oraPromise,
1213
assertFixable,
1314
wrapAction,
15+
pLimit,
1416
} from "@react-native-node-api/cli-utils";
1517

1618
import {
@@ -129,6 +131,16 @@ const ccachePathOption = new Option(
129131
"Specify the path to the ccache executable",
130132
).default(getCcachePath());
131133

134+
const concurrencyOption = new Option(
135+
"--concurrency <limit>",
136+
"Limit the number of concurrent tasks",
137+
)
138+
.argParser((value) => parseInt(value, 10))
139+
.default(
140+
os.availableParallelism(),
141+
`${os.availableParallelism()} or 1 when verbose is enabled`,
142+
);
143+
132144
let program = new Command("cmake-rn")
133145
.description("Build React Native Node API modules with CMake")
134146
.addOption(tripletOption)
@@ -144,7 +156,8 @@ let program = new Command("cmake-rn")
144156
.addOption(noAutoLinkOption)
145157
.addOption(noWeakNodeApiLinkageOption)
146158
.addOption(cmakeJsOption)
147-
.addOption(ccachePathOption);
159+
.addOption(ccachePathOption)
160+
.addOption(concurrencyOption);
148161

149162
for (const platform of platforms) {
150163
const allOption = new Option(
@@ -181,6 +194,7 @@ program = program.action(
181194
out,
182195
build: buildPath,
183196
ccachePath,
197+
concurrency,
184198
} = baseOptions;
185199

186200
assertFixable(
@@ -232,6 +246,8 @@ program = program.action(
232246
}
233247
}
234248

249+
const limit = pLimit(concurrency);
250+
235251
const tripletContexts = [...triplets].map((triplet) => {
236252
const platform = findPlatformForTriplet(triplet);
237253

@@ -244,17 +260,21 @@ program = program.action(
244260
triplet,
245261
platform,
246262
async spawn(command: string, args: string[], cwd?: string) {
247-
const outputPrefix = verbose ? chalk.dim(`[${triplet}] `) : undefined;
248-
if (verbose) {
249-
console.log(
250-
`${outputPrefix}» ${command} ${args.map((arg) => chalk.dim(`${arg}`)).join(" ")}`,
251-
cwd ? `(in ${chalk.dim(cwd)})` : "",
252-
);
253-
}
254-
await spawn(command, args, {
255-
outputMode: verbose ? "inherit" : "buffered",
256-
outputPrefix,
257-
cwd,
263+
await limit(async () => {
264+
const outputPrefix = verbose
265+
? chalk.dim(`[${triplet}] `)
266+
: undefined;
267+
if (verbose) {
268+
console.log(
269+
`${outputPrefix}» ${command} ${args.map((arg) => chalk.dim(`${arg}`)).join(" ")}`,
270+
cwd ? `(in ${chalk.dim(cwd)})` : "",
271+
);
272+
}
273+
await spawn(command, args, {
274+
outputMode: verbose ? "inherit" : "buffered",
275+
outputPrefix,
276+
cwd,
277+
});
258278
});
259279
},
260280
};
@@ -280,13 +300,15 @@ program = program.action(
280300
relevantTriplets,
281301
baseOptions,
282302
(command, args, cwd) =>
283-
spawn(command, args, {
284-
outputMode: verbose ? "inherit" : "buffered",
285-
outputPrefix: verbose
286-
? chalk.dim(`[${platform.name}] `)
287-
: undefined,
288-
cwd,
289-
}),
303+
limit(() =>
304+
spawn(command, args, {
305+
outputMode: verbose ? "inherit" : "buffered",
306+
outputPrefix: verbose
307+
? chalk.dim(`[${platform.name}] `)
308+
: undefined,
309+
cwd,
310+
}),
311+
),
290312
);
291313
}
292314
}),

0 commit comments

Comments
 (0)