diff --git a/packages/host/src/node/cli/apple.ts b/packages/host/src/node/cli/apple.ts index 5de453d6..47be74d1 100644 --- a/packages/host/src/node/cli/apple.ts +++ b/packages/host/src/node/cli/apple.ts @@ -64,73 +64,73 @@ export async function linkXcframework({ await fs.promises.rm(outputPath, { recursive: true, force: true }); await fs.promises.cp(modulePath, tempPath, { recursive: true }); - const frameworkPaths = await Promise.all( - fs - .readdirSync(tempPath, { - withFileTypes: true, - }) + // Following extracted function mimics `glob("*/*.framework/")` + function globFrameworkDirs( + startPath: string, + fn: (parentPath: string, name: string) => Promise + ) { + return fs + .readdirSync(startPath, { withFileTypes: true }) .filter((tripletEntry) => tripletEntry.isDirectory()) .flatMap((tripletEntry) => { - const tripletPath = path.join(tempPath, tripletEntry.name); + const tripletPath = path.join(startPath, tripletEntry.name); return fs - .readdirSync(tripletPath, { - withFileTypes: true, - }) + .readdirSync(tripletPath, { withFileTypes: true }) .filter( (frameworkEntry) => frameworkEntry.isDirectory() && path.extname(frameworkEntry.name) === ".framework" ) - .flatMap(async (frameworkEntry) => { - const frameworkPath = path.join(tripletPath, frameworkEntry.name); - const oldLibraryName = path.basename( - frameworkEntry.name, - ".framework" - ); - const oldLibraryPath = path.join(frameworkPath, oldLibraryName); - const newFrameworkPath = path.join( - tripletPath, - `${newLibraryName}.framework` - ); - const newLibraryPath = path.join( - newFrameworkPath, - newLibraryName - ); - assert( - fs.existsSync(oldLibraryPath), - `Expected a library at '${oldLibraryPath}'` - ); - // Rename the library - await fs.promises.rename( - oldLibraryPath, - // Cannot use newLibraryPath here, because the framework isn't renamed yet - path.join(frameworkPath, newLibraryName) - ); - // Rename the framework - await fs.promises.rename(frameworkPath, newFrameworkPath); - // Expect the library in the new location - assert(fs.existsSync(newLibraryPath)); - // Update the binary - await spawn( - "install_name_tool", - [ - "-id", - `@rpath/${newLibraryName}.framework/${newLibraryName}`, - newLibraryPath, - ], - { - outputMode: "buffered", - } - ); - // Update the Info.plist file for the framework - await updateInfoPlist({ - filePath: path.join(newFrameworkPath, "Info.plist"), - oldLibraryName, - newLibraryName, - }); - return newFrameworkPath; - }); - }) + .flatMap(async (frameworkEntry) => + await fn(tripletPath, frameworkEntry.name) + ); + }); + } + + const frameworkPaths = await Promise.all( + globFrameworkDirs(tempPath, async (tripletPath, frameworkEntryName) => { + const frameworkPath = path.join(tripletPath, frameworkEntryName); + const oldLibraryName = path.basename(frameworkEntryName, ".framework"); + const oldLibraryPath = path.join(frameworkPath, oldLibraryName); + const newFrameworkPath = path.join( + tripletPath, + `${newLibraryName}.framework` + ); + const newLibraryPath = path.join(newFrameworkPath, newLibraryName); + assert( + fs.existsSync(oldLibraryPath), + `Expected a library at '${oldLibraryPath}'` + ); + // Rename the library + await fs.promises.rename( + oldLibraryPath, + // Cannot use newLibraryPath here, because the framework isn't renamed yet + path.join(frameworkPath, newLibraryName) + ); + // Rename the framework + await fs.promises.rename(frameworkPath, newFrameworkPath); + // Expect the library in the new location + assert(fs.existsSync(newLibraryPath)); + // Update the binary + await spawn( + "install_name_tool", + [ + "-id", + `@rpath/${newLibraryName}.framework/${newLibraryName}`, + newLibraryPath, + ], + { + outputMode: "buffered", + } + ); + // Update the Info.plist file for the framework + await updateInfoPlist({ + filePath: path.join(newFrameworkPath, "Info.plist"), + oldLibraryName, + newLibraryName, + }); + return newFrameworkPath; + }) ); // Create a new xcframework from the renamed frameworks diff --git a/packages/host/src/node/cli/link-modules.ts b/packages/host/src/node/cli/link-modules.ts index b483b6cd..2c21c187 100644 --- a/packages/host/src/node/cli/link-modules.ts +++ b/packages/host/src/node/cli/link-modules.ts @@ -68,12 +68,10 @@ export async function linkModules({ }); // Find absolute paths to xcframeworks - const absoluteModulePaths = Object.entries(dependenciesByName).flatMap( - ([, dependency]) => { - return dependency.modulePaths.map((modulePath) => - path.join(dependency.path, modulePath) - ); - } + const absoluteModulePaths = Object.values(dependenciesByName).flatMap( + (dependency) => dependency.modulePaths.map( + (modulePath) => path.join(dependency.path, modulePath) + ) ); if (hasDuplicateLibraryNames(absoluteModulePaths, naming)) { @@ -82,28 +80,25 @@ export async function linkModules({ } return Promise.all( - Object.entries(dependenciesByName).flatMap(([, dependency]) => { - return dependency.modulePaths.map(async (modulePath) => { - const originalPath = path.join(dependency.path, modulePath); - try { - return await linker({ - modulePath: originalPath, - incremental, - naming, - platform, - }); - } catch (error) { - if (error instanceof SpawnFailure) { - return { - originalPath, - skipped: false, - failure: error, - }; - } else { - throw error; - } + absoluteModulePaths.map(async (originalPath) => { + try { + return await linker({ + modulePath: originalPath, + incremental, + naming, + platform, + }); + } catch (error) { + if (error instanceof SpawnFailure) { + return { + originalPath, + skipped: false, + failure: error, + }; + } else { + throw error; } - }); + } }) ); }