create-expo-updates-server 0.1.3 → 0.1.5

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 (84) hide show
  1. package/dist/template/.env.docker.example +1 -0
  2. package/dist/template/.env.example +2 -0
  3. package/dist/template/README.md +16 -8
  4. package/dist/template/app/api/admin/distribution-control/route.test.ts +153 -0
  5. package/dist/template/app/api/admin/distribution-control/route.ts +111 -0
  6. package/dist/template/app/api/admin/embedded-updates/route.test.ts +36 -1
  7. package/dist/template/app/api/admin/guard-actions/[id]/route.test.ts +60 -0
  8. package/dist/template/app/api/admin/guard-actions/[id]/route.ts +32 -0
  9. package/dist/template/app/api/admin/guard-actions/route.test.ts +123 -0
  10. package/dist/template/app/api/admin/guard-actions/route.ts +66 -0
  11. package/dist/template/app/api/admin/updates/[encodedKey]/deactivate/route.ts +4 -1
  12. package/dist/template/app/api/admin/updates/[encodedKey]/policy/route.test.ts +171 -0
  13. package/dist/template/app/api/admin/updates/[encodedKey]/policy/route.ts +75 -0
  14. package/dist/template/app/api/admin/updates/[encodedKey]/promote/route.ts +4 -1
  15. package/dist/template/app/api/admin/updates/[encodedKey]/rollback/route.ts +4 -1
  16. package/dist/template/app/api/admin/updates/[encodedKey]/route.ts +4 -1
  17. package/dist/template/app/api/admin/updates/route.test.ts +29 -1
  18. package/dist/template/app/updates/[encodedKey]/page.tsx +74 -6
  19. package/dist/template/app/updates/embedded/page.tsx +6 -2
  20. package/dist/template/app/updates/page.tsx +28 -3
  21. package/dist/template/docs/expo-app-setup.md +14 -3
  22. package/dist/template/docs/getting-started.md +13 -7
  23. package/dist/template/docs/getting-started.ru.md +15 -7
  24. package/dist/template/docs/migrations/2026-09-01-app-version-columns.sql +23 -0
  25. package/dist/template/docs/migrations/2026-09-01-ota-update-policy.sql +108 -0
  26. package/dist/template/docs/migrations/2026-09-01-p1-guard-actions.sql +19 -0
  27. package/dist/template/docs/migrations/2026-09-01-p2-simplify-ota-guard-policy.sql +69 -0
  28. package/dist/template/docs/migrations/2026-09-01-policy-publication-correction.sql +44 -0
  29. package/dist/template/docs/migrations/2026-09-02-ota-distribution-control.sql +377 -0
  30. package/dist/template/docs/migrations/schema.sql +561 -2
  31. package/dist/template/docs/operations-guide.md +52 -1
  32. package/dist/template/docs/ota-emergency-recovery.md +22 -0
  33. package/dist/template/docs/ota-update-policy.md +89 -0
  34. package/dist/template/docs/supabase-setup.md +2 -1
  35. package/dist/template/package-lock.json +2 -2
  36. package/dist/template/package.json +1 -1
  37. package/dist/template/scripts/configure-expo-app.mjs +6 -0
  38. package/dist/template/scripts/lib/embedded-update-registrar.test.ts +383 -3
  39. package/dist/template/scripts/lib/expo-app-configurator.mjs +165 -9
  40. package/dist/template/scripts/lib/expo-app-configurator.test.ts +658 -9
  41. package/dist/template/src/client/admin-api.ts +80 -1
  42. package/dist/template/src/client/admin-types.ts +52 -2
  43. package/dist/template/src/client/distribution-control.tsx +231 -0
  44. package/dist/template/src/client/guard-action-combobox.test.ts +26 -0
  45. package/dist/template/src/client/guard-action-combobox.tsx +267 -0
  46. package/dist/template/src/client/guard-action-options.test.ts +62 -0
  47. package/dist/template/src/client/guard-action-options.ts +70 -0
  48. package/dist/template/src/client/update-policy-editor.test.ts +36 -0
  49. package/dist/template/src/client/update-policy-editor.tsx +436 -0
  50. package/dist/template/src/server/controllers/assets.test.ts +87 -0
  51. package/dist/template/src/server/controllers/assets.ts +26 -0
  52. package/dist/template/src/server/controllers/manifest.test.ts +413 -2
  53. package/dist/template/src/server/controllers/manifest.ts +103 -38
  54. package/dist/template/src/server/lib/admin-distribution-guard.test.ts +61 -0
  55. package/dist/template/src/server/lib/admin-embedded-updates.test.ts +57 -0
  56. package/dist/template/src/server/lib/admin-embedded-updates.ts +42 -25
  57. package/dist/template/src/server/lib/admin-update-policy.test.ts +176 -0
  58. package/dist/template/src/server/lib/admin-updates.test.ts +133 -0
  59. package/dist/template/src/server/lib/admin-updates.ts +174 -9
  60. package/dist/template/src/server/lib/distribution-control-migration.test.ts +94 -0
  61. package/dist/template/src/server/lib/distribution-control.test.ts +160 -0
  62. package/dist/template/src/server/lib/distribution-control.ts +250 -0
  63. package/dist/template/src/server/lib/guard-actions-migration.test.ts +56 -0
  64. package/dist/template/src/server/lib/guard-actions.test.ts +142 -0
  65. package/dist/template/src/server/lib/guard-actions.ts +106 -0
  66. package/dist/template/src/server/lib/ota-policy-migrations.test.ts +64 -0
  67. package/dist/template/src/server/lib/simple-policy-migration.test.ts +97 -0
  68. package/dist/template/src/server/lib/supabase.js +95 -8
  69. package/dist/template/src/server/lib/supabase.test.ts +194 -0
  70. package/dist/template/src/server/lib/update-policy.test.ts +98 -0
  71. package/dist/template/src/server/lib/update-policy.ts +171 -0
  72. package/dist/template/src/server/manifest/manifest-repository.ts +10 -1
  73. package/dist/template/src/server/manifest/manifest-response.ts +0 -16
  74. package/dist/template/src/server/manifest/manifest-values.test.ts +54 -0
  75. package/dist/template/src/server/manifest/manifest-values.ts +145 -0
  76. package/dist/template/templates/expo-app/ci_scripts/ci_post_xcodebuild.sh +23 -1
  77. package/dist/template/templates/expo-app/plugins/with-ota-embedded-registration.js +13 -4
  78. package/dist/template/templates/expo-app/scripts/ota-register-embedded/index.mjs +53 -7
  79. package/dist/template/templates/expo-app/scripts/ota-register-embedded/register-ios.sh +87 -0
  80. package/dist/template/tsconfig.json +4 -1
  81. package/dist/template-manifest.json +283 -88
  82. package/package.json +1 -1
  83. package/dist/template/templates/expo-app/ci_scripts/register-embedded-update.sh +0 -36
  84. /package/dist/template/templates/expo-app/scripts/ota-register-embedded/{android-register-embedded-update.sh → register-android.sh} +0 -0
@@ -0,0 +1,171 @@
1
+ export const UPDATE_POLICY_SCHEMA_VERSION = 1 as const;
2
+
3
+ export type UpdateDeliveryMode = "manual" | "background";
4
+ export type UpdatePolicyGuard = {
5
+ action: string;
6
+ payload?: unknown;
7
+ };
8
+ export type UpdatePolicyInput = {
9
+ delivery: UpdateDeliveryMode;
10
+ guard: UpdatePolicyGuard | null;
11
+ };
12
+ export type ManifestUpdatePolicy = {
13
+ schemaVersion: 1;
14
+ policyVersion: number;
15
+ delivery: UpdateDeliveryMode;
16
+ guard?: UpdatePolicyGuard;
17
+ };
18
+
19
+ const MAX_ACTION_LENGTH = 100;
20
+ const MAX_PAYLOAD_BYTES = 16 * 1024;
21
+ const MAX_PAYLOAD_DEPTH = 10;
22
+ const CONTROL_CHARACTER_PATTERN = /[\u0000-\u001f\u007f-\u009f]/;
23
+
24
+ export class UpdatePolicyValidationError extends Error {
25
+ constructor(message: string) {
26
+ super(message);
27
+ this.name = "UpdatePolicyValidationError";
28
+ }
29
+ }
30
+
31
+ function fail(path: string, message: string): never {
32
+ throw new UpdatePolicyValidationError(`${path}: ${message}`);
33
+ }
34
+
35
+ function isObject(value: unknown): value is Record<string, unknown> {
36
+ return Boolean(value) && typeof value === "object" && !Array.isArray(value);
37
+ }
38
+
39
+ function validateJsonValue(
40
+ value: unknown,
41
+ path: string,
42
+ depth = 0,
43
+ ): void {
44
+ if (depth > MAX_PAYLOAD_DEPTH) {
45
+ fail(path, `JSON depth must not exceed ${MAX_PAYLOAD_DEPTH}`);
46
+ }
47
+ if (
48
+ value === null ||
49
+ typeof value === "string" ||
50
+ typeof value === "boolean"
51
+ ) {
52
+ return;
53
+ }
54
+ if (typeof value === "number") {
55
+ if (!Number.isFinite(value)) fail(path, "numbers must be finite");
56
+ return;
57
+ }
58
+ if (Array.isArray(value)) {
59
+ value.forEach((item, index) =>
60
+ validateJsonValue(item, `${path}[${index}]`, depth + 1),
61
+ );
62
+ return;
63
+ }
64
+ if (isObject(value)) {
65
+ Object.entries(value).forEach(([key, item]) =>
66
+ validateJsonValue(item, `${path}.${key}`, depth + 1),
67
+ );
68
+ return;
69
+ }
70
+ fail(path, "must be JSON-serializable");
71
+ }
72
+
73
+ function validatePayload(payload: unknown): void {
74
+ validateJsonValue(payload, "policy.guard.payload");
75
+ let serialized: string;
76
+ try {
77
+ serialized = JSON.stringify(payload);
78
+ } catch {
79
+ fail("policy.guard.payload", "must be JSON-serializable");
80
+ }
81
+ if (Buffer.byteLength(serialized, "utf8") > MAX_PAYLOAD_BYTES) {
82
+ fail(
83
+ "policy.guard.payload",
84
+ `must not exceed ${MAX_PAYLOAD_BYTES} UTF-8 bytes`,
85
+ );
86
+ }
87
+ }
88
+
89
+ export function validateUpdatePolicy(value: unknown): UpdatePolicyInput {
90
+ if (!isObject(value)) fail("policy", "must be an object");
91
+ if (value.delivery !== "manual" && value.delivery !== "background") {
92
+ fail("policy.delivery", "must be manual or background");
93
+ }
94
+
95
+ if (value.guard === null) {
96
+ return { delivery: value.delivery, guard: null };
97
+ }
98
+ if (!isObject(value.guard)) {
99
+ fail("policy.guard", "must be an object or null");
100
+ }
101
+
102
+ const action = value.guard.action;
103
+ if (typeof action !== "string") {
104
+ fail("policy.guard.action", "must be a string");
105
+ }
106
+ if (!action.trim()) {
107
+ if (value.guard.payload !== undefined && value.guard.payload !== null) {
108
+ fail("policy.guard.payload", "must be absent when action is empty");
109
+ }
110
+ return { delivery: value.delivery, guard: null };
111
+ }
112
+ if (
113
+ action !== action.trim() ||
114
+ Array.from(action).length > MAX_ACTION_LENGTH ||
115
+ CONTROL_CHARACTER_PATTERN.test(action)
116
+ ) {
117
+ fail(
118
+ "policy.guard.action",
119
+ "must be trimmed, contain 1-100 characters, and have no control characters",
120
+ );
121
+ }
122
+
123
+ if (value.guard.payload !== undefined && value.guard.payload !== null) {
124
+ validatePayload(value.guard.payload);
125
+ }
126
+
127
+ return {
128
+ delivery: value.delivery,
129
+ guard: {
130
+ action,
131
+ ...(value.guard.payload !== undefined && value.guard.payload !== null
132
+ ? { payload: value.guard.payload }
133
+ : {}),
134
+ },
135
+ };
136
+ }
137
+
138
+ export function createManifestUpdatePolicy({
139
+ delivery,
140
+ guard,
141
+ policyVersion,
142
+ onCorrupt,
143
+ }: {
144
+ delivery: unknown;
145
+ guard: unknown;
146
+ policyVersion: unknown;
147
+ onCorrupt?: (error: Error) => void;
148
+ }): ManifestUpdatePolicy {
149
+ try {
150
+ const validated = validateUpdatePolicy({ delivery, guard });
151
+ if (!Number.isInteger(policyVersion) || (policyVersion as number) < 1) {
152
+ fail("policy.policyVersion", "must be a positive integer");
153
+ }
154
+ return {
155
+ schemaVersion: UPDATE_POLICY_SCHEMA_VERSION,
156
+ policyVersion: policyVersion as number,
157
+ delivery: validated.delivery,
158
+ ...(validated.guard ? { guard: validated.guard } : {}),
159
+ };
160
+ } catch (error: unknown) {
161
+ onCorrupt?.(error as Error);
162
+ return {
163
+ schemaVersion: UPDATE_POLICY_SCHEMA_VERSION,
164
+ policyVersion:
165
+ Number.isInteger(policyVersion) && (policyVersion as number) > 0
166
+ ? (policyVersion as number)
167
+ : 1,
168
+ delivery: "manual",
169
+ };
170
+ }
171
+ }
@@ -1,4 +1,6 @@
1
1
  import { supabase } from "../lib/supabase.js";
2
+ import { UpdateDeliveryMode } from "../lib/update-policy";
3
+ import { getManifestAppVersion } from "./manifest-values";
2
4
 
3
5
  export type OtaUpdateRow = {
4
6
  update_id: string;
@@ -10,6 +12,11 @@ export type OtaUpdateRow = {
10
12
  created_at_path: string;
11
13
  storage_base_path: string;
12
14
  is_active: boolean;
15
+ delivery_mode: UpdateDeliveryMode;
16
+ guard_action: string | null;
17
+ guard_payload: unknown | null;
18
+ policy_version: number;
19
+ policy_published_at: string | Date | null;
13
20
  manifest: Record<string, unknown>;
14
21
  };
15
22
 
@@ -26,6 +33,7 @@ export type EmbeddedUpdateMeta = {
26
33
  created_at: string | Date;
27
34
  channel: string;
28
35
  platform: "ios" | "android";
36
+ app_version: string | null;
29
37
  };
30
38
 
31
39
  let hasServedManifestLogTable: boolean | null = null;
@@ -193,7 +201,7 @@ async function getEmbeddedUpdateMetaById(
193
201
  ): Promise<EmbeddedUpdateMeta | null> {
194
202
  const { data, error } = await supabase
195
203
  .from("ota_embedded_updates")
196
- .select("embedded_update_id,created_at,channel,platform")
204
+ .select("embedded_update_id,created_at,channel,platform,app_version")
197
205
  .eq("embedded_update_id", embeddedUpdateId)
198
206
  .maybeSingle();
199
207
 
@@ -227,6 +235,7 @@ function toEmbeddedUpdateMeta(updateRow: OtaUpdateRow): EmbeddedUpdateMeta {
227
235
  created_at: updateRow.created_at,
228
236
  channel: updateRow.channel,
229
237
  platform: updateRow.platform,
238
+ app_version: getManifestAppVersion(updateRow.manifest),
230
239
  };
231
240
  }
232
241
 
@@ -1,5 +1,4 @@
1
1
  import NodeFormData from "form-data";
2
- import path from "node:path";
3
2
  import { NextRequest } from "next/server";
4
3
  import { toPosixPath } from "../lib/manifest-helpers.js";
5
4
  import { createSignatureHeaderIfRequested } from "../lib/signing.js";
@@ -61,11 +60,6 @@ function getServerBaseUrl(request: NextRequest): string {
61
60
  return `${protocol}://${host}`;
62
61
  }
63
62
 
64
- function getCdnBaseUrl(): string | null {
65
- const value = process.env.OTA_CDN_BASE_URL?.trim();
66
- return value ? removeTrailingSlash(value) : null;
67
- }
68
-
69
63
  export function buildAssetUrl({
70
64
  request,
71
65
  runtimeVersion,
@@ -74,7 +68,6 @@ export function buildAssetUrl({
74
68
  channel,
75
69
  updateId,
76
70
  assetPath,
77
- storageBasePath,
78
71
  }: {
79
72
  request: NextRequest;
80
73
  runtimeVersion: string;
@@ -83,16 +76,7 @@ export function buildAssetUrl({
83
76
  channel: string;
84
77
  updateId: string;
85
78
  assetPath: string;
86
- storageBasePath: string;
87
79
  }): string {
88
- const cdnBaseUrl = getCdnBaseUrl();
89
- if (cdnBaseUrl && storageBasePath) {
90
- const objectPath = toPosixPath(
91
- path.posix.join(storageBasePath, toPosixPath(assetPath)),
92
- );
93
- return `${cdnBaseUrl}/${objectPath}`;
94
- }
95
-
96
80
  const params = new URLSearchParams({
97
81
  runtimeVersion,
98
82
  platform,
@@ -0,0 +1,54 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import {
3
+ compareAppVersions,
4
+ getManifestAppVersion,
5
+ } from "./manifest-values";
6
+
7
+ describe("compareAppVersions", () => {
8
+ it.each([
9
+ ["1.7.11", "1.7.10", 1],
10
+ ["1.10", "1.9", 1],
11
+ ["1.0.0", "1", 0],
12
+ ["1.0.0-beta.2", "1.0.0-beta.11", -1],
13
+ ["1.0.0-beta", "1.0.0", -1],
14
+ ["1.0.0", "1.0.0-rc.1", 1],
15
+ ["1.0.0+build.2", "1.0.0+build.1", 0],
16
+ ])("compares %s with %s", (left, right, expected) => {
17
+ expect(compareAppVersions(left, right)).toBe(expected);
18
+ });
19
+
20
+ it.each([
21
+ ["", "1.0.0"],
22
+ ["version 2", "1.0.0"],
23
+ ["1..2", "1.0.0"],
24
+ ["1.0.0_beta", "1.0.0"],
25
+ ["1.02.0", "1.2.0"],
26
+ ["1.0.0-beta.01", "1.0.0-beta.1"],
27
+ [null, "1.0.0"],
28
+ ])("returns indeterminate for %s and %s", (left, right) => {
29
+ expect(compareAppVersions(left, right)).toBeNull();
30
+ });
31
+ });
32
+
33
+ describe("getManifestAppVersion", () => {
34
+ it("reads the root expoClient version", () => {
35
+ expect(
36
+ getManifestAppVersion({ expoClient: { version: " 1.7.11 " } }),
37
+ ).toBe("1.7.11");
38
+ });
39
+
40
+ it("reads the extra expoClient version", () => {
41
+ expect(
42
+ getManifestAppVersion({ extra: { expoClient: { version: "1.7.10" } } }),
43
+ ).toBe("1.7.10");
44
+ });
45
+
46
+ it("prefers the extra expoClient version when both are present", () => {
47
+ expect(
48
+ getManifestAppVersion({
49
+ expoClient: { version: "2.0.0" },
50
+ extra: { expoClient: { version: "1.0.0" } },
51
+ }),
52
+ ).toBe("1.0.0");
53
+ });
54
+ });
@@ -13,6 +13,151 @@ export function debugString(value: string | null | undefined): string {
13
13
  return normalizeId(value) || "none";
14
14
  }
15
15
 
16
+ export function getManifestAppVersion(manifest: unknown): string | null {
17
+ if (!manifest || typeof manifest !== "object" || Array.isArray(manifest)) {
18
+ return null;
19
+ }
20
+
21
+ const manifestRecord = manifest as Record<string, unknown>;
22
+ const extra =
23
+ manifestRecord.extra &&
24
+ typeof manifestRecord.extra === "object" &&
25
+ !Array.isArray(manifestRecord.extra)
26
+ ? (manifestRecord.extra as Record<string, unknown>)
27
+ : null;
28
+ for (const expoClient of [extra?.expoClient, manifestRecord.expoClient]) {
29
+ if (
30
+ expoClient &&
31
+ typeof expoClient === "object" &&
32
+ !Array.isArray(expoClient)
33
+ ) {
34
+ const version = (expoClient as Record<string, unknown>).version;
35
+ if (typeof version === "string" && version.trim()) {
36
+ return version.trim();
37
+ }
38
+ }
39
+ }
40
+
41
+ return null;
42
+ }
43
+
44
+ type ParsedAppVersion = {
45
+ core: string[];
46
+ prerelease: string[] | null;
47
+ };
48
+
49
+ function parseAppVersion(value: string): ParsedAppVersion | null {
50
+ const normalized = value.trim();
51
+ const match =
52
+ /^(0|[1-9]\d*(?:\.(?:0|[1-9]\d*))*)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/.exec(
53
+ normalized,
54
+ );
55
+ if (!match) {
56
+ return null;
57
+ }
58
+
59
+ const prerelease = match[2]?.split(".") ?? null;
60
+ if (
61
+ prerelease?.some(
62
+ (identifier) =>
63
+ /^\d+$/.test(identifier) &&
64
+ identifier.length > 1 &&
65
+ identifier.startsWith("0"),
66
+ )
67
+ ) {
68
+ return null;
69
+ }
70
+
71
+ return {
72
+ core: match[1].split("."),
73
+ prerelease,
74
+ };
75
+ }
76
+
77
+ function compareNumericIdentifiers(left: string, right: string): -1 | 0 | 1 {
78
+ if (left.length !== right.length) {
79
+ return left.length < right.length ? -1 : 1;
80
+ }
81
+ if (left === right) {
82
+ return 0;
83
+ }
84
+ return left < right ? -1 : 1;
85
+ }
86
+
87
+ /**
88
+ * Compares semver-style numeric app versions without coercing numeric
89
+ * identifiers to JavaScript numbers. Build metadata is ignored.
90
+ *
91
+ * Returns null when either value is malformed and cannot be compared safely.
92
+ */
93
+ export function compareAppVersions(
94
+ left: string | null | undefined,
95
+ right: string | null | undefined,
96
+ ): -1 | 0 | 1 | null {
97
+ if (typeof left !== "string" || typeof right !== "string") {
98
+ return null;
99
+ }
100
+
101
+ const parsedLeft = parseAppVersion(left);
102
+ const parsedRight = parseAppVersion(right);
103
+ if (!parsedLeft || !parsedRight) {
104
+ return null;
105
+ }
106
+
107
+ const coreLength = Math.max(parsedLeft.core.length, parsedRight.core.length);
108
+ for (let index = 0; index < coreLength; index += 1) {
109
+ const comparison = compareNumericIdentifiers(
110
+ parsedLeft.core[index] ?? "0",
111
+ parsedRight.core[index] ?? "0",
112
+ );
113
+ if (comparison !== 0) {
114
+ return comparison;
115
+ }
116
+ }
117
+
118
+ const leftPrerelease = parsedLeft.prerelease;
119
+ const rightPrerelease = parsedRight.prerelease;
120
+ if (!leftPrerelease && !rightPrerelease) {
121
+ return 0;
122
+ }
123
+ if (!leftPrerelease) {
124
+ return 1;
125
+ }
126
+ if (!rightPrerelease) {
127
+ return -1;
128
+ }
129
+
130
+ const prereleaseLength = Math.max(
131
+ leftPrerelease.length,
132
+ rightPrerelease.length,
133
+ );
134
+ for (let index = 0; index < prereleaseLength; index += 1) {
135
+ const leftIdentifier = leftPrerelease[index];
136
+ const rightIdentifier = rightPrerelease[index];
137
+ if (leftIdentifier === undefined) {
138
+ return -1;
139
+ }
140
+ if (rightIdentifier === undefined) {
141
+ return 1;
142
+ }
143
+ if (leftIdentifier === rightIdentifier) {
144
+ continue;
145
+ }
146
+
147
+ const leftIsNumeric = /^\d+$/.test(leftIdentifier);
148
+ const rightIsNumeric = /^\d+$/.test(rightIdentifier);
149
+ if (leftIsNumeric && rightIsNumeric) {
150
+ return compareNumericIdentifiers(leftIdentifier, rightIdentifier);
151
+ }
152
+ if (leftIsNumeric !== rightIsNumeric) {
153
+ return leftIsNumeric ? -1 : 1;
154
+ }
155
+ return leftIdentifier > rightIdentifier ? 1 : -1;
156
+ }
157
+
158
+ return 0;
159
+ }
160
+
16
161
  export function normalizeExpoManifestDate(
17
162
  value: string | Date | null | undefined,
18
163
  ): string | null {
@@ -2,5 +2,27 @@
2
2
 
3
3
  set -eu
4
4
 
5
+ if [ -n "${CI_PRIMARY_REPOSITORY_PATH:-}" ]; then
6
+ if ! PROJECT_ROOT="$(CDPATH= cd -- "$CI_PRIMARY_REPOSITORY_PATH" && pwd)"; then
7
+ echo "Embedded update registration failed: CI_PRIMARY_REPOSITORY_PATH is unavailable: $CI_PRIMARY_REPOSITORY_PATH" >&2
8
+ exit 1
9
+ fi
10
+ else
11
+ SCRIPT_DIR="$(CDPATH= cd -- "$(dirname "$0")" && pwd)" || {
12
+ echo "Embedded update registration failed: unable to resolve the Xcode Cloud script directory." >&2
13
+ exit 1
14
+ }
15
+ PROJECT_ROOT="$(CDPATH= cd -- "$SCRIPT_DIR/.." && pwd)" || {
16
+ echo "Embedded update registration failed: unable to resolve the Expo project root." >&2
17
+ exit 1
18
+ }
19
+ fi
20
+
21
+ REGISTER_SCRIPT="$PROJECT_ROOT/scripts/ota-register-embedded/register-ios.sh"
22
+ if [ ! -f "$REGISTER_SCRIPT" ]; then
23
+ echo "Embedded update registration failed: iOS registrar was not found at $REGISTER_SCRIPT." >&2
24
+ exit 1
25
+ fi
26
+
5
27
  export OTA_EMBEDDED_REGISTER_STRICT="${OTA_EMBEDDED_REGISTER_STRICT:-true}"
6
- sh "${CI_PRIMARY_REPOSITORY_PATH}/ci_scripts/register-embedded-update.sh"
28
+ exec sh "$REGISTER_SCRIPT"
@@ -1,20 +1,30 @@
1
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
1
2
  const { withXcodeProject } = require("expo/config-plugins");
2
3
 
3
4
  const PHASE_NAME = "Register Expo embedded update";
5
+ const SHELL_SCRIPT =
6
+ 'OTA_EMBEDDED_REGISTER_STRICT=true sh "$PROJECT_DIR/../scripts/ota-register-embedded/register-ios.sh"';
7
+ const SERIALIZED_SHELL_SCRIPT = `"${SHELL_SCRIPT.replaceAll(
8
+ "\\",
9
+ "\\\\",
10
+ ).replaceAll('"', '\\"')}"`;
4
11
 
5
12
  module.exports = function withOtaEmbeddedRegistration(config) {
6
13
  return withXcodeProject(config, (projectConfig) => {
7
14
  const project = projectConfig.modResults;
8
15
  const phases =
9
16
  project.hash.project.objects.PBXShellScriptBuildPhase || {};
10
- const phaseExists = Object.values(phases).some(
17
+ const existingPhase = Object.values(phases).find(
11
18
  (phase) =>
12
19
  phase &&
13
20
  typeof phase === "object" &&
14
21
  phase.name === `"${PHASE_NAME}"`,
15
22
  );
16
23
 
17
- if (!phaseExists) {
24
+ if (existingPhase) {
25
+ existingPhase.shellPath = "/bin/sh";
26
+ existingPhase.shellScript = SERIALIZED_SHELL_SCRIPT;
27
+ } else {
18
28
  project.addBuildPhase(
19
29
  [],
20
30
  "PBXShellScriptBuildPhase",
@@ -22,8 +32,7 @@ module.exports = function withOtaEmbeddedRegistration(config) {
22
32
  project.getFirstTarget().uuid,
23
33
  {
24
34
  shellPath: "/bin/sh",
25
- shellScript:
26
- 'OTA_EMBEDDED_REGISTER_STRICT=true sh "$PROJECT_DIR/../ci_scripts/register-embedded-update.sh"',
35
+ shellScript: SHELL_SCRIPT,
27
36
  },
28
37
  );
29
38
  }
@@ -108,6 +108,17 @@ export function parseEmbeddedManifest(content, fallbackDate = new Date()) {
108
108
  ["metadata", "createdAt"],
109
109
  ["extra", "expoClient", "createdAt"],
110
110
  ]);
111
+ const appVersion =
112
+ readPath(manifest, [
113
+ ["extra", "expoClient", "version"],
114
+ ["expoClient", "version"],
115
+ ["version"],
116
+ ]) ||
117
+ readPath(root, [
118
+ ["extra", "expoClient", "version"],
119
+ ["expoClient", "version"],
120
+ ["version"],
121
+ ]);
111
122
  const fallbackId = crypto
112
123
  .createHash("sha256")
113
124
  .update(content)
@@ -118,10 +129,38 @@ export function parseEmbeddedManifest(content, fallbackDate = new Date()) {
118
129
  embeddedUpdateId:
119
130
  embeddedUpdateId ||
120
131
  `${fallbackId.slice(0, 8)}-${fallbackId.slice(8, 12)}-4${fallbackId.slice(13, 16)}-a${fallbackId.slice(17, 20)}-${fallbackId.slice(20)}`,
132
+ appVersion: typeof appVersion === "string" ? appVersion : null,
121
133
  createdAt: normalizeDate(createdAt, fallbackDate),
122
134
  };
123
135
  }
124
136
 
137
+ export function readExpoAppVersion(appRoot, readFile = fs.readFileSync) {
138
+ try {
139
+ const appJson = JSON.parse(
140
+ readFile(path.join(appRoot, "app.json"), "utf8"),
141
+ );
142
+ const appVersion = appJson?.expo?.version;
143
+ return typeof appVersion === "string" && appVersion.trim()
144
+ ? appVersion.trim()
145
+ : null;
146
+ } catch {
147
+ return null;
148
+ }
149
+ }
150
+
151
+ export function parseEmbeddedManifestForProject(
152
+ content,
153
+ appRoot,
154
+ fallbackDate = new Date(),
155
+ readFile = fs.readFileSync,
156
+ ) {
157
+ const fields = parseEmbeddedManifest(content, fallbackDate);
158
+ return {
159
+ ...fields,
160
+ appVersion: fields.appVersion || readExpoAppVersion(appRoot, readFile),
161
+ };
162
+ }
163
+
125
164
  function parseArgs(argv) {
126
165
  const args = {};
127
166
  for (let index = 0; index < argv.length; index += 1) {
@@ -135,16 +174,17 @@ function parseArgs(argv) {
135
174
  }
136
175
 
137
176
  function readChannel(appRoot, explicitChannel) {
138
- if (explicitChannel) {
139
- return explicitChannel.trim().toLowerCase();
177
+ const configuredChannel =
178
+ explicitChannel || process.env.EXPO_UPDATE_CHANNEL;
179
+ if (configuredChannel) {
180
+ return configuredChannel.trim().toLowerCase();
140
181
  }
141
182
 
142
183
  const appJson = JSON.parse(
143
184
  fs.readFileSync(path.join(appRoot, "app.json"), "utf8"),
144
185
  );
145
186
  return String(
146
- process.env.EXPO_UPDATE_CHANNEL ||
147
- appJson.expo?.updates?.requestHeaders?.["expo-channel-name"] ||
187
+ appJson.expo?.updates?.requestHeaders?.["expo-channel-name"] ||
148
188
  appJson.expo?.extra?.updateChannel ||
149
189
  "production",
150
190
  )
@@ -210,9 +250,13 @@ async function registerWithDatabase(input) {
210
250
  try {
211
251
  await client.query(
212
252
  `INSERT INTO ota_embedded_updates (
213
- embedded_update_id, created_at, channel, platform, is_embedded
214
- ) VALUES ($1, $2, $3, $4, true)
253
+ embedded_update_id, app_version, created_at, channel, platform, is_embedded
254
+ ) VALUES ($1, $2, $3, $4, $5, true)
215
255
  ON CONFLICT (embedded_update_id) DO UPDATE SET
256
+ app_version = COALESCE(
257
+ NULLIF(BTRIM(EXCLUDED.app_version), ''),
258
+ ota_embedded_updates.app_version
259
+ ),
216
260
  created_at = EXCLUDED.created_at,
217
261
  channel = EXCLUDED.channel,
218
262
  platform = EXCLUDED.platform,
@@ -220,6 +264,7 @@ async function registerWithDatabase(input) {
220
264
  modified_at = now()`,
221
265
  [
222
266
  input.embeddedUpdateId,
267
+ input.appVersion || null,
223
268
  input.createdAt,
224
269
  input.channel,
225
270
  input.platform,
@@ -251,8 +296,9 @@ async function main() {
251
296
 
252
297
  const manifestPath = path.resolve(args.manifest);
253
298
  const channel = readChannel(ROOT_DIR, args.channel);
254
- const fields = parseEmbeddedManifest(
299
+ const fields = parseEmbeddedManifestForProject(
255
300
  fs.readFileSync(manifestPath, "utf8"),
301
+ ROOT_DIR,
256
302
  fs.statSync(manifestPath).mtime,
257
303
  );
258
304
  const input = {