UNPKG

react-native-asset

Version:

Linking and unlinking of assets in your react-native app, works for fonts and sounds

95 lines (94 loc) 4.91 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = copyAssetsIos; const dntShim = __importStar(require("../_dnt.shims.js")); const path = __importStar(require("../deps/jsr.io/@std/path/1.1.4/mod.js")); const xcode = __importStar(require("xcode")); const xcodeParserUntyped = __importStar(require("xcode/lib/parser/pbxproj.js")); const xcodeParser = xcodeParserUntyped; const createGroupWithMessage_js_1 = __importDefault(require("../react-native-lib/ios/createGroupWithMessage.js")); const getPlist_js_1 = __importDefault(require("../react-native-lib/ios/getPlist.js")); const writePlist_js_1 = __importDefault(require("../react-native-lib/ios/writePlist.js")); const utils_js_1 = require("../utils.js"); async function copyAssetsIos(filePaths, platformConfig, options) { const project = xcode.project(platformConfig.pbxprojPath); const pbxprojContent = await dntShim.Deno.readFile(platformConfig.pbxprojPath).then((buf) => new TextDecoder("utf-8").decode(buf)); project.hash = xcodeParser.parse(pbxprojContent); (0, createGroupWithMessage_js_1.default)(project, "Resources"); const targetUUIDs = (0, utils_js_1.getTargetUUIDs)(project); for (const filePath of filePaths) { const relativeFilePath = path.relative(platformConfig.path, filePath); let file = false; for (const target of targetUUIDs) { if (!file) { file = project.addResourceFile(relativeFilePath, { target }); if (!file) { // We know the resource is already in the project but there's no obvious way to get the PBXFile reference // It's kinda sloppy but we can remove and re-add the resource to get the reference // This has the side effect of unlinking all other targets... not a problem because we're about to link them anyway project.removeResourceFile(relativeFilePath, { target }); file = project.addResourceFile(relativeFilePath, { target }); if (!file) { throw new Error(`Failed to add file to pbxproj "${filePath}"`); } } } else { // Each target & asset combination needs a different build phase UUID file.target = target; file.uuid = project.generateUuid(); project.addToPbxBuildFileSection(file); project.addToPbxResourcesBuildPhase(file); } } } await dntShim.Deno.writeTextFile(platformConfig.pbxprojPath, project.writeSync()); const fileBasenames = filePaths.map((p) => path.basename(p)); for (const targetUUID of targetUUIDs) { // deno-lint-ignore no-await-in-loop -- sequential read/write to same plist file const plist = await (0, getPlist_js_1.default)(project, platformConfig.path, targetUUID); if (options.addFont && plist) { const existingFonts = plist.UIAppFonts || []; const allFonts = [...existingFonts, ...fileBasenames]; plist.UIAppFonts = Array.from(new Set(allFonts)); // use Set to dedupe w/existing } // deno-lint-ignore no-await-in-loop -- sequential read/write to same plist file await (0, writePlist_js_1.default)(project, platformConfig.path, plist, targetUUID); } }