|
| 1 | +// @ts-check |
| 2 | +import { spawnSync } from "node:child_process"; |
| 3 | +import * as nodefs from "node:fs"; |
| 4 | +import * as path from "node:path"; |
| 5 | +import { sourceForAppConfig } from "../scripts/appConfig.mjs"; |
| 6 | +import { readJSONFile } from "../scripts/helpers.js"; |
| 7 | +import { isObject, projectPath } from "./utils.mjs"; |
| 8 | + |
| 9 | +/** |
| 10 | + * @import { ApplePlatform, JSONObject, JSONValue } from "../scripts/types.js"; |
| 11 | + * |
| 12 | + * @typedef {{ filename: string; }} Icon; |
| 13 | + */ |
| 14 | + |
| 15 | +const ALTERNATE_ICONS_KEY = "alternateIcons"; |
| 16 | +const APP_ICON_KEY = "AppIcon"; |
| 17 | +const PRIMARY_ICON_KEY = "primaryIcon"; |
| 18 | + |
| 19 | +/** |
| 20 | + * @param {JSONValue} value |
| 21 | + * @returns {value is Icon} |
| 22 | + */ |
| 23 | +function isIcon(value) { |
| 24 | + return Boolean(value && typeof value === "object" && "filename" in value); |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * @param {[string, JSONValue]} entry |
| 29 | + * @returns {entry is [string, Icon]} |
| 30 | + */ |
| 31 | +function isIconEntry(entry) { |
| 32 | + const [, value] = entry; |
| 33 | + return isIcon(value); |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * @param {JSONValue} icons |
| 38 | + * @returns {[string, Icon][]} |
| 39 | + */ |
| 40 | +function parseAlternateIcons(icons) { |
| 41 | + if (!isObject(icons)) { |
| 42 | + return []; |
| 43 | + } |
| 44 | + |
| 45 | + /** @type {[string, Icon][]} */ |
| 46 | + const sanitizedIcons = []; |
| 47 | + for (const entry of Object.entries(icons)) { |
| 48 | + if (entry[0] !== APP_ICON_KEY && isIconEntry(entry)) { |
| 49 | + sanitizedIcons.push(entry); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + return sanitizedIcons; |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * @param {string} input |
| 58 | + * @param {string} output |
| 59 | + * @param {number} width |
| 60 | + * @param {number} height |
| 61 | + */ |
| 62 | +function resampleImage(input, output, width, height) { |
| 63 | + const args = [ |
| 64 | + "--resampleHeightWidth", |
| 65 | + height.toString(), |
| 66 | + width.toString(), |
| 67 | + "--out", |
| 68 | + output, |
| 69 | + input, |
| 70 | + ]; |
| 71 | + spawnSync("sips", args, { stdio: "inherit" }); |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * @param {JSONObject} appConfig |
| 76 | + * @param {ApplePlatform} targetPlatform |
| 77 | + * @param {string} destination |
| 78 | + * @returns {void} |
| 79 | + */ |
| 80 | +export function generateAssetsCatalogs( |
| 81 | + appConfig, |
| 82 | + targetPlatform, |
| 83 | + destination, |
| 84 | + resizeImage = resampleImage, |
| 85 | + fs = nodefs |
| 86 | +) { |
| 87 | + const xcassets_src = projectPath( |
| 88 | + "ReactTestApp/Assets.xcassets", |
| 89 | + targetPlatform |
| 90 | + ); |
| 91 | + const xcassets_dst = path.join(destination, path.basename(xcassets_src)); |
| 92 | + |
| 93 | + fs.rmSync(xcassets_dst, { force: true, maxRetries: 3, recursive: true }); |
| 94 | + fs.cpSync(xcassets_src, xcassets_dst, { recursive: true }); |
| 95 | + |
| 96 | + const platformConfig = appConfig[targetPlatform]; |
| 97 | + if (!isObject(platformConfig)) { |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + const icons = platformConfig["icons"]; |
| 102 | + if (!isObject(icons)) { |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + const primaryIcon = icons[PRIMARY_ICON_KEY]; |
| 107 | + if (!isIcon(primaryIcon)) { |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + const appIcons = parseAlternateIcons(icons[ALTERNATE_ICONS_KEY]); |
| 112 | + appIcons.push([APP_ICON_KEY, primaryIcon]); |
| 113 | + |
| 114 | + const templateIconSet = "AppIcon.appiconset"; |
| 115 | + const template = readJSONFile( |
| 116 | + path.join(xcassets_src, templateIconSet, "Contents.json") |
| 117 | + ); |
| 118 | + if (!Array.isArray(template.images)) { |
| 119 | + throw new Error(`${templateIconSet}: Expected 'images' to be an array`); |
| 120 | + } |
| 121 | + |
| 122 | + const appManifestDir = sourceForAppConfig(appConfig); |
| 123 | + const mkdirOptions = { recursive: true, mode: 0o755 }; |
| 124 | + |
| 125 | + for (const [setName, appIcon] of appIcons) { |
| 126 | + const appIconSet = path.join(destination, `${setName}.appiconset`); |
| 127 | + fs.mkdirSync(appIconSet, mkdirOptions); |
| 128 | + |
| 129 | + const icon = path.join(appManifestDir, appIcon.filename); |
| 130 | + const extname = path.extname(icon); |
| 131 | + const basename = path.basename(icon, extname); |
| 132 | + |
| 133 | + /** @type {Icon[]} */ |
| 134 | + const images = []; |
| 135 | + |
| 136 | + for (const image of template.images) { |
| 137 | + const { scale, size } = image; |
| 138 | + const [width, height] = size.split("x"); |
| 139 | + const filename = `${basename}-${height}@${scale}${extname}`; |
| 140 | + images.push({ filename, ...image }); |
| 141 | + |
| 142 | + const output = path.join(appIconSet, filename); |
| 143 | + const x = parseFloat(scale); |
| 144 | + resizeImage(icon, output, Number(width) * x, Number(height) * x); |
| 145 | + } |
| 146 | + |
| 147 | + const contents = { images, info: template["info"] }; |
| 148 | + const dest = path.join(appIconSet, "Contents.json"); |
| 149 | + fs.writeFileSync(dest, JSON.stringify(contents, undefined, 2)); |
| 150 | + } |
| 151 | +} |
0 commit comments