-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwizard.ts
More file actions
executable file
·431 lines (363 loc) · 12.3 KB
/
Copy pathwizard.ts
File metadata and controls
executable file
·431 lines (363 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#!/usr/bin/env bun
import { spawn } from "bun";
import * as readline from "readline/promises";
import { promises as fs } from "fs";
import * as path from "path";
interface BuildOption {
name: string;
flag: string;
command: string;
description: string;
customHandler?: () => Promise<number>;
}
// Directories containing WebRTC headers that require patching.
const HEADER_DIRS = [
'ios/Pods/WebRTC-lib/WebRTC.xcframework/ios-arm64/WebRTC.framework/Headers',
'ios/Pods/WebRTC-lib/WebRTC.xcframework/ios-x86_64_arm64-simulator/WebRTC.framework/Headers',
];
async function patchHeaderDirectory(relativeDir: string): Promise<void> {
const absoluteDir = path.resolve(process.cwd(), relativeDir);
try {
await fs.access(absoluteDir, fs.constants.R_OK | fs.constants.W_OK);
} catch (err) {
console.warn(`\n⚠️ Skipping ${relativeDir}: directory not found or inaccessible.`);
return;
}
const targetDir = path.join(absoluteDir, 'sdk/objc/base');
await fs.mkdir(targetDir, { recursive: true });
const entries = await fs.readdir(absoluteDir, { withFileTypes: true });
for (const entry of entries) {
const { name } = entry;
if (name === 'sdk') {
continue;
}
if (entry.isDirectory()) {
continue;
}
const source = path.join(absoluteDir, name);
const destination = path.join(targetDir, name);
try {
await fs.link(source, destination);
} catch (err: any) {
if (err.code === 'EEXIST') {
continue;
}
throw new Error(`Failed to link ${name} in ${relativeDir}: ${err.message}`);
}
}
console.log(`\n✅ Patched headers in ${relativeDir}`);
}
async function patchHeaders(): Promise<number> {
console.log('\nPatching WebRTC-lib headers...');
try {
for (const dir of HEADER_DIRS) {
await patchHeaderDirectory(dir);
}
console.log('\n🎉 All done!');
return 0;
} catch (err: any) {
console.error(`\n❌ ${err.message}`);
return 1;
}
}
async function findMostRecentIpa(): Promise<{ filePath: string; size: number; mtime: Date } | null> {
let mostRecent: { filePath: string; size: number; mtime: Date } | null = null;
async function searchDir(dir: string, depth: number): Promise<void> {
if (depth > 3) return;
let entries;
try {
entries = await fs.readdir(dir, { withFileTypes: true });
} catch {
return;
}
for (const entry of entries) {
if (entry.name.startsWith('.') || entry.name === 'node_modules') continue;
const fullPath = path.join(dir, entry.name);
if (entry.isFile() && entry.name.endsWith('.ipa')) {
const stat = await fs.stat(fullPath);
if (!mostRecent || stat.mtime > mostRecent.mtime) {
mostRecent = { filePath: fullPath, size: stat.size, mtime: stat.mtime };
}
} else if (entry.isDirectory()) {
await searchDir(fullPath, depth + 1);
}
}
}
await searchDir(process.cwd(), 0);
return mostRecent;
}
async function easSubmitLocalIpa(): Promise<number> {
console.log('\n🔍 Searching for IPA files...');
const ipa = await findMostRecentIpa();
if (!ipa) {
console.error('\n❌ No IPA file found. Run a local build first (e.g. EAS Build Dev Local).');
return 1;
}
const sizeMb = (ipa.size / (1024 * 1024)).toFixed(1);
console.log('\n📦 Most recent IPA found:');
console.log(` Path: ${ipa.filePath}`);
console.log(` Size: ${sizeMb} MB`);
console.log(` Modified: ${ipa.mtime.toLocaleString()}`);
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
const answer = await rl.question('\nSubmit this IPA to the App Store? (y/N): ');
rl.close();
if (answer.trim().toLowerCase() !== 'y') {
console.log('\nCancelled.');
process.exit(0);
}
return executeCommand(`eas submit --platform ios --path "${ipa.filePath}"`);
}
async function easBuildProdLocal(): Promise<number> {
const buildExitCode = await executeCommand(
"eas build --platform ios --profile production --non-interactive --local"
);
if (buildExitCode !== 0) {
return buildExitCode;
}
console.log('\n✅ Local production build complete. Proceeding to submission...');
return easSubmitLocalIpa();
}
async function startExpoServer(): Promise<number> {
console.log('\n🔍 Running TypeScript check...\n');
const tscProc = spawn({
cmd: ["sh", "-c", "bunx tsc --noEmit"],
stdout: "inherit",
stderr: "inherit",
stdin: "inherit",
});
const tscExitCode = await tscProc.exited;
if (tscExitCode !== 0) {
console.error('\n❌ TypeScript check failed. Fix errors before starting Expo.');
return tscExitCode;
}
console.log('\n✅ TypeScript check passed!\n');
console.log('🚀 Starting Expo server...\n');
const expoProc = spawn({
cmd: ["sh", "-c", "npx expo start"],
stdout: "inherit",
stderr: "inherit",
stdin: "inherit",
});
// Set up signal handlers to forward signals to child process
let isShuttingDown = false;
const shutdownHandler = (signal: NodeJS.Signals) => {
if (isShuttingDown) {
return;
}
isShuttingDown = true;
console.log(`\n\n⚠️ Received ${signal}, shutting down Expo server gracefully...`);
// Kill the child process
expoProc.kill(signal);
// Give it 2 seconds to clean up, then force kill if needed
setTimeout(() => {
if (!expoProc.killed) {
console.log('\n⚠️ Process did not exit cleanly, force killing...');
expoProc.kill('SIGKILL');
}
}, 2000);
};
// Handle SIGINT (Ctrl+C) and SIGTERM
const sigintHandler = () => shutdownHandler('SIGINT');
const sigtermHandler = () => shutdownHandler('SIGTERM');
process.on('SIGINT', sigintHandler);
process.on('SIGTERM', sigtermHandler);
const expoExitCode = await expoProc.exited;
// Clean up signal handlers
process.off('SIGINT', sigintHandler);
process.off('SIGTERM', sigtermHandler);
return expoExitCode;
}
const BUILD_OPTIONS: BuildOption[] = [
{
name: "Start Expo Server",
flag: "start-expo",
command: "",
description: "Run TypeScript check, then start Expo server",
customHandler: startExpoServer,
},
{
name: "Patch WebRTC Headers",
flag: "patch-webrtc",
command: "",
description: "Patch WebRTC-lib headers for iOS",
customHandler: patchHeaders,
},
{
name: "EAS Build Dev",
flag: "eas-build-dev",
command: "eas build --platform ios --profile dev_self_contained --non-interactive",
description: "Build iOS app with dev_self_contained profile",
},
{
name: "EAS Build Dev Local",
flag: "eas-build-dev-local",
command: "eas build --platform ios --profile dev_self_contained --non-interactive --local",
description: "Build iOS app locally with dev_self_contained profile",
},
{
name: "EAS Update Dev",
flag: "eas-update-dev",
command: 'eas update --platform ios --branch dev_self_contained --message "Update"',
description: "Push an OTA update to dev_self_contained branch",
},
{
name: "Clean Build",
flag: "clean-build",
command: "CI=1 bunx expo prebuild --clean --platform ios",
description: "Clean prebuild for iOS",
},
{
name: "EAS Build Prod",
flag: "eas-build-prod",
command: "eas build --platform ios --profile production --non-interactive && eas submit --platform ios",
description: "Build and submit iOS app to App Store",
},
{
name: "EAS Build Prod Local",
flag: "eas-build-prod-local",
command: "",
description: "Build IPA locally with Distribution profile, then submit to App Store",
customHandler: easBuildProdLocal,
},
{
name: "EAS Submit Local IPA",
flag: "eas-submit-local-ipa",
command: "",
description: "Find the most recent local IPA and submit it to the App Store",
customHandler: easSubmitLocalIpa,
},
{
name: "Run Xcodebuild",
flag: "build-ios-local",
command:
"set -o pipefail && if test -x \"$(command -v xcpretty)\"; then xcodebuild build -workspace ios/vibemachine.xcworkspace -scheme vibemachine -configuration Debug -sdk iphonesimulator -destination 'generic/platform=iOS Simulator' | xcpretty; else xcodebuild build -workspace ios/vibemachine.xcworkspace -scheme vibemachine -configuration Debug -sdk iphonesimulator -destination 'generic/platform=iOS Simulator'; fi",
description: "Compile Swift without launching (same compiler as Xcode, no EAS overhead)",
},
{
name: "Open in Xcode",
flag: "open-xcode",
command: "xed ios",
description: "Open iOS project in Xcode",
},
{
name: "Register New Device UDID",
flag: "register-device",
command: "bunx eas device:create",
description: "Register a new device UDID with EAS",
},
];
async function executeCommand(command: string): Promise<number> {
console.log(`\n🚀 Executing: ${command}\n`);
const proc = spawn({
cmd: ["sh", "-c", command],
stdout: "inherit",
stderr: "inherit",
stdin: "inherit",
});
// Set up signal handlers to forward signals to child process
let isShuttingDown = false;
const shutdownHandler = (signal: NodeJS.Signals) => {
if (isShuttingDown) {
return;
}
isShuttingDown = true;
console.log(`\n\n⚠️ Received ${signal}, shutting down command gracefully...`);
// Kill the child process
proc.kill(signal);
// Give it 2 seconds to clean up, then force kill if needed
setTimeout(() => {
if (!proc.killed) {
console.log('\n⚠️ Process did not exit cleanly, force killing...');
proc.kill('SIGKILL');
}
}, 2000);
};
// Handle SIGINT (Ctrl+C) and SIGTERM
const sigintHandler = () => shutdownHandler('SIGINT');
const sigtermHandler = () => shutdownHandler('SIGTERM');
process.on('SIGINT', sigintHandler);
process.on('SIGTERM', sigtermHandler);
const exitCode = await proc.exited;
// Clean up signal handlers
process.off('SIGINT', sigintHandler);
process.off('SIGTERM', sigtermHandler);
return exitCode;
}
function showMenu(): void {
console.log("\n=== Arty Build Wizard ===\n");
BUILD_OPTIONS.forEach((option, index) => {
console.log(`${index + 1}) ${option.name}`);
console.log(` ${option.description}`);
console.log(` Flag: bun run wizard ${option.flag}\n`);
});
console.log("0) Exit");
}
async function executeChoice(choice: string): Promise<void> {
const index = parseInt(choice) - 1;
if (choice === "0" || choice === "") {
console.log("\nGoodbye!");
process.exit(0);
}
if (index >= 0 && index < BUILD_OPTIONS.length) {
const option = BUILD_OPTIONS[index];
console.log(`\n📦 ${option.name}`);
const exitCode = option.customHandler
? await option.customHandler()
: await executeCommand(option.command);
if (exitCode === 0) {
console.log(`\n✅ ${option.name} completed successfully! (${new Date().toLocaleTimeString()})`);
process.exit(0);
} else {
console.error(`\n❌ ${option.name} failed with exit code ${exitCode}`);
process.exit(exitCode);
}
} else {
console.log("\n❌ Unrecognized option.");
process.exit(1);
}
}
async function handleFlag(flag: string): Promise<void> {
const option = BUILD_OPTIONS.find((opt) => opt.flag === flag);
if (option) {
console.log(`\n📦 ${option.name}`);
const exitCode = option.customHandler
? await option.customHandler()
: await executeCommand(option.command);
if (exitCode === 0) {
console.log(`\n✅ ${option.name} completed successfully! (${new Date().toLocaleTimeString()})`);
process.exit(0);
} else {
console.error(`\n❌ ${option.name} failed with exit code ${exitCode}`);
process.exit(exitCode);
}
} else {
console.error(`\n❌ Unknown flag: ${flag}`);
console.log("\nAvailable flags:");
BUILD_OPTIONS.forEach((opt) => {
console.log(` - ${opt.flag}: ${opt.description}`);
});
process.exit(1);
}
}
async function main(): Promise<void> {
const args = Bun.argv.slice(2);
// Check if a flag was provided
if (args.length > 0) {
await handleFlag(args[0]);
return;
}
// Interactive mode
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
showMenu();
const answer = await rl.question("\nSelect an option: ");
rl.close();
const choice = answer.trim();
await executeChoice(choice);
}
main().catch((err) => {
console.error(`\n❌ ${err.message}`);
process.exit(1);
});