Skip to content

Commit e8e8c5d

Browse files
committed
More async fs I/O in cmake CLI
1 parent 59934ae commit e8e8c5d

File tree

1 file changed

+59
-47
lines changed
  • packages/react-native-node-api-cmake/src

1 file changed

+59
-47
lines changed

packages/react-native-node-api-cmake/src/cli.ts

Lines changed: 59 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import assert from "node:assert/strict";
22
import path from "node:path";
3-
import fs from "node:fs/promises";
4-
import { existsSync, readdirSync, renameSync } from "node:fs";
3+
import fs from "node:fs";
54
import { EventEmitter } from "node:events";
65

76
import { Command, Option } from "@commander-js/extra-typings";
@@ -109,7 +108,7 @@ export const program = new Command("react-native-node-api-cmake")
109108
try {
110109
const buildPath = getBuildPath(globalContext);
111110
if (globalContext.clean) {
112-
await fs.rm(buildPath, { recursive: true, force: true });
111+
await fs.promises.rm(buildPath, { recursive: true, force: true });
113112
}
114113
const triplets = new Set<SupportedTriplet>(tripletValues);
115114
if (globalContext.apple) {
@@ -162,7 +161,7 @@ export const program = new Command("react-native-node-api-cmake")
162161
tripletContext.map(async (context) => {
163162
// Delete any stale build artifacts before building
164163
// This is important, since we might rename the output files
165-
await fs.rm(context.tripletOutputPath, {
164+
await fs.promises.rm(context.tripletOutputPath, {
166165
recursive: true,
167166
force: true,
168167
});
@@ -181,32 +180,37 @@ export const program = new Command("react-native-node-api-cmake")
181180
isAppleTriplet(triplet)
182181
);
183182
if (appleTriplets.length > 0) {
184-
const libraryPaths = appleTriplets.flatMap(({ tripletOutputPath }) => {
185-
const configSpecificPath = path.join(
186-
tripletOutputPath,
187-
globalContext.configuration
188-
);
189-
assert(
190-
existsSync(configSpecificPath),
191-
`Expected a directory at ${configSpecificPath}`
192-
);
193-
// Expect binary file(s), either .node or .dylib
194-
return readdirSync(configSpecificPath).map((file) => {
195-
const filePath = path.join(configSpecificPath, file);
196-
if (filePath.endsWith(".dylib")) {
197-
return filePath;
198-
} else if (file.endsWith(".node")) {
199-
// Rename the file to .dylib for xcodebuild to accept it
200-
const newFilePath = filePath.replace(/\.node$/, ".dylib");
201-
renameSync(filePath, newFilePath);
202-
return newFilePath;
203-
} else {
204-
throw new Error(
205-
`Expected a .node or .dylib file, but found ${file}`
206-
);
207-
}
208-
});
209-
});
183+
const libraryPaths = await Promise.all(
184+
appleTriplets.map(async ({ tripletOutputPath }) => {
185+
const configSpecificPath = path.join(
186+
tripletOutputPath,
187+
globalContext.configuration
188+
);
189+
assert(
190+
fs.existsSync(configSpecificPath),
191+
`Expected a directory at ${configSpecificPath}`
192+
);
193+
// Expect binary file(s), either .node or .dylib
194+
const files = await fs.promises.readdir(configSpecificPath);
195+
const result = files.map(async (file) => {
196+
const filePath = path.join(configSpecificPath, file);
197+
if (filePath.endsWith(".dylib")) {
198+
return filePath;
199+
} else if (file.endsWith(".node")) {
200+
// Rename the file to .dylib for xcodebuild to accept it
201+
const newFilePath = filePath.replace(/\.node$/, ".dylib");
202+
await fs.promises.rename(filePath, newFilePath);
203+
return newFilePath;
204+
} else {
205+
throw new Error(
206+
`Expected a .node or .dylib file, but found ${file}`
207+
);
208+
}
209+
});
210+
assert.equal(result.length, 1, "Expected exactly one library file");
211+
return await result[0];
212+
})
213+
);
210214
const frameworkPaths = libraryPaths.map(createAppleFramework);
211215
const xcframeworkFilename =
212216
determineXCFrameworkFilename(frameworkPaths);
@@ -239,24 +243,32 @@ export const program = new Command("react-native-node-api-cmake")
239243
);
240244
if (androidTriplets.length > 0) {
241245
const libraryPathByTriplet = Object.fromEntries(
242-
androidTriplets.map(({ tripletOutputPath, triplet }) => {
243-
assert(
244-
existsSync(tripletOutputPath),
245-
`Expected a directory at ${tripletOutputPath}`
246-
);
247-
// Expect binary file(s), either .node or .so
248-
const result = readdirSync(tripletOutputPath, {
249-
withFileTypes: true,
246+
await Promise.all(
247+
androidTriplets.map(async ({ tripletOutputPath, triplet }) => {
248+
assert(
249+
fs.existsSync(tripletOutputPath),
250+
`Expected a directory at ${tripletOutputPath}`
251+
);
252+
// Expect binary file(s), either .node or .so
253+
const dirents = await fs.promises.readdir(tripletOutputPath, {
254+
withFileTypes: true,
255+
});
256+
const result = dirents
257+
.filter(
258+
(dirent) =>
259+
dirent.isFile() &&
260+
(dirent.name.endsWith(".so") ||
261+
dirent.name.endsWith(".node"))
262+
)
263+
.map((dirent) => path.join(dirent.parentPath, dirent.name));
264+
assert.equal(
265+
result.length,
266+
1,
267+
"Expected exactly one library file"
268+
);
269+
return [triplet, result[0]] as const;
250270
})
251-
.filter(
252-
(dirent) =>
253-
dirent.isFile() &&
254-
(dirent.name.endsWith(".so") || dirent.name.endsWith(".node"))
255-
)
256-
.map((dirent) => path.join(dirent.parentPath, dirent.name));
257-
assert.equal(result.length, 1, "Expected exactly one library file");
258-
return [triplet, result[0]] as const;
259-
})
271+
)
260272
) as Record<AndroidTriplet, string>;
261273
const androidLibsFilename = determineAndroidLibsFilename(
262274
Object.values(libraryPathByTriplet)

0 commit comments

Comments
 (0)