react-native-test-app
Version:
react-native-test-app provides a test app for all supported platforms as a package
148 lines (128 loc) • 3.63 kB
JavaScript
// @ts-check
import { spawnSync } from "node:child_process";
import * as path from "node:path";
import { fileURLToPath } from "node:url";
/**
* @typedef {import("../scripts/types.ts").ApplePlatform} ApplePlatform;
* @typedef {import("../scripts/types.ts").JSONObject} JSONObject;
* @typedef {import("../scripts/types.ts").JSONValue} JSONValue;
*/
const MAX_BUFFER = 16 * 1024 * 1024; // 16 MB because some plists can get big
/**
* @param {JSONValue} obj
* @returns {obj is JSONObject}
*/
export function isObject(obj) {
return Boolean(obj && typeof obj === "object" && !Array.isArray(obj));
}
/**
* @param {unknown} value
* @returns {value is string}
*/
export function isString(value) {
return typeof value === "string";
}
/**
* @param {JSONValue} value
* @param {string} key
* @returns {asserts value is JSONValue[]}
*/
export function assertArray(value, key) {
if (!Array.isArray(value)) {
throw new Error(`Expected '${key}' to be an array`);
}
}
/**
* @param {JSONValue} value
* @param {string} key
* @returns {asserts value is JSONObject}
*/
export function assertObject(value, key) {
if (!isObject(value)) {
throw new Error(`Expected '${key}' to be an object`);
}
}
/**
* @param {JSONValue} value
* @param {string} key
* @returns {asserts value is string}
*/
export function assertUniqueId(value, key) {
if (typeof value !== "string") {
throw new Error(`Expected '${key}' to be a unique id string`);
}
}
/**
* @param {string} filename
* @returns {JSONObject}
*/
export function jsonFromPlist(filename) {
const args = ["-convert", "json", "-o", "-", filename];
const plutil = spawnSync("/usr/bin/plutil", args, {
stdio: ["ignore", "pipe", "inherit"],
maxBuffer: MAX_BUFFER,
});
if (plutil.status !== 0) {
throw plutil.error ?? new Error(`Failed to read '${filename}'`);
}
return JSON.parse(plutil.stdout.toString());
}
/**
* @param {JSONObject} source
* @param {string} filename
* @returns {string}
*/
export function plistFromJSON(source, filename) {
const args = ["-convert", "xml1", "-r", "-o", "-", "--", "-"];
const plutil = spawnSync("/usr/bin/plutil", args, {
stdio: ["pipe", "pipe", "inherit"],
input: JSON.stringify(source),
maxBuffer: MAX_BUFFER,
});
if (plutil.status !== 0) {
throw plutil.error ?? new Error(`Failed to generate '${filename}'`);
}
return plutil.stdout.toString();
}
/**
* @param {string} p
* @param {ApplePlatform} targetPlatform
* @returns {string}
*/
export function projectPath(p, targetPlatform) {
const packageDir = path.dirname(path.dirname(fileURLToPath(import.meta.url)));
return path.resolve(packageDir, targetPlatform, p);
}
/**
* @param {JSONObject} appConfig
* @param {ApplePlatform} targetPlatform
* @returns {JSONValue[] | undefined}
*/
export function resolveResources(appConfig, targetPlatform) {
const resources = appConfig["resources"];
if (resources && typeof resources === "object") {
if (Array.isArray(resources)) {
return resources;
}
const res = resources[targetPlatform];
if (Array.isArray(res)) {
return res;
}
}
return undefined;
}
/**
* @param {string} destination
* @param {JSONObject} source
* @returns {void}
*/
export function writePlistFromJSON(destination, source) {
const args = ["-convert", "xml1", "-r", "-o", destination, "--", "-"];
const plutil = spawnSync("/usr/bin/plutil", args, {
stdio: ["pipe", "pipe", "inherit"],
input: JSON.stringify(source),
});
if (plutil.status !== 0) {
throw plutil.error ?? new Error(`Failed to write '${destination}'`);
}
}