create-expo-updates-server 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (117) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +19 -0
  3. package/bin/create-expo-updates-server.mjs +5 -0
  4. package/dist/template/.env.docker.example +15 -0
  5. package/dist/template/.env.example +35 -0
  6. package/dist/template/Dockerfile +42 -0
  7. package/dist/template/LICENSE +21 -0
  8. package/dist/template/README.md +665 -0
  9. package/dist/template/app/api/admin/access-tokens/[id]/route.ts +25 -0
  10. package/dist/template/app/api/admin/access-tokens/route.test.ts +73 -0
  11. package/dist/template/app/api/admin/access-tokens/route.ts +90 -0
  12. package/dist/template/app/api/admin/embedded-updates/[embeddedUpdateId]/route.ts +25 -0
  13. package/dist/template/app/api/admin/embedded-updates/route.test.ts +102 -0
  14. package/dist/template/app/api/admin/embedded-updates/route.ts +47 -0
  15. package/dist/template/app/api/admin/emergency-redirects/[id]/route.ts +62 -0
  16. package/dist/template/app/api/admin/emergency-redirects/route.test.ts +84 -0
  17. package/dist/template/app/api/admin/emergency-redirects/route.ts +56 -0
  18. package/dist/template/app/api/admin/login/route.test.ts +55 -0
  19. package/dist/template/app/api/admin/login/route.ts +52 -0
  20. package/dist/template/app/api/admin/logout/route.ts +20 -0
  21. package/dist/template/app/api/admin/session/route.ts +15 -0
  22. package/dist/template/app/api/admin/updates/[encodedKey]/deactivate/route.ts +33 -0
  23. package/dist/template/app/api/admin/updates/[encodedKey]/promote/route.ts +29 -0
  24. package/dist/template/app/api/admin/updates/[encodedKey]/rollback/route.ts +29 -0
  25. package/dist/template/app/api/admin/updates/[encodedKey]/route.ts +96 -0
  26. package/dist/template/app/api/admin/updates/route.test.ts +142 -0
  27. package/dist/template/app/api/admin/updates/route.ts +32 -0
  28. package/dist/template/app/api/assets/route.ts +6 -0
  29. package/dist/template/app/api/health/route.test.ts +26 -0
  30. package/dist/template/app/api/health/route.ts +42 -0
  31. package/dist/template/app/api/manifest/route.ts +6 -0
  32. package/dist/template/app/api/update-complete/route.ts +6 -0
  33. package/dist/template/app/favicon.ico +0 -0
  34. package/dist/template/app/globals.css +26 -0
  35. package/dist/template/app/layout.tsx +34 -0
  36. package/dist/template/app/login/page.tsx +77 -0
  37. package/dist/template/app/page.tsx +12 -0
  38. package/dist/template/app/updates/[encodedKey]/page.tsx +482 -0
  39. package/dist/template/app/updates/embedded/page.tsx +164 -0
  40. package/dist/template/app/updates/emergency-redirects/page.tsx +430 -0
  41. package/dist/template/app/updates/layout.tsx +14 -0
  42. package/dist/template/app/updates/page.tsx +281 -0
  43. package/dist/template/docker-compose.yml +100 -0
  44. package/dist/template/docs/expo-app-setup.md +497 -0
  45. package/dist/template/docs/getting-started.md +502 -0
  46. package/dist/template/docs/getting-started.ru.md +623 -0
  47. package/dist/template/docs/index.md +57 -0
  48. package/dist/template/docs/migrations/schema.sql +363 -0
  49. package/dist/template/docs/npm-installer.md +83 -0
  50. package/dist/template/docs/operations-guide.md +270 -0
  51. package/dist/template/docs/ota-emergency-recovery.md +109 -0
  52. package/dist/template/docs/supabase-setup.md +133 -0
  53. package/dist/template/eslint.config.mjs +30 -0
  54. package/dist/template/gitignore.template +58 -0
  55. package/dist/template/next.config.ts +7 -0
  56. package/dist/template/package-lock.json +12156 -0
  57. package/dist/template/package.json +50 -0
  58. package/dist/template/postcss.config.mjs +7 -0
  59. package/dist/template/public/file.svg +1 -0
  60. package/dist/template/public/globe.svg +1 -0
  61. package/dist/template/public/next.svg +1 -0
  62. package/dist/template/public/vercel.svg +1 -0
  63. package/dist/template/public/window.svg +1 -0
  64. package/dist/template/scripts/configure-expo-app.mjs +145 -0
  65. package/dist/template/scripts/create-access-token.mjs +135 -0
  66. package/dist/template/scripts/create-admin.mjs +169 -0
  67. package/dist/template/scripts/import-emergency-redirects.mjs +126 -0
  68. package/dist/template/scripts/lib/embedded-update-registrar.test.ts +64 -0
  69. package/dist/template/scripts/lib/expo-app-configurator.mjs +666 -0
  70. package/dist/template/scripts/lib/expo-app-configurator.test.ts +463 -0
  71. package/dist/template/scripts/lib/expo-app-prompts.mjs +139 -0
  72. package/dist/template/scripts/lib/expo-app-prompts.test.ts +97 -0
  73. package/dist/template/scripts/lib/expo-export-command.test.ts +31 -0
  74. package/dist/template/scripts/lib/ota-publish-retry.test.ts +33 -0
  75. package/dist/template/scripts/manage-emergency-redirect.mjs +483 -0
  76. package/dist/template/scripts/sync-docker-database-password.sh +16 -0
  77. package/dist/template/src/client/admin-api.ts +174 -0
  78. package/dist/template/src/client/admin-types.ts +93 -0
  79. package/dist/template/src/client/logout-button.tsx +31 -0
  80. package/dist/template/src/server/controllers/assets.ts +216 -0
  81. package/dist/template/src/server/controllers/manifest.test.ts +403 -0
  82. package/dist/template/src/server/controllers/manifest.ts +626 -0
  83. package/dist/template/src/server/controllers/update-complete.ts +36 -0
  84. package/dist/template/src/server/lib/admin-access-tokens.ts +169 -0
  85. package/dist/template/src/server/lib/admin-api.ts +101 -0
  86. package/dist/template/src/server/lib/admin-auth.ts +208 -0
  87. package/dist/template/src/server/lib/admin-embedded-updates.ts +150 -0
  88. package/dist/template/src/server/lib/admin-updates.ts +1282 -0
  89. package/dist/template/src/server/lib/common.js +24 -0
  90. package/dist/template/src/server/lib/emergency-channel-redirects.ts +310 -0
  91. package/dist/template/src/server/lib/env.js +8 -0
  92. package/dist/template/src/server/lib/manifest-helpers.js +91 -0
  93. package/dist/template/src/server/lib/response.js +11 -0
  94. package/dist/template/src/server/lib/signing.js +55 -0
  95. package/dist/template/src/server/lib/supabase.js +763 -0
  96. package/dist/template/src/server/manifest/manifest-repository.ts +269 -0
  97. package/dist/template/src/server/manifest/manifest-request.ts +21 -0
  98. package/dist/template/src/server/manifest/manifest-response.ts +235 -0
  99. package/dist/template/src/server/manifest/manifest-values.ts +62 -0
  100. package/dist/template/templates/expo-app/.env.ota.example +18 -0
  101. package/dist/template/templates/expo-app/ci_scripts/ci_post_xcodebuild.sh +6 -0
  102. package/dist/template/templates/expo-app/ci_scripts/register-embedded-update.sh +36 -0
  103. package/dist/template/templates/expo-app/plugins/with-ota-embedded-registration.js +33 -0
  104. package/dist/template/templates/expo-app/scripts/ota-publish/api/database-client.mjs +87 -0
  105. package/dist/template/templates/expo-app/scripts/ota-publish/api/storage-client.mjs +57 -0
  106. package/dist/template/templates/expo-app/scripts/ota-publish/index.mjs +182 -0
  107. package/dist/template/templates/expo-app/scripts/ota-publish/lib/common.mjs +84 -0
  108. package/dist/template/templates/expo-app/scripts/ota-publish/lib/expo-export-command.mjs +24 -0
  109. package/dist/template/templates/expo-app/scripts/ota-publish/lib/retry.mjs +25 -0
  110. package/dist/template/templates/expo-app/scripts/ota-publish/shared/config.mjs +48 -0
  111. package/dist/template/templates/expo-app/scripts/ota-register-embedded/android-register-embedded-update.sh +18 -0
  112. package/dist/template/templates/expo-app/scripts/ota-register-embedded/index.mjs +275 -0
  113. package/dist/template/tsconfig.json +36 -0
  114. package/dist/template/vitest.config.mts +20 -0
  115. package/dist/template-manifest.json +564 -0
  116. package/lib/cli.mjs +549 -0
  117. package/package.json +41 -0
@@ -0,0 +1,666 @@
1
+ import { execFileSync } from "node:child_process";
2
+ import fs from "node:fs";
3
+ import path from "node:path";
4
+ import { fileURLToPath } from "node:url";
5
+
6
+ const CHANNEL_PATTERN = /^[a-z0-9][a-z0-9._-]{0,63}$/;
7
+ const PUBLISHER_DEPENDENCIES = [
8
+ "@aws-sdk/client-s3",
9
+ "dotenv",
10
+ "pg",
11
+ ];
12
+
13
+ function readJson(filePath) {
14
+ try {
15
+ return JSON.parse(fs.readFileSync(filePath, "utf8"));
16
+ } catch (error) {
17
+ throw new Error(`Unable to read JSON file ${filePath}: ${error.message}`);
18
+ }
19
+ }
20
+
21
+ function writeJson(filePath, value) {
22
+ fs.writeFileSync(filePath, `${JSON.stringify(value, null, 2)}\n`, "utf8");
23
+ }
24
+
25
+ function normalizeRequired(value, name) {
26
+ const normalized = String(value || "").trim();
27
+ if (!normalized) {
28
+ throw new Error(`${name} is required.`);
29
+ }
30
+ return normalized;
31
+ }
32
+
33
+ function resolveManifestUrl(value) {
34
+ const raw = normalizeRequired(value, "--server-url");
35
+ let url;
36
+
37
+ try {
38
+ url = new URL(raw);
39
+ } catch {
40
+ throw new Error("--server-url must be a valid HTTP or HTTPS URL.");
41
+ }
42
+
43
+ if (url.protocol !== "http:" && url.protocol !== "https:") {
44
+ throw new Error("--server-url must use HTTP or HTTPS.");
45
+ }
46
+
47
+ if (url.search || url.hash) {
48
+ throw new Error("--server-url must not contain query parameters or a hash.");
49
+ }
50
+
51
+ const pathWithoutTrailingSlash = url.pathname.replace(/\/+$/, "");
52
+ url.pathname =
53
+ pathWithoutTrailingSlash.endsWith("/api/manifest")
54
+ ? pathWithoutTrailingSlash
55
+ : `${pathWithoutTrailingSlash}/api/manifest`.replace(/\/+/g, "/");
56
+
57
+ return url.toString().replace(/\/$/, "");
58
+ }
59
+
60
+ function validateCertificate(certificatePath) {
61
+ if (!fs.existsSync(certificatePath)) {
62
+ throw new Error(`Certificate not found: ${certificatePath}`);
63
+ }
64
+
65
+ const content = fs.readFileSync(certificatePath, "utf8");
66
+ if (!content.includes("-----BEGIN CERTIFICATE-----")) {
67
+ throw new Error(
68
+ "--certificate must point to a public PEM certificate, not a private key.",
69
+ );
70
+ }
71
+
72
+ let certificateDetails;
73
+ try {
74
+ certificateDetails = execFileSync(
75
+ "openssl",
76
+ ["x509", "-in", certificatePath, "-noout", "-text"],
77
+ { encoding: "utf8" },
78
+ );
79
+ } catch {
80
+ throw new Error(
81
+ "--certificate must be a valid X.509 PEM certificate readable by OpenSSL.",
82
+ );
83
+ }
84
+
85
+ const hasDigitalSignature =
86
+ /X509v3 Key Usage:[^\n]*\n\s*Digital Signature\b/.test(
87
+ certificateDetails,
88
+ );
89
+ const hasCodeSigning =
90
+ /X509v3 Extended Key Usage:[^\n]*\n\s*(?:[^\n]*,\s*)?Code Signing\b/.test(
91
+ certificateDetails,
92
+ );
93
+ if (!hasDigitalSignature || !hasCodeSigning) {
94
+ throw new Error(
95
+ "--certificate must include X509v3 Key Usage: Digital Signature and Extended Key Usage: Code Signing.",
96
+ );
97
+ }
98
+ }
99
+
100
+ function detectPackageManager(appRoot) {
101
+ if (fs.existsSync(path.join(appRoot, "pnpm-lock.yaml"))) {
102
+ return "pnpm";
103
+ }
104
+ if (fs.existsSync(path.join(appRoot, "yarn.lock"))) {
105
+ return "yarn";
106
+ }
107
+ if (
108
+ fs.existsSync(path.join(appRoot, "bun.lock")) ||
109
+ fs.existsSync(path.join(appRoot, "bun.lockb"))
110
+ ) {
111
+ return "bun";
112
+ }
113
+ return "npm";
114
+ }
115
+
116
+ function getPackageCommands(packageManager, platform) {
117
+ const prebuildArgs = ["expo", "prebuild"];
118
+ if (platform !== "all") {
119
+ prebuildArgs.push("--platform", platform);
120
+ }
121
+
122
+ if (packageManager === "pnpm") {
123
+ return {
124
+ installExpoUpdates: ["pnpm", ["exec", "expo", "install", "expo-updates"]],
125
+ installPublisherDependencies: [
126
+ "pnpm",
127
+ ["add", "--save-dev", ...PUBLISHER_DEPENDENCIES],
128
+ ],
129
+ prebuild: ["pnpm", ["exec", ...prebuildArgs]],
130
+ };
131
+ }
132
+
133
+ if (packageManager === "yarn") {
134
+ return {
135
+ installExpoUpdates: ["yarn", ["expo", "install", "expo-updates"]],
136
+ installPublisherDependencies: [
137
+ "yarn",
138
+ ["add", "--dev", ...PUBLISHER_DEPENDENCIES],
139
+ ],
140
+ prebuild: ["yarn", prebuildArgs],
141
+ };
142
+ }
143
+
144
+ if (packageManager === "bun") {
145
+ return {
146
+ installExpoUpdates: ["bunx", ["expo", "install", "expo-updates"]],
147
+ installPublisherDependencies: [
148
+ "bun",
149
+ ["add", "--dev", ...PUBLISHER_DEPENDENCIES],
150
+ ],
151
+ prebuild: ["bunx", prebuildArgs],
152
+ };
153
+ }
154
+
155
+ return {
156
+ installExpoUpdates: ["npx", ["expo", "install", "expo-updates"]],
157
+ installPublisherDependencies: [
158
+ "npm",
159
+ ["install", "--save-dev", ...PUBLISHER_DEPENDENCIES],
160
+ ],
161
+ prebuild: ["npx", prebuildArgs],
162
+ };
163
+ }
164
+
165
+ function stopAndroidGradleIfPresent(appRoot, platform, commandRunner) {
166
+ if (platform === "ios") {
167
+ return;
168
+ }
169
+
170
+ const androidRoot = path.join(appRoot, "android");
171
+ const unixWrapper = path.join(androidRoot, "gradlew");
172
+ const windowsWrapper = path.join(androidRoot, "gradlew.bat");
173
+ const wrapper = fs.existsSync(unixWrapper)
174
+ ? unixWrapper
175
+ : fs.existsSync(windowsWrapper)
176
+ ? windowsWrapper
177
+ : null;
178
+
179
+ if (wrapper) {
180
+ commandRunner(wrapper, ["--stop"], androidRoot);
181
+ }
182
+ }
183
+
184
+ function assertCopyAllowed(source, destination, force) {
185
+ if (fs.existsSync(destination) && !force) {
186
+ const current = fs.readFileSync(destination);
187
+ const next = fs.readFileSync(source);
188
+ if (!current.equals(next)) {
189
+ throw new Error(
190
+ `Refusing to overwrite ${destination}. Re-run with --force.`,
191
+ );
192
+ }
193
+ }
194
+ }
195
+
196
+ function copyFile(source, destination) {
197
+ fs.mkdirSync(path.dirname(destination), { recursive: true });
198
+ fs.copyFileSync(source, destination);
199
+ }
200
+
201
+ function listFilesRecursive(directory) {
202
+ return fs.readdirSync(directory, { withFileTypes: true }).flatMap((entry) => {
203
+ const fullPath = path.join(directory, entry.name);
204
+ return entry.isDirectory() ? listFilesRecursive(fullPath) : [fullPath];
205
+ });
206
+ }
207
+
208
+ function parseEnvFile(filePath) {
209
+ const values = {};
210
+ const content = fs.readFileSync(filePath, "utf8");
211
+
212
+ for (const rawLine of content.split(/\r?\n/)) {
213
+ const line = rawLine.trim();
214
+ if (!line || line.startsWith("#")) {
215
+ continue;
216
+ }
217
+
218
+ const separator = line.indexOf("=");
219
+ if (separator <= 0) {
220
+ continue;
221
+ }
222
+
223
+ const key = line.slice(0, separator).trim();
224
+ let value = line.slice(separator + 1).trim();
225
+ if (
226
+ (value.startsWith('"') && value.endsWith('"')) ||
227
+ (value.startsWith("'") && value.endsWith("'"))
228
+ ) {
229
+ value = value.slice(1, -1);
230
+ }
231
+ values[key] = value;
232
+ }
233
+
234
+ return values;
235
+ }
236
+
237
+ function getLocalDockerPublisherEnv({
238
+ channel,
239
+ serverRoot,
240
+ serverUrl,
241
+ }) {
242
+ const hostname = new URL(serverUrl).hostname;
243
+ const usesLocalDocker =
244
+ hostname === "localhost" ||
245
+ hostname === "127.0.0.1" ||
246
+ hostname.endsWith(".devtunnels.ms");
247
+ if (!usesLocalDocker) {
248
+ return null;
249
+ }
250
+
251
+ const serverEnvPath = path.join(serverRoot, ".env");
252
+ if (!fs.existsSync(serverEnvPath)) {
253
+ return null;
254
+ }
255
+
256
+ const env = parseEnvFile(serverEnvPath);
257
+ const databaseUser = env.POSTGRES_USER || "expo_updates";
258
+ const databasePassword = env.POSTGRES_PASSWORD;
259
+ const databaseName = env.POSTGRES_DB || "expo_updates";
260
+ const databasePort = env.POSTGRES_PORT || "5432";
261
+ const storageUser = env.MINIO_ROOT_USER || "expo_updates";
262
+ const storagePassword = env.MINIO_ROOT_PASSWORD;
263
+ const storagePort = env.MINIO_API_PORT || "9000";
264
+ const bucket = env.OTA_STORAGE_BUCKET || "expo-updates";
265
+
266
+ if (!databasePassword || !storagePassword) {
267
+ return null;
268
+ }
269
+
270
+ const databaseUrl =
271
+ `postgresql://${encodeURIComponent(databaseUser)}:` +
272
+ `${encodeURIComponent(databasePassword)}@127.0.0.1:` +
273
+ `${databasePort}/${encodeURIComponent(databaseName)}`;
274
+
275
+ return [
276
+ `EXPO_UPDATE_CHANNEL=${channel}`,
277
+ `OTA_SERVER_URL=${serverUrl}`,
278
+ "",
279
+ `OTA_DATABASE_URL=${databaseUrl}`,
280
+ "OTA_DATABASE_SSL=false",
281
+ "",
282
+ `R2_ENDPOINT=http://127.0.0.1:${storagePort}`,
283
+ `R2_ACCESS_KEY_ID=${storageUser}`,
284
+ `R2_SECRET_ACCESS_KEY=${storagePassword}`,
285
+ `R2_BUCKET=${bucket}`,
286
+ "R2_REGION=us-east-1",
287
+ "S3_FORCE_PATH_STYLE=true",
288
+ "",
289
+ ].join("\n");
290
+ }
291
+
292
+ function appendGitignore(appRoot) {
293
+ const filePath = path.join(appRoot, ".gitignore");
294
+ const current = fs.existsSync(filePath)
295
+ ? fs.readFileSync(filePath, "utf8")
296
+ : "";
297
+ const requiredEntries = [".env.ota", ".ota-dist/"];
298
+ const lines = new Set(current.split(/\r?\n/));
299
+ const missing = requiredEntries.filter((entry) => !lines.has(entry));
300
+
301
+ if (missing.length === 0) {
302
+ return;
303
+ }
304
+
305
+ const prefix = current && !current.endsWith("\n") ? "\n" : "";
306
+ fs.appendFileSync(filePath, `${prefix}${missing.join("\n")}\n`, "utf8");
307
+ }
308
+
309
+ function patchAppJson(appJson, options) {
310
+ const expo = appJson.expo;
311
+ expo.runtimeVersion = options.runtimeVersion;
312
+ expo.updates = {
313
+ ...(expo.updates || {}),
314
+ enabled: true,
315
+ url: options.manifestUrl,
316
+ checkAutomatically: "NEVER",
317
+ fallbackToCacheTimeout: 0,
318
+ codeSigningCertificate:
319
+ "./certificates/code-signing-certificate.pem",
320
+ codeSigningMetadata: {
321
+ alg: "rsa-v1_5-sha256",
322
+ keyid: "main",
323
+ },
324
+ requestHeaders: {
325
+ ...(expo.updates?.requestHeaders || {}),
326
+ "expo-channel-name": options.channel,
327
+ },
328
+ };
329
+ expo.extra = {
330
+ ...(expo.extra || {}),
331
+ updateChannel: options.channel,
332
+ };
333
+ const plugins = Array.isArray(expo.plugins) ? expo.plugins : [];
334
+ if (!plugins.includes("./plugins/with-ota-embedded-registration")) {
335
+ expo.plugins = [
336
+ ...plugins,
337
+ "./plugins/with-ota-embedded-registration",
338
+ ];
339
+ }
340
+
341
+ return appJson;
342
+ }
343
+
344
+ function patchPackageJson(packageJson, platform) {
345
+ packageJson.scripts = {
346
+ ...(packageJson.scripts || {}),
347
+ "ota:publish": "node scripts/ota-publish/index.mjs",
348
+ };
349
+ if (platform !== "ios") {
350
+ packageJson.scripts["ota:build:android:apk"] =
351
+ "cd android && ./gradlew app:assembleRelease && cd .. && OTA_EMBEDDED_REGISTER_STRICT=true sh scripts/ota-register-embedded/android-register-embedded-update.sh";
352
+ packageJson.scripts["ota:build:android:aab"] =
353
+ "cd android && ./gradlew app:bundleRelease && cd .. && OTA_EMBEDDED_REGISTER_STRICT=true sh scripts/ota-register-embedded/android-register-embedded-update.sh";
354
+ }
355
+ return packageJson;
356
+ }
357
+
358
+ function patchPublisherEnvExample(filePath, channel) {
359
+ const current = fs.readFileSync(filePath, "utf8");
360
+ const next = current.replace(
361
+ /^EXPO_UPDATE_CHANNEL=.*$/m,
362
+ `EXPO_UPDATE_CHANNEL=${channel}`,
363
+ );
364
+ fs.writeFileSync(filePath, next, "utf8");
365
+ }
366
+
367
+ function validateProject(appRoot) {
368
+ const packagePath = path.join(appRoot, "package.json");
369
+ const appJsonPath = path.join(appRoot, "app.json");
370
+
371
+ if (!fs.existsSync(packagePath)) {
372
+ throw new Error(`Expo package.json not found in ${appRoot}`);
373
+ }
374
+ if (!fs.existsSync(appJsonPath)) {
375
+ throw new Error(`Static Expo app.json not found in ${appRoot}`);
376
+ }
377
+
378
+ const dynamicConfig = [
379
+ "app.config.js",
380
+ "app.config.cjs",
381
+ "app.config.mjs",
382
+ "app.config.ts",
383
+ ].find((fileName) => fs.existsSync(path.join(appRoot, fileName)));
384
+
385
+ if (dynamicConfig) {
386
+ throw new Error(
387
+ `${dynamicConfig} overrides app.json. This CLI mode supports static app.json only.`,
388
+ );
389
+ }
390
+
391
+ const packageJson = readJson(packagePath);
392
+ const appJson = readJson(appJsonPath);
393
+
394
+ if (!packageJson.dependencies?.expo && !packageJson.devDependencies?.expo) {
395
+ throw new Error("The target package.json does not contain Expo.");
396
+ }
397
+ if (!appJson.expo || typeof appJson.expo !== "object") {
398
+ throw new Error("app.json must contain an expo object.");
399
+ }
400
+
401
+ return { packagePath, appJsonPath, packageJson, appJson };
402
+ }
403
+
404
+ export function createCommandRunner() {
405
+ return (command, args, cwd) => {
406
+ execFileSync(command, args, {
407
+ cwd,
408
+ stdio: "inherit",
409
+ env: process.env,
410
+ });
411
+ };
412
+ }
413
+
414
+ export function configureExpoApp(
415
+ rawOptions,
416
+ {
417
+ commandRunner = createCommandRunner(),
418
+ templateRoot = path.resolve(
419
+ path.dirname(fileURLToPath(import.meta.url)),
420
+ "../../templates/expo-app",
421
+ ),
422
+ serverRoot = path.resolve(
423
+ path.dirname(fileURLToPath(import.meta.url)),
424
+ "../..",
425
+ ),
426
+ } = {},
427
+ ) {
428
+ const appRoot = path.resolve(
429
+ normalizeRequired(rawOptions.app, "--app"),
430
+ );
431
+ const certificatePath = path.resolve(
432
+ normalizeRequired(rawOptions.certificate, "--certificate"),
433
+ );
434
+ const channel = normalizeRequired(rawOptions.channel, "--channel")
435
+ .toLowerCase();
436
+ const runtimeVersion = normalizeRequired(
437
+ rawOptions.runtimeVersion,
438
+ "--runtime-version",
439
+ );
440
+ const platform = rawOptions.platform || "all";
441
+ const force = rawOptions.force === true;
442
+
443
+ if (!CHANNEL_PATTERN.test(channel)) {
444
+ throw new Error(
445
+ "--channel must start with a letter or number and contain only lowercase letters, numbers, dots, underscores or hyphens.",
446
+ );
447
+ }
448
+ if (runtimeVersion.length > 100) {
449
+ throw new Error("--runtime-version must contain at most 100 characters.");
450
+ }
451
+ if (!["all", "ios", "android"].includes(platform)) {
452
+ throw new Error("--platform must be all, ios or android.");
453
+ }
454
+
455
+ validateCertificate(certificatePath);
456
+ const manifestUrl = resolveManifestUrl(rawOptions.serverUrl);
457
+ const project = validateProject(appRoot);
458
+ const packageManager = detectPackageManager(appRoot);
459
+ const commands = getPackageCommands(packageManager, platform);
460
+ const certificateDestination = path.join(
461
+ appRoot,
462
+ "certificates",
463
+ "code-signing-certificate.pem",
464
+ );
465
+ const publisherSource = path.join(
466
+ templateRoot,
467
+ "scripts",
468
+ "ota-publish",
469
+ "index.mjs",
470
+ );
471
+ const publisherDestination = path.join(
472
+ appRoot,
473
+ "scripts",
474
+ "ota-publish",
475
+ "index.mjs",
476
+ );
477
+ const exportCommandSource = path.join(
478
+ templateRoot,
479
+ "scripts",
480
+ "ota-publish",
481
+ "lib",
482
+ "expo-export-command.mjs",
483
+ );
484
+ const exportCommandDestination = path.join(
485
+ appRoot,
486
+ "scripts",
487
+ "ota-publish",
488
+ "lib",
489
+ "expo-export-command.mjs",
490
+ );
491
+ const envExampleSource = path.join(templateRoot, ".env.ota.example");
492
+ const envExampleDestination = path.join(appRoot, ".env.ota.example");
493
+ const registrarSource = path.join(
494
+ templateRoot,
495
+ "scripts",
496
+ "ota-register-embedded",
497
+ "index.mjs",
498
+ );
499
+ const registrarDestination = path.join(
500
+ appRoot,
501
+ "scripts",
502
+ "ota-register-embedded",
503
+ "index.mjs",
504
+ );
505
+ const androidRegistrarSource = path.join(
506
+ templateRoot,
507
+ "scripts",
508
+ "ota-register-embedded",
509
+ "android-register-embedded-update.sh",
510
+ );
511
+ const androidRegistrarDestination = path.join(
512
+ appRoot,
513
+ "scripts",
514
+ "ota-register-embedded",
515
+ "android-register-embedded-update.sh",
516
+ );
517
+ const iosPluginSource = path.join(
518
+ templateRoot,
519
+ "plugins",
520
+ "with-ota-embedded-registration.js",
521
+ );
522
+ const iosPluginDestination = path.join(
523
+ appRoot,
524
+ "plugins",
525
+ "with-ota-embedded-registration.js",
526
+ );
527
+ const iosRegisterSource = path.join(
528
+ templateRoot,
529
+ "ci_scripts",
530
+ "register-embedded-update.sh",
531
+ );
532
+ const iosRegisterDestination = path.join(
533
+ appRoot,
534
+ "ci_scripts",
535
+ "register-embedded-update.sh",
536
+ );
537
+ const xcodeCloudHookSource = path.join(
538
+ templateRoot,
539
+ "ci_scripts",
540
+ "ci_post_xcodebuild.sh",
541
+ );
542
+ const xcodeCloudHookDestination = path.join(
543
+ appRoot,
544
+ "ci_scripts",
545
+ "ci_post_xcodebuild.sh",
546
+ );
547
+ const publisherEnvDestination = path.join(appRoot, ".env.ota");
548
+ const localPublisherEnv = getLocalDockerPublisherEnv({
549
+ channel,
550
+ serverRoot,
551
+ serverUrl: manifestUrl,
552
+ });
553
+ const publisherTemplateRoot = path.join(
554
+ templateRoot,
555
+ "scripts",
556
+ "ota-publish",
557
+ );
558
+ const publisherFiles = listFilesRecursive(publisherTemplateRoot).map(
559
+ (source) => ({
560
+ source,
561
+ destination: path.join(
562
+ appRoot,
563
+ "scripts",
564
+ "ota-publish",
565
+ path.relative(publisherTemplateRoot, source),
566
+ ),
567
+ }),
568
+ );
569
+
570
+ assertCopyAllowed(certificatePath, certificateDestination, force);
571
+ assertCopyAllowed(publisherSource, publisherDestination, force);
572
+ assertCopyAllowed(
573
+ exportCommandSource,
574
+ exportCommandDestination,
575
+ force,
576
+ );
577
+ assertCopyAllowed(envExampleSource, envExampleDestination, force);
578
+ for (const file of publisherFiles) {
579
+ assertCopyAllowed(file.source, file.destination, force);
580
+ }
581
+ assertCopyAllowed(registrarSource, registrarDestination, force);
582
+ assertCopyAllowed(
583
+ androidRegistrarSource,
584
+ androidRegistrarDestination,
585
+ force,
586
+ );
587
+ assertCopyAllowed(iosPluginSource, iosPluginDestination, force);
588
+ if (platform !== "android") {
589
+ assertCopyAllowed(iosRegisterSource, iosRegisterDestination, force);
590
+ assertCopyAllowed(
591
+ xcodeCloudHookSource,
592
+ xcodeCloudHookDestination,
593
+ force,
594
+ );
595
+ }
596
+ if (
597
+ localPublisherEnv &&
598
+ fs.existsSync(publisherEnvDestination) &&
599
+ !force &&
600
+ fs.readFileSync(publisherEnvDestination, "utf8") !== localPublisherEnv
601
+ ) {
602
+ throw new Error(
603
+ `Refusing to overwrite ${publisherEnvDestination}. Re-run with --force.`,
604
+ );
605
+ }
606
+
607
+ writeJson(
608
+ project.appJsonPath,
609
+ patchAppJson(project.appJson, {
610
+ channel,
611
+ runtimeVersion,
612
+ manifestUrl,
613
+ platform,
614
+ }),
615
+ );
616
+
617
+ copyFile(certificatePath, certificateDestination);
618
+ copyFile(publisherSource, publisherDestination);
619
+ copyFile(exportCommandSource, exportCommandDestination);
620
+ copyFile(envExampleSource, envExampleDestination);
621
+ for (const file of publisherFiles) {
622
+ copyFile(file.source, file.destination);
623
+ }
624
+ copyFile(registrarSource, registrarDestination);
625
+ copyFile(androidRegistrarSource, androidRegistrarDestination);
626
+ fs.chmodSync(androidRegistrarDestination, 0o755);
627
+ copyFile(iosPluginSource, iosPluginDestination);
628
+ patchPublisherEnvExample(envExampleDestination, channel);
629
+ if (localPublisherEnv) {
630
+ fs.writeFileSync(
631
+ publisherEnvDestination,
632
+ localPublisherEnv,
633
+ { encoding: "utf8", mode: 0o600 },
634
+ );
635
+ }
636
+ appendGitignore(appRoot);
637
+
638
+ commandRunner(...commands.installExpoUpdates, appRoot);
639
+
640
+ const installedPackageJson = readJson(project.packagePath);
641
+ writeJson(
642
+ project.packagePath,
643
+ patchPackageJson(installedPackageJson, platform),
644
+ );
645
+
646
+ commandRunner(...commands.installPublisherDependencies, appRoot);
647
+ stopAndroidGradleIfPresent(appRoot, platform, commandRunner);
648
+ commandRunner(...commands.prebuild, appRoot);
649
+ if (platform !== "android") {
650
+ copyFile(iosRegisterSource, iosRegisterDestination);
651
+ copyFile(xcodeCloudHookSource, xcodeCloudHookDestination);
652
+ fs.chmodSync(iosRegisterDestination, 0o755);
653
+ fs.chmodSync(xcodeCloudHookDestination, 0o755);
654
+ }
655
+
656
+ return {
657
+ appRoot,
658
+ certificatePath: certificateDestination,
659
+ manifestUrl,
660
+ channel,
661
+ runtimeVersion,
662
+ platform,
663
+ packageManager,
664
+ publisherEnvConfigured: Boolean(localPublisherEnv),
665
+ };
666
+ }