|
| 1 | +// @ts-check |
| 2 | +import * as nodefs from "node:fs"; |
| 3 | +import * as path from "node:path"; |
| 4 | +import { |
| 5 | + isObject, |
| 6 | + jsonFromPlist, |
| 7 | + plistFromJSON, |
| 8 | + projectPath, |
| 9 | + resolveResources, |
| 10 | +} from "./utils.mjs"; |
| 11 | + |
| 12 | +/** @import { ApplePlatform, JSONArray, JSONObject } from "../scripts/types.js"; */ |
| 13 | + |
| 14 | +/** |
| 15 | + * @param {JSONObject} appConfig |
| 16 | + * @param {ApplePlatform} targetPlatform |
| 17 | + * @param {JSONObject} info |
| 18 | + */ |
| 19 | +function setMacProperties(appConfig, targetPlatform, info) { |
| 20 | + if (targetPlatform !== "macos") { |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + const config = appConfig[targetPlatform]; |
| 25 | + if (!isObject(config)) { |
| 26 | + return; |
| 27 | + } |
| 28 | + |
| 29 | + const category = config["applicationCategoryType"]; |
| 30 | + if (typeof category === "string") { |
| 31 | + info["LSApplicationCategoryType"] = category; |
| 32 | + } |
| 33 | + |
| 34 | + const copyright = config["humanReadableCopyright"]; |
| 35 | + if (typeof copyright === "string") { |
| 36 | + info["NSHumanReadableCopyright"] = copyright; |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * @param {JSONObject} appConfig |
| 42 | + * @param {ApplePlatform} targetPlatform |
| 43 | + * @param {string} destination |
| 44 | + * @returns {void} |
| 45 | + */ |
| 46 | +export function generateInfoPlist( |
| 47 | + appConfig, |
| 48 | + targetPlatform, |
| 49 | + destination, |
| 50 | + fs = nodefs |
| 51 | +) { |
| 52 | + const filename = "ReactTestApp/Info.plist"; |
| 53 | + const infoPlistSrc = projectPath(filename, targetPlatform); |
| 54 | + |
| 55 | + const info = jsonFromPlist(infoPlistSrc); |
| 56 | + |
| 57 | + const resources = resolveResources(appConfig, targetPlatform); |
| 58 | + registerFonts(resources, targetPlatform, info); |
| 59 | + setMacProperties(appConfig, targetPlatform, info); |
| 60 | + |
| 61 | + const infoPlistDst = path.join(destination, path.basename(infoPlistSrc)); |
| 62 | + fs.writeFileSync(infoPlistDst, plistFromJSON(info, filename)); |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * @param {JSONArray | undefined} resources |
| 67 | + * @param {ApplePlatform} targetPlatform |
| 68 | + * @param {JSONObject} info |
| 69 | + */ |
| 70 | +export function registerFonts(resources, targetPlatform, info) { |
| 71 | + if (!resources) { |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + const fontFiles = [".otf", ".ttf"]; |
| 76 | + const fonts = []; |
| 77 | + for (const filename of resources) { |
| 78 | + if (typeof filename === "string") { |
| 79 | + const extname = path.extname(filename); |
| 80 | + if (fontFiles.includes(extname)) { |
| 81 | + fonts.push(path.basename(filename)); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + if (fonts.length > 0) { |
| 87 | + switch (targetPlatform) { |
| 88 | + case "macos": |
| 89 | + // https://developer.apple.com/documentation/bundleresources/information_property_list/atsapplicationfontspath |
| 90 | + info["ATSApplicationFontsPath"] = "."; |
| 91 | + break; |
| 92 | + default: |
| 93 | + // https://developer.apple.com/documentation/uikit/text_display_and_fonts/adding_a_custom_font_to_your_app |
| 94 | + info["UIAppFonts"] = fonts; |
| 95 | + break; |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments