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
package/lib/cli.mjs ADDED
@@ -0,0 +1,549 @@
1
+ import { createHash, randomUUID } from "node:crypto";
2
+ import { constants } from "node:fs";
3
+ import {
4
+ chmod,
5
+ copyFile,
6
+ lstat,
7
+ mkdir,
8
+ open,
9
+ readFile,
10
+ readdir,
11
+ rename,
12
+ } from "node:fs/promises";
13
+ import path from "node:path";
14
+ import { fileURLToPath } from "node:url";
15
+
16
+ const defaultPackageRoot = path.dirname(
17
+ path.dirname(fileURLToPath(import.meta.url)),
18
+ );
19
+ const command = "create-expo-updates-server";
20
+ const maximumTemplateFileSize = 10 * 1024 * 1024;
21
+ const maximumTemplateSize = 50 * 1024 * 1024;
22
+ const maximumManifestSize = 5 * 1024 * 1024;
23
+
24
+ const help = `Create a standalone Expo Updates Server project.
25
+
26
+ Usage:
27
+ ${command} <directory>
28
+ ${command} --help
29
+ ${command} --version
30
+
31
+ Options:
32
+ -h, --help Show this help
33
+ -v, --version Show the package version
34
+
35
+ The target path must not already exist.
36
+ `;
37
+
38
+ function cliError(message) {
39
+ const error = new Error(message);
40
+ error.isCliError = true;
41
+ return error;
42
+ }
43
+
44
+ function validateManifestPath(file) {
45
+ if (
46
+ typeof file !== "string" ||
47
+ file.length === 0 ||
48
+ file.includes("\0") ||
49
+ file.includes("\\") ||
50
+ path.posix.isAbsolute(file) ||
51
+ path.posix.normalize(file) !== file ||
52
+ file === ".." ||
53
+ file.startsWith("../") ||
54
+ file.split("/").includes("..")
55
+ ) {
56
+ throw cliError(`The bundled template contains an unsafe path: ${file}`);
57
+ }
58
+ }
59
+
60
+ function canonicalPath(file) {
61
+ return file.normalize("NFKC").toLowerCase();
62
+ }
63
+
64
+ async function listTree(root, relative = "") {
65
+ const directory = path.join(root, ...relative.split("/").filter(Boolean));
66
+ const entries = await readdir(directory, { withFileTypes: true });
67
+ const result = [];
68
+
69
+ for (const entry of entries) {
70
+ const child = relative ? `${relative}/${entry.name}` : entry.name;
71
+ validateManifestPath(child);
72
+ if (entry.isSymbolicLink()) {
73
+ throw cliError(`The bundled template contains a symlink: ${child}`);
74
+ }
75
+ if (entry.isDirectory()) {
76
+ result.push({ path: child, type: "directory" });
77
+ result.push(...(await listTree(root, child)));
78
+ } else if (entry.isFile()) {
79
+ result.push({ path: child, type: "file" });
80
+ } else {
81
+ throw cliError(`The bundled template contains a non-file: ${child}`);
82
+ }
83
+ }
84
+ return result;
85
+ }
86
+
87
+ export async function verifyTemplate(packageRoot = defaultPackageRoot) {
88
+ const packageJson = JSON.parse(
89
+ await readFile(path.join(packageRoot, "package.json"), "utf8"),
90
+ );
91
+ const manifestPath = path.join(
92
+ packageRoot,
93
+ "dist",
94
+ "template-manifest.json",
95
+ );
96
+ const manifestHandle = await open(
97
+ manifestPath,
98
+ constants.O_RDONLY | constants.O_NOFOLLOW,
99
+ );
100
+ let manifestBytes;
101
+ try {
102
+ const manifestStat = await manifestHandle.stat();
103
+ if (
104
+ !manifestStat.isFile() ||
105
+ manifestStat.size > maximumManifestSize
106
+ ) {
107
+ throw cliError("The bundled template manifest is too large");
108
+ }
109
+ manifestBytes = await manifestHandle.readFile();
110
+ } finally {
111
+ await manifestHandle.close();
112
+ }
113
+ const manifest = JSON.parse(manifestBytes.toString("utf8"));
114
+ const templateRoot = path.join(packageRoot, "dist", "template");
115
+
116
+ if (
117
+ manifest.schemaVersion !== 1 ||
118
+ manifest.installerVersion !== packageJson.version ||
119
+ !/^[0-9a-f]{40}$/.test(manifest.sourceCommit) ||
120
+ !Number.isSafeInteger(manifest.totalSize) ||
121
+ manifest.totalSize < 0 ||
122
+ manifest.totalSize > maximumTemplateSize ||
123
+ !Array.isArray(manifest.files)
124
+ ) {
125
+ throw cliError("The bundled template manifest is invalid");
126
+ }
127
+
128
+ const actualFiles = (await listTree(templateRoot))
129
+ .filter(({ type }) => type === "file")
130
+ .map(({ path: file }) => file)
131
+ .sort((a, b) => a.localeCompare(b, "en"));
132
+ const manifestFiles = [];
133
+ const canonical = new Map();
134
+ let previous = "";
135
+ let totalSize = 0;
136
+
137
+ for (const entry of manifest.files) {
138
+ validateManifestPath(entry?.path);
139
+ if (entry.bundledPath !== undefined) {
140
+ validateManifestPath(entry.bundledPath);
141
+ }
142
+ if (
143
+ !Number.isSafeInteger(entry.size) ||
144
+ entry.size < 0 ||
145
+ entry.size > maximumTemplateFileSize ||
146
+ !/^[0-9a-f]{64}$/.test(entry.sha256)
147
+ ) {
148
+ throw cliError(`Invalid manifest entry for ${entry.path}`);
149
+ }
150
+ if (previous && previous.localeCompare(entry.path, "en") >= 0) {
151
+ throw cliError("The bundled template manifest is not sorted");
152
+ }
153
+ previous = entry.path;
154
+ totalSize += entry.size;
155
+ if (
156
+ !Number.isSafeInteger(totalSize) ||
157
+ totalSize > maximumTemplateSize
158
+ ) {
159
+ throw cliError("The bundled template exceeds its total size limit");
160
+ }
161
+
162
+ const segments = entry.path.split("/");
163
+ for (let index = 1; index <= segments.length; index += 1) {
164
+ const partial = segments.slice(0, index).join("/");
165
+ const key = canonicalPath(partial);
166
+ const collision = canonical.get(key);
167
+ if (collision && collision !== partial) {
168
+ throw cliError(
169
+ `The bundled template has case-colliding paths: ${collision} and ${partial}`,
170
+ );
171
+ }
172
+ canonical.set(key, partial);
173
+ }
174
+
175
+ const bundledPath = entry.bundledPath ?? entry.path;
176
+ const source = path.join(templateRoot, ...bundledPath.split("/"));
177
+ const handle = await open(
178
+ source,
179
+ constants.O_RDONLY | constants.O_NOFOLLOW,
180
+ );
181
+ try {
182
+ const sourceStat = await handle.stat();
183
+ if (!sourceStat.isFile()) {
184
+ throw cliError(
185
+ `Bundled template entry is not a regular file: ${entry.path}`,
186
+ );
187
+ }
188
+ if (sourceStat.size !== entry.size) {
189
+ throw cliError(`Bundled template size mismatch: ${entry.path}`);
190
+ }
191
+ const content = await handle.readFile();
192
+ const digest = createHash("sha256").update(content).digest("hex");
193
+ const sourcePathStat = await lstat(source);
194
+ if (
195
+ sourcePathStat.dev !== sourceStat.dev ||
196
+ sourcePathStat.ino !== sourceStat.ino ||
197
+ content.byteLength !== entry.size ||
198
+ digest !== entry.sha256
199
+ ) {
200
+ throw cliError(`Bundled template checksum mismatch: ${entry.path}`);
201
+ }
202
+ } finally {
203
+ await handle.close();
204
+ }
205
+ manifestFiles.push(bundledPath);
206
+ }
207
+
208
+ if (totalSize !== manifest.totalSize) {
209
+ throw cliError("The bundled template total size is invalid");
210
+ }
211
+
212
+ manifestFiles.sort((a, b) => a.localeCompare(b, "en"));
213
+ if (
214
+ actualFiles.length !== manifestFiles.length ||
215
+ actualFiles.some((file, index) => file !== manifestFiles[index])
216
+ ) {
217
+ throw cliError("Bundled template files do not match the manifest");
218
+ }
219
+
220
+ return { manifest, templateRoot };
221
+ }
222
+
223
+ async function copyToStage(templateRoot, manifest, staging) {
224
+ const directories = new Set();
225
+ for (const entry of manifest.files) {
226
+ const file = entry.path;
227
+ const segments = file.split("/");
228
+ for (let index = 1; index < segments.length; index += 1) {
229
+ directories.add(segments.slice(0, index).join("/"));
230
+ }
231
+ }
232
+
233
+ for (const directory of [...directories].sort((left, right) => {
234
+ const depth = left.split("/").length - right.split("/").length;
235
+ return depth || left.localeCompare(right, "en");
236
+ })) {
237
+ const destination = path.join(staging, ...directory.split("/"));
238
+ await mkdir(destination);
239
+ }
240
+
241
+ for (const entry of manifest.files) {
242
+ const file = entry.path;
243
+ const source = path.join(
244
+ templateRoot,
245
+ ...(entry.bundledPath ?? file).split("/"),
246
+ );
247
+ const destination = path.join(staging, ...file.split("/"));
248
+ await copyFile(source, destination);
249
+ const sourceStat = await lstat(source);
250
+ await chmod(destination, sourceStat.mode & 0o777);
251
+ }
252
+ }
253
+
254
+ async function assertTargetAbsent(target) {
255
+ try {
256
+ await lstat(target);
257
+ } catch (error) {
258
+ if (error?.code === "ENOENT") return;
259
+ throw error;
260
+ }
261
+ throw cliError(`Target path already exists: ${target}`);
262
+ }
263
+
264
+ function expectedDirectories(manifest) {
265
+ return new Set(
266
+ manifest.files.flatMap(({ path: file }) => {
267
+ const segments = file.split("/");
268
+ return segments
269
+ .slice(0, -1)
270
+ .map((_, index) => segments.slice(0, index + 1).join("/"));
271
+ }),
272
+ );
273
+ }
274
+
275
+ export async function verifyStagedProject(target, manifest) {
276
+ const expectedFiles = new Map(
277
+ manifest.files.map((entry) => [entry.path, entry]),
278
+ );
279
+ const directories = expectedDirectories(manifest);
280
+ const assertPathSet = (actual) => {
281
+ const expectedCount = expectedFiles.size + directories.size;
282
+ if (actual.length !== expectedCount) {
283
+ throw cliError("Generated project paths do not match the manifest");
284
+ }
285
+ for (const entry of actual) {
286
+ if (
287
+ (entry.type === "directory" && !directories.has(entry.path)) ||
288
+ (entry.type === "file" && !expectedFiles.has(entry.path))
289
+ ) {
290
+ throw cliError(`Unexpected generated project path: ${entry.path}`);
291
+ }
292
+ }
293
+ };
294
+
295
+ const actual = await listTree(target);
296
+ assertPathSet(actual);
297
+
298
+ for (const entry of actual) {
299
+ if (entry.type === "directory") {
300
+ continue;
301
+ }
302
+
303
+ const expected = expectedFiles.get(entry.path);
304
+ const location = path.join(target, ...entry.path.split("/"));
305
+ const handle = await open(
306
+ location,
307
+ constants.O_RDONLY | constants.O_NOFOLLOW,
308
+ );
309
+ try {
310
+ const before = await handle.stat();
311
+ if (!before.isFile()) {
312
+ throw cliError(`Generated project entry is not a file: ${entry.path}`);
313
+ }
314
+ if (
315
+ before.size !== expected.size ||
316
+ before.size > maximumTemplateFileSize
317
+ ) {
318
+ throw cliError(`Generated project size mismatch: ${entry.path}`);
319
+ }
320
+ const content = await handle.readFile();
321
+ const after = await handle.stat();
322
+ const pathname = await lstat(location);
323
+ const digest = createHash("sha256").update(content).digest("hex");
324
+ if (
325
+ before.dev !== after.dev ||
326
+ before.ino !== after.ino ||
327
+ before.size !== after.size ||
328
+ pathname.dev !== after.dev ||
329
+ pathname.ino !== after.ino ||
330
+ !pathname.isFile() ||
331
+ pathname.isSymbolicLink() ||
332
+ content.byteLength !== expected.size ||
333
+ digest !== expected.sha256
334
+ ) {
335
+ throw cliError(`Generated project checksum mismatch: ${entry.path}`);
336
+ }
337
+ } finally {
338
+ await handle.close();
339
+ }
340
+ }
341
+
342
+ assertPathSet(await listTree(target));
343
+ }
344
+
345
+ function sameIdentity(actual, expected) {
346
+ return (
347
+ actual.dev === expected.dev &&
348
+ actual.ino === expected.ino &&
349
+ actual.isDirectory() &&
350
+ !actual.isSymbolicLink()
351
+ );
352
+ }
353
+
354
+ async function assertOwnedStaging(staging, expected) {
355
+ let actual;
356
+ try {
357
+ actual = await lstat(staging);
358
+ } catch (error) {
359
+ if (error?.code === "ENOENT") {
360
+ throw cliError(`Staging directory disappeared: ${staging}`);
361
+ }
362
+ throw error;
363
+ }
364
+ if (!sameIdentity(actual, expected)) {
365
+ throw cliError(`Staging directory was replaced: ${staging}`);
366
+ }
367
+ }
368
+
369
+ export async function installStagedProject({
370
+ staging,
371
+ target,
372
+ manifest,
373
+ stagingIdentity,
374
+ renameProject = rename,
375
+ }) {
376
+ await assertOwnedStaging(staging, stagingIdentity);
377
+ await verifyStagedProject(staging, manifest);
378
+ await assertOwnedStaging(staging, stagingIdentity);
379
+ await assertTargetAbsent(target);
380
+
381
+ try {
382
+ await renameProject(staging, target);
383
+ } catch (error) {
384
+ if (error?.code === "EEXIST") {
385
+ throw cliError(
386
+ `Target path appeared before installation completed: ${target}`,
387
+ );
388
+ }
389
+ if (error?.code === "ENOTEMPTY") {
390
+ throw cliError(
391
+ `Target path became an existing non-empty directory before installation completed: ${target}`,
392
+ );
393
+ }
394
+ if (error?.code === "ENOTDIR" || error?.code === "EISDIR") {
395
+ throw cliError(
396
+ `Target path became an incompatible file or symbolic link before installation completed: ${target}`,
397
+ );
398
+ }
399
+ throw error;
400
+ }
401
+ }
402
+
403
+ function shellQuote(value) {
404
+ return `'${value.replaceAll("'", "'\\''")}'`;
405
+ }
406
+
407
+ export async function scaffold(targetArgument, options = {}) {
408
+ const packageRoot = options.packageRoot ?? defaultPackageRoot;
409
+ const { manifest, templateRoot } = await verifyTemplate(packageRoot);
410
+ const target = path.resolve(options.cwd ?? process.cwd(), targetArgument);
411
+ await assertTargetAbsent(target);
412
+ const parent = path.dirname(target);
413
+ const parentStat = await lstat(parent).catch((error) => {
414
+ if (error?.code === "ENOENT") {
415
+ throw cliError(`Target parent directory does not exist: ${parent}`);
416
+ }
417
+ throw error;
418
+ });
419
+ if (!parentStat.isDirectory()) {
420
+ throw cliError(`Target parent is not a directory: ${parent}`);
421
+ }
422
+
423
+ const staging = path.join(
424
+ parent,
425
+ `.create-expo-updates-server-${randomUUID()}`,
426
+ );
427
+ let stagingIdentity;
428
+ let stagingCreated = false;
429
+
430
+ try {
431
+ await mkdir(staging, { mode: 0o700 });
432
+ stagingCreated = true;
433
+ stagingIdentity = await lstat(staging);
434
+ if (
435
+ !stagingIdentity.isDirectory() ||
436
+ stagingIdentity.isSymbolicLink()
437
+ ) {
438
+ throw cliError(`Could not create a private staging directory: ${staging}`);
439
+ }
440
+ await chmod(staging, 0o700);
441
+ const privateStaging = await lstat(staging);
442
+ if (
443
+ !sameIdentity(privateStaging, stagingIdentity) ||
444
+ (privateStaging.mode & 0o777) !== 0o700
445
+ ) {
446
+ throw cliError(`Could not create a private staging directory: ${staging}`);
447
+ }
448
+ await copyToStage(templateRoot, manifest, staging);
449
+ if (options.beforeInstall) {
450
+ await options.beforeInstall({ staging, target });
451
+ }
452
+ await installStagedProject({
453
+ staging,
454
+ target,
455
+ manifest,
456
+ stagingIdentity,
457
+ });
458
+ } catch (error) {
459
+ if (stagingCreated) {
460
+ throw cliError(
461
+ `${error.message}
462
+ Staging directory retained at: ${staging}
463
+ Inspect or remove it manually.`,
464
+ );
465
+ }
466
+ throw error;
467
+ }
468
+
469
+ return {
470
+ target,
471
+ nextSteps: `Created Expo Updates Server in ${target}
472
+
473
+ Next steps (Docker):
474
+ cd -- ${shellQuote(targetArgument)}
475
+ cp .env.docker.example .env
476
+ docker compose up -d --build
477
+
478
+ Next steps (npm):
479
+ cd -- ${shellQuote(targetArgument)}
480
+ npm ci
481
+ cp .env.example .env.local
482
+ npm run dev`,
483
+ };
484
+ }
485
+
486
+ function parseArguments(args) {
487
+ const options = args.filter((argument) => argument.startsWith("-"));
488
+ const positionals = args.filter((argument) => !argument.startsWith("-"));
489
+ for (const option of options) {
490
+ if (!["--help", "-h", "--version", "-v"].includes(option)) {
491
+ throw cliError(`Unknown option: ${option}`);
492
+ }
493
+ }
494
+
495
+ if (options.length > 0) {
496
+ if (args.length !== 1) {
497
+ throw cliError("Help and version options must be used on their own");
498
+ }
499
+ return {
500
+ action: options[0] === "--help" || options[0] === "-h" ? "help" : "version",
501
+ };
502
+ }
503
+ if (positionals.length !== 1) {
504
+ throw cliError(
505
+ positionals.length === 0
506
+ ? "Exactly one target directory is required"
507
+ : "Only one target directory is allowed",
508
+ );
509
+ }
510
+ return { action: "scaffold", target: positionals[0] };
511
+ }
512
+
513
+ function supportedNode() {
514
+ const [major, minor] = process.versions.node.split(".").map(Number);
515
+ return major > 20 || (major === 20 && minor >= 9);
516
+ }
517
+
518
+ export async function main(args, io = console, options = {}) {
519
+ try {
520
+ if (!supportedNode()) {
521
+ throw cliError(`${command} requires Node.js 20.9 or newer`);
522
+ }
523
+ const parsed = parseArguments(args);
524
+ if (parsed.action === "help") {
525
+ io.log(help.trimEnd());
526
+ return 0;
527
+ }
528
+
529
+ const packageRoot = options.packageRoot ?? defaultPackageRoot;
530
+ if (parsed.action === "version") {
531
+ const packageJson = JSON.parse(
532
+ await readFile(path.join(packageRoot, "package.json"), "utf8"),
533
+ );
534
+ io.log(packageJson.version);
535
+ return 0;
536
+ }
537
+
538
+ const result = await scaffold(parsed.target, {
539
+ ...options,
540
+ packageRoot,
541
+ });
542
+ io.log(result.nextSteps);
543
+ return 0;
544
+ } catch (error) {
545
+ io.error(`Error: ${error.message}`);
546
+ io.error(`Run "${command} --help" for usage.`);
547
+ return 1;
548
+ }
549
+ }
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "create-expo-updates-server",
3
+ "version": "0.1.0",
4
+ "description": "Create a standalone Expo Updates Server project",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "bin": {
8
+ "create-expo-updates-server": "bin/create-expo-updates-server.mjs"
9
+ },
10
+ "files": [
11
+ "bin/",
12
+ "lib/",
13
+ "dist/",
14
+ "README.md",
15
+ "LICENSE"
16
+ ],
17
+ "engines": {
18
+ "node": ">=20.9"
19
+ },
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "https://github.com/tonichiga/expo-updates-server",
23
+ "directory": "packages/create-expo-updates-server"
24
+ },
25
+ "bugs": {
26
+ "url": "https://github.com/tonichiga/expo-updates-server/issues"
27
+ },
28
+ "homepage": "https://github.com/tonichiga/expo-updates-server#readme",
29
+ "keywords": [
30
+ "expo",
31
+ "expo-updates",
32
+ "initializer",
33
+ "nextjs",
34
+ "ota"
35
+ ],
36
+ "scripts": {
37
+ "build:template": "node scripts/build-template.mjs",
38
+ "test": "node --test --test-concurrency=1 test/*.test.mjs",
39
+ "prepack": "npm run build:template"
40
+ }
41
+ }