react-native-node-api
Version:
Node-API for React Native
79 lines (78 loc) • 3.5 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.linkModules = linkModules;
exports.pruneLinkedModules = pruneLinkedModules;
exports.getLinkedModuleOutputPath = getLinkedModuleOutputPath;
const node_path_1 = __importDefault(require("node:path"));
const node_fs_1 = __importDefault(require("node:fs"));
const cli_utils_1 = require("@react-native-node-api/cli-utils");
const path_utils_1 = require("../path-utils");
async function linkModules({ fromPath, incremental, naming, platform, linker, }) {
// Find all their xcframeworks
const dependenciesByName = await (0, path_utils_1.findNodeApiModulePathsByDependency)({
fromPath,
platform,
includeSelf: true,
});
// Find absolute paths to xcframeworks
const absoluteModulePaths = Object.values(dependenciesByName).flatMap((dependency) => dependency.modulePaths.map((modulePath) => node_path_1.default.join(dependency.path, modulePath)));
const libraryMap = (0, path_utils_1.getLibraryMap)(absoluteModulePaths, naming);
const duplicates = new Map(Array.from(libraryMap.entries()).filter(([, paths]) => paths.length > 1));
if (duplicates.size > 0) {
const visualized = (0, path_utils_1.visualizeLibraryMap)(duplicates);
throw new Error("Found conflicting library names:\n" + visualized);
}
return Promise.all(absoluteModulePaths.map(async (originalPath) => {
try {
return await linker({
modulePath: originalPath,
incremental,
naming,
platform,
});
}
catch (error) {
if (error instanceof cli_utils_1.SpawnFailure) {
return {
originalPath,
skipped: false,
failure: error,
};
}
else {
throw error;
}
}
}));
}
async function pruneLinkedModules(platform, linkedModules) {
if (linkedModules.some(({ failure }) => failure)) {
// Don't prune if any of the modules failed to copy
return;
}
const platformOutputPath = (0, path_utils_1.getAutolinkPath)(platform);
// Pruning only when all modules are copied successfully
const expectedPaths = new Set([...linkedModules.map((m) => m.outputPath)]);
await Promise.all(node_fs_1.default.readdirSync(platformOutputPath).map(async (entry) => {
const candidatePath = node_path_1.default.resolve(platformOutputPath, entry);
if (!expectedPaths.has(candidatePath)) {
console.log("🧹Deleting", (0, cli_utils_1.prettyPath)(candidatePath), cli_utils_1.chalk.dim("(no longer linked)"));
await node_fs_1.default.promises.rm(candidatePath, { recursive: true, force: true });
}
}));
}
function getLinkedModuleOutputPath(platform, modulePath, naming) {
const libraryName = (0, path_utils_1.getLibraryName)(modulePath, naming);
if (platform === "android") {
return node_path_1.default.join((0, path_utils_1.getAutolinkPath)(platform), libraryName);
}
else if (platform === "apple") {
return node_path_1.default.join((0, path_utils_1.getAutolinkPath)(platform), libraryName + ".xcframework");
}
else {
throw new Error(`Unsupported platform: ${platform}`);
}
}