@sgedda/mockifyer-fetch 1.8.72 → 1.8.86
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.
- package/dist/index.d.ts +3 -0
- package/dist/index.js +92 -8
- package/dist/metro-atlas-key-handlers.d.ts +115 -0
- package/dist/metro-atlas-key-handlers.js +448 -0
- package/dist/metro-config.d.ts +3 -0
- package/dist/metro-config.js +31 -3
- package/dist/metro-sync-middleware.d.ts +19 -0
- package/dist/metro-sync-middleware.js +54 -11
- package/dist/react-native.d.ts +26 -2
- package/dist/react-native.js +39 -21
- package/package.json +2 -2
- package/src/index.ts +130 -11
- package/src/metro-atlas-key-handlers.ts +515 -0
- package/src/metro-config.ts +31 -3
- package/src/metro-sync-middleware.ts +92 -20
- package/src/react-native.ts +78 -24
|
@@ -0,0 +1,515 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Metro terminal keys for Mockifyer:
|
|
3
|
+
* - `t` (default): start/stop Atlas capture (stop generates HTML)
|
|
4
|
+
* - `m` (default): open the Mockifyer dashboard in the browser
|
|
5
|
+
*
|
|
6
|
+
* Opening `mockifyer-atlas` (SSE `/mockifyer-network-events/stream`) auto-starts
|
|
7
|
+
* capture when idle — then press `t` once to stop & generate.
|
|
8
|
+
*
|
|
9
|
+
* `a` is reserved by Metro for Android (`i` iOS, `r` reload, `d` Dev Menu, `j` DevTools).
|
|
10
|
+
* Disable Atlas with `atlasKey: false`, dashboard with `dashboardKey: false`.
|
|
11
|
+
*/
|
|
12
|
+
import * as readline from "readline";
|
|
13
|
+
import { spawn } from "child_process";
|
|
14
|
+
import { logger } from "@sgedda/mockifyer-core";
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Keys already used by @react-native/community-cli-plugin (avoid stealing).
|
|
18
|
+
* Value is the short reason shown when a binding collides.
|
|
19
|
+
*/
|
|
20
|
+
const RESERVED_METRO_KEYS: ReadonlyMap<string, string> = new Map([
|
|
21
|
+
["a", "Android"],
|
|
22
|
+
["d", "the Dev Menu"],
|
|
23
|
+
["i", "iOS"],
|
|
24
|
+
["j", "DevTools"],
|
|
25
|
+
["r", "reload"],
|
|
26
|
+
]);
|
|
27
|
+
|
|
28
|
+
/** `t` — `a` launches Android in the Metro / RN CLI. */
|
|
29
|
+
export const DEFAULT_METRO_ATLAS_KEY = "t";
|
|
30
|
+
export const DEFAULT_METRO_DASHBOARD_KEY = "m";
|
|
31
|
+
export const DEFAULT_METRO_DASHBOARD_URL = "http://localhost:3002";
|
|
32
|
+
|
|
33
|
+
/** Delay so RN/Expo CLI can own raw-mode stdin first. */
|
|
34
|
+
const ATTACH_DEFER_MS = 1000;
|
|
35
|
+
|
|
36
|
+
export type AtlasKeyOption = string | false;
|
|
37
|
+
|
|
38
|
+
export type MetroAtlasSessionPhase = "idle" | "capturing" | "rendering";
|
|
39
|
+
|
|
40
|
+
export type MetroAtlasSessionStartReason = "key" | "stream";
|
|
41
|
+
|
|
42
|
+
export interface AttachMetroAtlasKeyHandlerOptions {
|
|
43
|
+
/**
|
|
44
|
+
* Key that toggles Atlas capture (default `"t"`).
|
|
45
|
+
* Pass `false` to disable. `a` is reserved by Metro for Android.
|
|
46
|
+
*/
|
|
47
|
+
atlasKey?: AtlasKeyOption;
|
|
48
|
+
/**
|
|
49
|
+
* Key that opens the dashboard in the browser (default `"m"`).
|
|
50
|
+
* Pass `false` to disable.
|
|
51
|
+
*/
|
|
52
|
+
dashboardKey?: AtlasKeyOption;
|
|
53
|
+
/**
|
|
54
|
+
* Dashboard origin (default: `MOCKIFYER_DASHBOARD_URL` or `http://localhost:3002`).
|
|
55
|
+
* Mount prefix from `MOCKIFYER_DASHBOARD_BASE` is appended when set.
|
|
56
|
+
*/
|
|
57
|
+
dashboardUrl?: string;
|
|
58
|
+
/** Called on session start (clear hop buffer, etc.). Required when Atlas key is enabled. */
|
|
59
|
+
onSessionStart?: () => void;
|
|
60
|
+
/** Called on session stop — generate Atlas HTML. Required when Atlas key is enabled. */
|
|
61
|
+
onSessionStop?: () => void | Promise<void>;
|
|
62
|
+
/** Override stdin (tests). */
|
|
63
|
+
stdin?: NodeJS.ReadStream;
|
|
64
|
+
/** Override defer delay in ms (tests). */
|
|
65
|
+
deferMs?: number;
|
|
66
|
+
/** Override URL opener (tests). */
|
|
67
|
+
openUrl?: (url: string) => void;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export interface AttachedMetroAtlasKeyHandler {
|
|
71
|
+
/** Resolved Atlas key (lowercase), or null if disabled. */
|
|
72
|
+
key: string | null;
|
|
73
|
+
/** Resolved dashboard key (lowercase), or null if disabled. */
|
|
74
|
+
dashboardKey: string | null;
|
|
75
|
+
/** Resolved dashboard browse URL. */
|
|
76
|
+
dashboardUrl: string | null;
|
|
77
|
+
/** Detach listener and clear module attach flag. */
|
|
78
|
+
detach: () => void;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
interface SessionCallbacks {
|
|
82
|
+
key: string | null;
|
|
83
|
+
dashboardKey: string | null;
|
|
84
|
+
dashboardUrl: string | null;
|
|
85
|
+
onSessionStart?: () => void;
|
|
86
|
+
onSessionStop?: () => void | Promise<void>;
|
|
87
|
+
openUrl: (url: string) => void;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
let attached = false;
|
|
91
|
+
let weSetRawMode = false;
|
|
92
|
+
let activeHandler: ((str: string, key: readline.Key) => void) | null = null;
|
|
93
|
+
let activeStdin: NodeJS.ReadStream | null = null;
|
|
94
|
+
let pendingTimer: ReturnType<typeof setTimeout> | null = null;
|
|
95
|
+
let sessionPhase: MetroAtlasSessionPhase = "idle";
|
|
96
|
+
let sessionStartedAt: number | null = null;
|
|
97
|
+
let sessionCallbacks: SessionCallbacks | null = null;
|
|
98
|
+
let streamSubscriberCount = 0;
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Normalize a single-char key option: default when undefined, `false` → disabled.
|
|
102
|
+
*/
|
|
103
|
+
export function resolveKeyOption(
|
|
104
|
+
value: AtlasKeyOption | undefined,
|
|
105
|
+
defaultKey: string,
|
|
106
|
+
): string | null {
|
|
107
|
+
if (value === false) return null;
|
|
108
|
+
const raw =
|
|
109
|
+
value === undefined || value === null ? defaultKey : String(value).trim();
|
|
110
|
+
if (!raw) return null;
|
|
111
|
+
const key = raw.length === 1 ? raw.toLowerCase() : raw.toLowerCase().charAt(0);
|
|
112
|
+
return key || null;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Normalize `atlasKey` option: default `"t"`, `false` → disabled, else single char.
|
|
117
|
+
*/
|
|
118
|
+
export function resolveAtlasKeyOption(
|
|
119
|
+
atlasKey?: AtlasKeyOption,
|
|
120
|
+
): string | null {
|
|
121
|
+
return resolveKeyOption(atlasKey, DEFAULT_METRO_ATLAS_KEY);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Normalize `dashboardKey` option: default `"m"`, `false` → disabled.
|
|
126
|
+
*/
|
|
127
|
+
export function resolveDashboardKeyOption(
|
|
128
|
+
dashboardKey?: AtlasKeyOption,
|
|
129
|
+
): string | null {
|
|
130
|
+
return resolveKeyOption(dashboardKey, DEFAULT_METRO_DASHBOARD_KEY);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Resolve dashboard browse URL for Metro `m`.
|
|
135
|
+
* Precedence: explicit → `MOCKIFYER_DASHBOARD_URL` → `http://localhost:3002`,
|
|
136
|
+
* then append `MOCKIFYER_DASHBOARD_BASE` when set.
|
|
137
|
+
*/
|
|
138
|
+
export function resolveMetroDashboardUrl(explicit?: string): string {
|
|
139
|
+
const fromExplicit = explicit?.trim();
|
|
140
|
+
const fromEnv =
|
|
141
|
+
typeof process !== "undefined"
|
|
142
|
+
? process.env.MOCKIFYER_DASHBOARD_URL?.trim()
|
|
143
|
+
: undefined;
|
|
144
|
+
const base = (fromExplicit || fromEnv || DEFAULT_METRO_DASHBOARD_URL).replace(
|
|
145
|
+
/\/+$/,
|
|
146
|
+
"",
|
|
147
|
+
);
|
|
148
|
+
const mountRaw =
|
|
149
|
+
typeof process !== "undefined"
|
|
150
|
+
? process.env.MOCKIFYER_DASHBOARD_BASE?.trim()
|
|
151
|
+
: undefined;
|
|
152
|
+
if (!mountRaw) return base;
|
|
153
|
+
const mount = mountRaw.replace(/\/+$/, "");
|
|
154
|
+
if (!mount || mount === "/") return base;
|
|
155
|
+
return `${base}${mount.startsWith("/") ? mount : `/${mount}`}`;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Open a URL with the OS default handler (browser / file).
|
|
160
|
+
*/
|
|
161
|
+
export function openMetroBrowseUrl(target: string): void {
|
|
162
|
+
const platform = process.platform;
|
|
163
|
+
const child =
|
|
164
|
+
platform === "darwin"
|
|
165
|
+
? spawn("open", [target], { detached: true, stdio: "ignore" })
|
|
166
|
+
: platform === "win32"
|
|
167
|
+
? spawn("cmd", ["/c", "start", "", target], {
|
|
168
|
+
detached: true,
|
|
169
|
+
stdio: "ignore",
|
|
170
|
+
windowsVerbatimArguments: true,
|
|
171
|
+
})
|
|
172
|
+
: spawn("xdg-open", [target], { detached: true, stdio: "ignore" });
|
|
173
|
+
child.once("error", (err) => {
|
|
174
|
+
console.error(`[Mockifyer] could not open: ${err.message}`);
|
|
175
|
+
console.log(`[Mockifyer] open manually: ${target}`);
|
|
176
|
+
});
|
|
177
|
+
child.unref();
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Whether a keypress matches a bound key (no ctrl/meta).
|
|
182
|
+
*/
|
|
183
|
+
export function matchesAtlasKey(
|
|
184
|
+
str: string | undefined,
|
|
185
|
+
key: readline.Key | undefined,
|
|
186
|
+
atlasKey: string,
|
|
187
|
+
): boolean {
|
|
188
|
+
if (!atlasKey) return false;
|
|
189
|
+
if (key?.ctrl || key?.meta) return false;
|
|
190
|
+
if (key?.name === atlasKey) return true;
|
|
191
|
+
if (typeof str === "string" && str.toLowerCase() === atlasKey) return true;
|
|
192
|
+
return false;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** Current session phase (idle / capturing / rendering). Exported for tests. */
|
|
196
|
+
export function getMetroAtlasSessionPhase(): MetroAtlasSessionPhase {
|
|
197
|
+
return sessionPhase;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** Active SSE stream subscriber count (tests / diagnostics). */
|
|
201
|
+
export function getMetroAtlasStreamSubscriberCount(): number {
|
|
202
|
+
return streamSubscriberCount;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function formatSessionDuration(startedAt: number): string {
|
|
206
|
+
const ms = Date.now() - startedAt;
|
|
207
|
+
if (ms < 1000) return `${ms}ms`;
|
|
208
|
+
const sec = Math.round(ms / 1000);
|
|
209
|
+
if (sec < 60) return `${sec}s`;
|
|
210
|
+
const min = Math.floor(sec / 60);
|
|
211
|
+
const rem = sec % 60;
|
|
212
|
+
return `${min}m ${rem}s`;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Start capture when idle. No-op if already capturing/rendering or Atlas key off.
|
|
217
|
+
* @returns true if a new session was started
|
|
218
|
+
*/
|
|
219
|
+
export function startMetroAtlasSession(
|
|
220
|
+
reason: MetroAtlasSessionStartReason = "key",
|
|
221
|
+
): boolean {
|
|
222
|
+
if (!sessionCallbacks?.key || !sessionCallbacks.onSessionStart) return false;
|
|
223
|
+
if (sessionPhase !== "idle") return false;
|
|
224
|
+
|
|
225
|
+
const { key, onSessionStart } = sessionCallbacks;
|
|
226
|
+
sessionPhase = "capturing";
|
|
227
|
+
sessionStartedAt = Date.now();
|
|
228
|
+
try {
|
|
229
|
+
onSessionStart();
|
|
230
|
+
} catch (err) {
|
|
231
|
+
sessionPhase = "idle";
|
|
232
|
+
sessionStartedAt = null;
|
|
233
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
234
|
+
console.error(`[Mockifyer] Atlas session start failed: ${message}`);
|
|
235
|
+
return false;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
if (reason === "stream") {
|
|
239
|
+
console.log(
|
|
240
|
+
`[Mockifyer] Atlas capture started (stream connected) — press ${key} to stop & generate HTML.`,
|
|
241
|
+
);
|
|
242
|
+
} else {
|
|
243
|
+
console.log(
|
|
244
|
+
`[Mockifyer] Atlas capture started — press ${key} again to stop & generate HTML.`,
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
return true;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Stop capture and generate Atlas HTML when capturing.
|
|
252
|
+
* @returns true if stop/render was kicked off
|
|
253
|
+
*/
|
|
254
|
+
export function stopMetroAtlasSession(): boolean {
|
|
255
|
+
if (!sessionCallbacks?.onSessionStop) return false;
|
|
256
|
+
if (sessionPhase !== "capturing") return false;
|
|
257
|
+
|
|
258
|
+
const { onSessionStop } = sessionCallbacks;
|
|
259
|
+
const startedAt = sessionStartedAt ?? Date.now();
|
|
260
|
+
const duration = formatSessionDuration(startedAt);
|
|
261
|
+
sessionPhase = "rendering";
|
|
262
|
+
console.log(
|
|
263
|
+
`[Mockifyer] Atlas capture stopped (${duration}) — generating HTML…`,
|
|
264
|
+
);
|
|
265
|
+
Promise.resolve()
|
|
266
|
+
.then(() => onSessionStop())
|
|
267
|
+
.catch((err: unknown) => {
|
|
268
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
269
|
+
console.error(`[Mockifyer] Atlas render failed: ${message}`);
|
|
270
|
+
})
|
|
271
|
+
.finally(() => {
|
|
272
|
+
sessionPhase = "idle";
|
|
273
|
+
sessionStartedAt = null;
|
|
274
|
+
});
|
|
275
|
+
return true;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/** Open the dashboard URL when the dashboard key is enabled. */
|
|
279
|
+
export function openMetroDashboard(): boolean {
|
|
280
|
+
if (!sessionCallbacks?.dashboardKey || !sessionCallbacks.dashboardUrl) {
|
|
281
|
+
return false;
|
|
282
|
+
}
|
|
283
|
+
const url = sessionCallbacks.dashboardUrl;
|
|
284
|
+
console.log(`[Mockifyer] Opening dashboard → ${url}`);
|
|
285
|
+
try {
|
|
286
|
+
sessionCallbacks.openUrl(url);
|
|
287
|
+
} catch (err) {
|
|
288
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
289
|
+
console.error(`[Mockifyer] Could not open dashboard: ${message}`);
|
|
290
|
+
return false;
|
|
291
|
+
}
|
|
292
|
+
return true;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* First SSE client for `/mockifyer-network-events/stream` auto-starts capture.
|
|
297
|
+
*/
|
|
298
|
+
export function notifyMetroAtlasStreamClientConnected(): void {
|
|
299
|
+
streamSubscriberCount += 1;
|
|
300
|
+
if (streamSubscriberCount === 1) {
|
|
301
|
+
startMetroAtlasSession("stream");
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* SSE client disconnected — capture stays open until Metro `a` (or explicit stop).
|
|
307
|
+
*/
|
|
308
|
+
export function notifyMetroAtlasStreamClientDisconnected(): void {
|
|
309
|
+
streamSubscriberCount = Math.max(0, streamSubscriberCount - 1);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
function clearPendingTimer(): void {
|
|
313
|
+
if (pendingTimer != null) {
|
|
314
|
+
clearTimeout(pendingTimer);
|
|
315
|
+
pendingTimer = null;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* Detach a previously attached handler (idempotent). Exported for tests.
|
|
321
|
+
*/
|
|
322
|
+
export function detachMetroAtlasKeyHandler(): void {
|
|
323
|
+
clearPendingTimer();
|
|
324
|
+
if (activeHandler && activeStdin) {
|
|
325
|
+
activeStdin.removeListener("keypress", activeHandler);
|
|
326
|
+
}
|
|
327
|
+
if (weSetRawMode && activeStdin?.isTTY && typeof activeStdin.setRawMode === "function") {
|
|
328
|
+
try {
|
|
329
|
+
activeStdin.setRawMode(false);
|
|
330
|
+
} catch {
|
|
331
|
+
// ignore
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
activeHandler = null;
|
|
335
|
+
activeStdin = null;
|
|
336
|
+
weSetRawMode = false;
|
|
337
|
+
attached = false;
|
|
338
|
+
sessionPhase = "idle";
|
|
339
|
+
sessionStartedAt = null;
|
|
340
|
+
sessionCallbacks = null;
|
|
341
|
+
streamSubscriberCount = 0;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
function warnIfReserved(key: string, label: string): void {
|
|
345
|
+
const reason = RESERVED_METRO_KEYS.get(key);
|
|
346
|
+
if (reason) {
|
|
347
|
+
logger.warn(
|
|
348
|
+
`[Mockifyer] ${label} "${key}" is reserved for ${reason} — pick another letter`,
|
|
349
|
+
);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Listen for Metro terminal keys (Atlas capture + open dashboard).
|
|
355
|
+
* Safe to call once per process; subsequent calls are no-ops until detach.
|
|
356
|
+
*/
|
|
357
|
+
export function attachMetroAtlasKeyHandler(
|
|
358
|
+
options: AttachMetroAtlasKeyHandlerOptions,
|
|
359
|
+
): AttachedMetroAtlasKeyHandler {
|
|
360
|
+
const atlasKey = resolveAtlasKeyOption(options.atlasKey);
|
|
361
|
+
const dashboardKey = resolveDashboardKeyOption(options.dashboardKey);
|
|
362
|
+
const dashboardUrl =
|
|
363
|
+
dashboardKey != null
|
|
364
|
+
? resolveMetroDashboardUrl(options.dashboardUrl)
|
|
365
|
+
: null;
|
|
366
|
+
|
|
367
|
+
if (atlasKey == null && dashboardKey == null) {
|
|
368
|
+
return {
|
|
369
|
+
key: null,
|
|
370
|
+
dashboardKey: null,
|
|
371
|
+
dashboardUrl: null,
|
|
372
|
+
detach: () => undefined,
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
if (atlasKey != null) warnIfReserved(atlasKey, "atlasKey");
|
|
377
|
+
if (dashboardKey != null) warnIfReserved(dashboardKey, "dashboardKey");
|
|
378
|
+
|
|
379
|
+
if (
|
|
380
|
+
atlasKey != null &&
|
|
381
|
+
dashboardKey != null &&
|
|
382
|
+
atlasKey === dashboardKey
|
|
383
|
+
) {
|
|
384
|
+
logger.warn(
|
|
385
|
+
`[Mockifyer] atlasKey and dashboardKey are both "${atlasKey}" — dashboard key ignored`,
|
|
386
|
+
);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
const effectiveDashboardKey =
|
|
390
|
+
atlasKey != null && dashboardKey === atlasKey ? null : dashboardKey;
|
|
391
|
+
|
|
392
|
+
sessionCallbacks = {
|
|
393
|
+
key: atlasKey,
|
|
394
|
+
dashboardKey: effectiveDashboardKey,
|
|
395
|
+
dashboardUrl: effectiveDashboardKey != null ? dashboardUrl : null,
|
|
396
|
+
onSessionStart: options.onSessionStart,
|
|
397
|
+
onSessionStop: options.onSessionStop,
|
|
398
|
+
openUrl: options.openUrl ?? openMetroBrowseUrl,
|
|
399
|
+
};
|
|
400
|
+
|
|
401
|
+
if (attached) {
|
|
402
|
+
return {
|
|
403
|
+
key: atlasKey,
|
|
404
|
+
dashboardKey: effectiveDashboardKey,
|
|
405
|
+
dashboardUrl: sessionCallbacks.dashboardUrl,
|
|
406
|
+
detach: detachMetroAtlasKeyHandler,
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
const stdin = options.stdin ?? process.stdin;
|
|
411
|
+
const deferMs = options.deferMs ?? ATTACH_DEFER_MS;
|
|
412
|
+
|
|
413
|
+
const onKeypress = (str: string, keyObj: readline.Key): void => {
|
|
414
|
+
if (keyObj?.ctrl && keyObj.name === "c") {
|
|
415
|
+
if (weSetRawMode) {
|
|
416
|
+
try {
|
|
417
|
+
if (typeof stdin.setRawMode === "function") stdin.setRawMode(false);
|
|
418
|
+
} catch {
|
|
419
|
+
// ignore
|
|
420
|
+
}
|
|
421
|
+
process.exit(0);
|
|
422
|
+
}
|
|
423
|
+
return;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
if (
|
|
427
|
+
effectiveDashboardKey &&
|
|
428
|
+
matchesAtlasKey(str, keyObj, effectiveDashboardKey)
|
|
429
|
+
) {
|
|
430
|
+
openMetroDashboard();
|
|
431
|
+
return;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
if (!atlasKey || !matchesAtlasKey(str, keyObj, atlasKey)) return;
|
|
435
|
+
|
|
436
|
+
if (sessionPhase === "rendering") {
|
|
437
|
+
console.log("[Mockifyer] Atlas render already in progress…");
|
|
438
|
+
return;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
if (sessionPhase === "idle") {
|
|
442
|
+
startMetroAtlasSession("key");
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
stopMetroAtlasSession();
|
|
447
|
+
};
|
|
448
|
+
|
|
449
|
+
const doAttach = (): void => {
|
|
450
|
+
if (attached) return;
|
|
451
|
+
if (!stdin.isTTY) {
|
|
452
|
+
logger.info(
|
|
453
|
+
"[Mockifyer] Metro keys skipped (stdin is not a TTY — use mockifyer-atlas or Dev Menu)",
|
|
454
|
+
);
|
|
455
|
+
return;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
readline.emitKeypressEvents(stdin);
|
|
459
|
+
if (typeof stdin.setRawMode === "function" && stdin.isRaw !== true) {
|
|
460
|
+
try {
|
|
461
|
+
stdin.setRawMode(true);
|
|
462
|
+
weSetRawMode = true;
|
|
463
|
+
} catch (err) {
|
|
464
|
+
logger.warn(
|
|
465
|
+
"[Mockifyer] Could not enable raw stdin for Metro keys:",
|
|
466
|
+
err,
|
|
467
|
+
);
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
if (typeof stdin.resume === "function") {
|
|
473
|
+
stdin.resume();
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
activeHandler = onKeypress;
|
|
477
|
+
activeStdin = stdin;
|
|
478
|
+
stdin.on("keypress", onKeypress);
|
|
479
|
+
attached = true;
|
|
480
|
+
|
|
481
|
+
const parts: string[] = [];
|
|
482
|
+
if (atlasKey) {
|
|
483
|
+
parts.push(
|
|
484
|
+
`Press ${atlasKey} to start/stop Atlas capture (stop generates HTML; stream auto-starts)`,
|
|
485
|
+
);
|
|
486
|
+
}
|
|
487
|
+
if (effectiveDashboardKey && dashboardUrl) {
|
|
488
|
+
parts.push(`Press ${effectiveDashboardKey} to open dashboard (${dashboardUrl})`);
|
|
489
|
+
}
|
|
490
|
+
if (parts.length > 0) {
|
|
491
|
+
console.log(`[Mockifyer] info ${parts.join(". ")}.`);
|
|
492
|
+
}
|
|
493
|
+
};
|
|
494
|
+
|
|
495
|
+
clearPendingTimer();
|
|
496
|
+
if (deferMs <= 0) {
|
|
497
|
+
doAttach();
|
|
498
|
+
} else {
|
|
499
|
+
pendingTimer = setTimeout(() => {
|
|
500
|
+
pendingTimer = null;
|
|
501
|
+
doAttach();
|
|
502
|
+
}, deferMs);
|
|
503
|
+
// Don't keep the process alive solely for the defer timer.
|
|
504
|
+
if (typeof pendingTimer === "object" && pendingTimer && "unref" in pendingTimer) {
|
|
505
|
+
(pendingTimer as NodeJS.Timeout).unref();
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
return {
|
|
510
|
+
key: atlasKey,
|
|
511
|
+
dashboardKey: effectiveDashboardKey,
|
|
512
|
+
dashboardUrl: sessionCallbacks.dashboardUrl,
|
|
513
|
+
detach: detachMetroAtlasKeyHandler,
|
|
514
|
+
};
|
|
515
|
+
}
|
package/src/metro-config.ts
CHANGED
|
@@ -88,7 +88,12 @@ const NODE_BUILTINS = [
|
|
|
88
88
|
* Optional React Native peer dependencies — stubbed when not installed so
|
|
89
89
|
* `tryGetClientIdFromLaunchArguments` and similar helpers no-op instead of crashing Metro.
|
|
90
90
|
*/
|
|
91
|
-
const OPTIONAL_RN_PEER_MODULES = [
|
|
91
|
+
const OPTIONAL_RN_PEER_MODULES = [
|
|
92
|
+
'react-native-launch-arguments',
|
|
93
|
+
'@react-native-async-storage/async-storage',
|
|
94
|
+
'@sgedda/mockifyer-axios',
|
|
95
|
+
'@sgedda/mockifyer-fetch',
|
|
96
|
+
] as const;
|
|
92
97
|
|
|
93
98
|
function isMockifyerStubbedModule(moduleName: string): boolean {
|
|
94
99
|
return (
|
|
@@ -166,6 +171,9 @@ export interface ConfigureMetroOptions {
|
|
|
166
171
|
* syncMiddleware: {
|
|
167
172
|
* projectRoot: __dirname,
|
|
168
173
|
* mockDataPath: './mock-data',
|
|
174
|
+
* // atlasKey: 't' (default) — Metro terminal: start/stop Atlas capture (stop → HTML). `a` is Android.
|
|
175
|
+
* // dashboardKey: 'm' (default) — open dashboard in browser
|
|
176
|
+
* // dashboardUrl: 'http://localhost:3002' — or MOCKIFYER_DASHBOARD_URL
|
|
169
177
|
* },
|
|
170
178
|
* });
|
|
171
179
|
* ```
|
|
@@ -212,14 +220,31 @@ export function configureMetroForMockifyer(
|
|
|
212
220
|
moduleName: string,
|
|
213
221
|
platform: string | null
|
|
214
222
|
): { filePath: string; type: string } | null | undefined => {
|
|
215
|
-
//
|
|
216
|
-
if (
|
|
223
|
+
// Always stub Node built-ins
|
|
224
|
+
if ((NODE_BUILTINS as readonly string[]).includes(moduleName)) {
|
|
217
225
|
return {
|
|
218
226
|
filePath: emptyModulePath,
|
|
219
227
|
type: 'sourceFile',
|
|
220
228
|
};
|
|
221
229
|
}
|
|
222
230
|
|
|
231
|
+
// Stub optional RN peers only when not installed
|
|
232
|
+
if ((OPTIONAL_RN_PEER_MODULES as readonly string[]).includes(moduleName)) {
|
|
233
|
+
// Try to resolve the module - if it exists, let Metro handle it normally
|
|
234
|
+
try {
|
|
235
|
+
const resolved = context.resolveRequest(context, moduleName, platform);
|
|
236
|
+
if (resolved) {
|
|
237
|
+
return resolved;
|
|
238
|
+
}
|
|
239
|
+
} catch {
|
|
240
|
+
// Module not installed - stub it
|
|
241
|
+
return {
|
|
242
|
+
filePath: emptyModulePath,
|
|
243
|
+
type: 'sourceFile',
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
223
248
|
const mockifyerCoreRn = resolveMockifyerCoreReactNativeEntry(moduleName, platform);
|
|
224
249
|
if (mockifyerCoreRn) {
|
|
225
250
|
return mockifyerCoreRn;
|
|
@@ -245,6 +270,9 @@ export function configureMetroForMockifyer(
|
|
|
245
270
|
projectRoot: options.syncMiddleware.projectRoot || process.cwd(),
|
|
246
271
|
mockDataPath: options.syncMiddleware.mockDataPath || 'mock-data',
|
|
247
272
|
testGeneration: options.syncMiddleware.testGeneration,
|
|
273
|
+
atlasKey: options.syncMiddleware.atlasKey,
|
|
274
|
+
dashboardKey: options.syncMiddleware.dashboardKey,
|
|
275
|
+
dashboardUrl: options.syncMiddleware.dashboardUrl,
|
|
248
276
|
});
|
|
249
277
|
|
|
250
278
|
// Add middleware to server config
|