UNPKG

one

Version:

One is a React framework for web, iOS, and Android apps.

254 lines (253 loc) • 10.9 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var updatesPublish_exports = {}; __export(updatesPublish_exports, { runUpdatesPublish: () => runUpdatesPublish }); module.exports = __toCommonJS(updatesPublish_exports); var import_node_child_process = require("node:child_process"); var import_node_crypto = require("node:crypto"); var import_node_fs = require("node:fs"); var import_node_module = require("node:module"); var import_node_os = require("node:os"); var import_node_path = require("node:path"); var import_appManifest = require("../native/appManifest.cjs"); var import_loadConfig = require("../vite/loadConfig.cjs"); function lastValue(value) { return Array.isArray(value) ? value[value.length - 1] : value; } function allValues(value) { if (value === void 0) return []; return Array.isArray(value) ? value : [value]; } function base64Url(bytes) { return bytes.toString("base64").replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, ""); } function sha256Base64Url(path) { return base64Url((0, import_node_crypto.createHash)("sha256").update((0, import_node_fs.readFileSync)(path)).digest()); } function parseMetadata(entries) { const metadata = {}; for (const entry of entries) { const equals = entry.indexOf("="); if (equals <= 0) { throw new Error(`[one] --metadata must look like key=value, got "${entry}"`); } const key = entry.slice(0, equals); const raw = entry.slice(equals + 1); let value = raw; try { const parsed = JSON.parse(raw); if (typeof parsed === "string" || typeof parsed === "number" || typeof parsed === "boolean") { value = parsed; } } catch {} metadata[key] = value; } return metadata; } function collectFiles(root) { const files = []; const visit = (dir) => { for (const entry of (0, import_node_fs.readdirSync)(dir, { withFileTypes: true })) { const path = (0, import_node_path.join)(dir, entry.name); if (entry.isDirectory()) visit(path); else if (entry.isFile()) files.push(path); } }; if ((0, import_node_fs.existsSync)(root)) visit(root); return files.sort(); } function resolveCliJs(root) { const require2 = (0, import_node_module.createRequire)((0, import_node_path.join)(root, "package.json")); const packageJson = require2.resolve("react-native/package.json"); return (0, import_node_path.join)(packageJson.slice(0, -"package.json".length), "cli.js"); } function resolveNpmHermesc(root) { const require2 = (0, import_node_module.createRequire)((0, import_node_path.join)(root, "package.json")); const reactNativeJson = require2.resolve("react-native/package.json"); const nested = (0, import_node_module.createRequire)(reactNativeJson); const compilerJson = nested.resolve("hermes-compiler/package.json"); const dir = compilerJson.slice(0, -"package.json".length); const bin = process.platform === "darwin" ? "osx-bin" : process.platform === "win32" ? "win64-bin" : "linux64-bin"; return (0, import_node_path.join)(dir, "hermesc", bin, process.platform === "win32" ? "hermesc.exe" : "hermesc"); } function resolveHermesc(root, platform) { if (platform === "ios") { const pod = (0, import_node_path.join)(root, "ios", "Pods", "hermes-engine", "destroot", "bin", "hermesc"); if ((0, import_node_fs.existsSync)(pod)) return pod; } const npm = resolveNpmHermesc(root); if (!(0, import_node_fs.existsSync)(npm)) { throw new Error(`[one] hermesc not found at ${npm}: install dependencies${platform === "ios" ? " and run one prebuild with pod install" : ""} first`); } return npm; } async function resolveRuntimeVersion(platform, flag) { const { oneOptions } = await (0, import_loadConfig.loadUserOneOptions)("build", true); const native = oneOptions?.native; const app = typeof native === "object" ? native.app : void 0; if (!app) { if (!flag) { throw new Error("[one] one updates publish needs native.app.updates.runtimeVersion, or --runtime-version for an app built with expo prebuild: the binary only takes updates published for its own runtime version"); } return flag; } (0, import_appManifest.validateNativeApp)(app, platform); const runtimeVersion = app.updates?.runtimeVersion; if (!runtimeVersion) { throw new Error("[one] one updates publish needs native.app.updates.runtimeVersion: the binary only takes updates published for its own runtime version"); } if (flag !== void 0 && flag !== runtimeVersion) { throw new Error(`[one] --runtime-version ${flag} differs from native.app.updates.runtimeVersion ${runtimeVersion}`); } return runtimeVersion; } async function runUpdatesPublish(args) { const platform = lastValue(args.platform); if (platform !== "ios" && platform !== "android") { throw new Error("[one] one updates publish needs --platform ios|android"); } const out = lastValue(args.out); if (!out) { throw new Error("[one] one updates publish needs --out <dir>"); } const intermediatesOut = lastValue(args.intermediatesOut); if (intermediatesOut) { const published = (0, import_node_path.resolve)(out); const debug = (0, import_node_path.resolve)(intermediatesOut); if (published === debug || published.startsWith(`${debug}${import_node_path.sep}`) || debug.startsWith(`${published}${import_node_path.sep}`)) { throw new Error("[one] --intermediates-out must be separate from --out"); } if ((0, import_node_fs.existsSync)(debug) && (0, import_node_fs.readdirSync)(debug).length > 0) { throw new Error(`[one] refusing to write intermediates into non-empty ${debug}`); } } const root = process.cwd(); const runtimeVersion = await resolveRuntimeVersion(platform, lastValue(args.runtimeVersion)); if ((0, import_node_fs.existsSync)(out) && (0, import_node_fs.readdirSync)(out).length > 0) { throw new Error(`[one] refusing to publish into non-empty ${out}: every publish writes a fresh directory`); } (0, import_node_fs.mkdirSync)(out, { recursive: true }); if (intermediatesOut) { (0, import_node_fs.mkdirSync)(intermediatesOut, { recursive: true }); const published = (0, import_node_fs.realpathSync)(out); const debug = (0, import_node_fs.realpathSync)(intermediatesOut); if (published === debug || published.startsWith(`${debug}${import_node_path.sep}`) || debug.startsWith(`${published}${import_node_path.sep}`)) { throw new Error("[one] --intermediates-out must be separate from --out"); } } const assetsOut = (0, import_node_path.join)(out, "assets"); (0, import_node_fs.mkdirSync)(assetsOut, { recursive: true }); const metadata = parseMetadata(allValues(args.metadata)); const work = (0, import_node_fs.mkdtempSync)((0, import_node_path.join)((0, import_node_os.tmpdir)(), "one-updates-publish-")); const bundleJs = (0, import_node_path.join)(work, "bundle.js"); const packagerMap = (0, import_node_path.join)(work, "bundle.js.map"); const assetsDir = (0, import_node_path.join)(work, "assets"); const bytecode = (0, import_node_path.join)(work, "main.jsbundle"); const composedMap = (0, import_node_path.join)(work, "main.jsbundle.map"); try { (0, import_node_child_process.execFileSync)(process.execPath, [ resolveCliJs(root), "bundle", "--entry-file", "index.js", "--platform", platform, "--dev", "false", "--reset-cache", "--bundle-output", bundleJs, ...intermediatesOut ? ["--sourcemap-output", packagerMap] : [], "--assets-dest", assetsDir, "--minify", platform === "ios" ? "true" : "false" ], { cwd: root, stdio: "inherit" }); const hermesc = resolveHermesc(root, platform); const hermescArgs = ["-emit-binary", "-max-diagnostic-width=80"]; if (platform === "android") hermescArgs.push("-w"); hermescArgs.push("-O"); if (intermediatesOut) hermescArgs.push("-output-source-map"); hermescArgs.push("-out", bytecode, bundleJs); try { (0, import_node_child_process.execFileSync)(hermesc, hermescArgs, { cwd: root, stdio: "inherit" }); } catch (error) { throw new Error(`[one] hermesc failed to compile the ${platform} update bundle${error instanceof Error ? `: ${error.message}` : ""}`); } if (intermediatesOut) { const require2 = (0, import_node_module.createRequire)((0, import_node_path.join)(root, "package.json")); const reactNativeJson = require2.resolve("react-native/package.json"); const composeSourceMaps = (0, import_node_path.join)(reactNativeJson.slice(0, -"package.json".length), "scripts", "compose-source-maps.js"); (0, import_node_child_process.execFileSync)(process.execPath, [ composeSourceMaps, packagerMap, `${bytecode}.map`, "-o", composedMap ], { cwd: root, stdio: "inherit" }); (0, import_node_fs.writeFileSync)((0, import_node_path.join)(intermediatesOut, "bundle.js"), (0, import_node_fs.readFileSync)(bundleJs)); (0, import_node_fs.writeFileSync)((0, import_node_path.join)(intermediatesOut, "main.jsbundle.map"), (0, import_node_fs.readFileSync)(composedMap)); } const bundleHash = sha256Base64Url(bytecode); (0, import_node_fs.writeFileSync)((0, import_node_path.join)(assetsOut, bundleHash), (0, import_node_fs.readFileSync)(bytecode)); const assets = []; for (const file of collectFiles(assetsDir)) { const hash = sha256Base64Url(file); const target = (0, import_node_path.join)(assetsOut, hash); if (!(0, import_node_fs.existsSync)(target)) (0, import_node_fs.writeFileSync)(target, (0, import_node_fs.readFileSync)(file)); assets.push({ hash, url: `assets/${hash}`, path: (0, import_node_path.relative)(assetsDir, file).split(import_node_path.sep).join("/") }); } assets.sort((a, b) => a.path < b.path ? -1 : a.path > b.path ? 1 : 0); const manifest = { id: (0, import_node_crypto.randomUUID)(), createdAt: (/* @__PURE__ */ new Date()).toISOString(), runtimeVersion, launchAsset: { hash: bundleHash, url: `assets/${bundleHash}`, path: "main.jsbundle" }, assets, metadata }; (0, import_node_fs.writeFileSync)((0, import_node_path.join)(out, "manifest.json"), `${JSON.stringify(manifest, null, 2)} `); console.info(`[one] published ${platform} update ${manifest.id} (${assets.length} assets) to ${out}`); } finally { (0, import_node_fs.rmSync)(work, { recursive: true, force: true }); } }