@expo-harmony/cli 55.0.26-harmony.1 → 55.0.26-harmony.11

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 (185) hide show
  1. package/README.md +87 -38
  2. package/build/args.d.ts +19 -0
  3. package/build/args.js +27 -0
  4. package/build/bin/expo-harmony.d.ts +2 -0
  5. package/build/bin/expo-harmony.js +1 -1
  6. package/build/buildHap/build.d.ts +18 -0
  7. package/build/buildHap/build.js +38 -0
  8. package/build/buildHap/index.d.ts +2 -0
  9. package/build/buildHap/index.js +19 -0
  10. package/build/buildHap/options.d.ts +7 -0
  11. package/build/buildHap/options.js +30 -0
  12. package/build/cli.d.ts +3 -0
  13. package/build/cli.js +40 -0
  14. package/build/command.d.ts +9 -0
  15. package/build/command.js +20 -0
  16. package/build/development/manifest.d.ts +1 -0
  17. package/build/development/manifest.js +121 -0
  18. package/build/development/protocol.d.ts +3 -0
  19. package/build/development/protocol.js +33 -0
  20. package/build/development/session.d.ts +5 -0
  21. package/build/development/session.js +66 -0
  22. package/build/doctor/doctor.d.ts +20 -0
  23. package/build/doctor/doctor.js +226 -0
  24. package/build/doctor/index.d.ts +2 -0
  25. package/build/doctor/index.js +16 -0
  26. package/build/doctor/options.d.ts +5 -0
  27. package/build/doctor/options.js +15 -0
  28. package/build/entry.d.ts +2 -0
  29. package/build/entry.js +47 -0
  30. package/build/errors.d.ts +12 -0
  31. package/build/errors.js +16 -0
  32. package/build/expo.d.ts +19 -0
  33. package/build/expo.js +53 -0
  34. package/build/exportEmbed/export.d.ts +18 -0
  35. package/build/exportEmbed/export.js +124 -0
  36. package/build/exportEmbed/index.d.ts +2 -0
  37. package/build/exportEmbed/index.js +19 -0
  38. package/build/exportEmbed/manifest.d.ts +44 -0
  39. package/build/exportEmbed/manifest.js +171 -0
  40. package/build/exportEmbed/options.d.ts +7 -0
  41. package/build/exportEmbed/options.js +22 -0
  42. package/build/file.d.ts +10 -0
  43. package/build/file.js +110 -0
  44. package/build/help.d.ts +1 -0
  45. package/build/help.js +44 -0
  46. package/build/index.d.ts +14 -0
  47. package/build/index.js +6 -5
  48. package/build/internal/development.d.ts +1 -0
  49. package/build/internal/development.js +8 -0
  50. package/build/internal/export.d.ts +1 -0
  51. package/build/internal/export.js +80 -0
  52. package/build/internal/prebuild.d.ts +3 -0
  53. package/build/internal/prebuild.js +9 -0
  54. package/build/log.d.ts +4 -0
  55. package/build/log.js +6 -0
  56. package/build/modules/index.d.ts +2 -0
  57. package/build/modules/index.js +16 -0
  58. package/build/modules/modules.d.ts +42 -0
  59. package/build/modules/modules.js +84 -0
  60. package/build/modules/options.d.ts +10 -0
  61. package/build/modules/options.js +45 -0
  62. package/build/native/bare.d.ts +4 -0
  63. package/build/native/bare.js +130 -0
  64. package/build/native/build.d.ts +15 -0
  65. package/build/native/build.js +69 -0
  66. package/build/native/cache.d.ts +11 -0
  67. package/build/native/cache.js +73 -0
  68. package/build/native/hvigor.d.ts +3 -0
  69. package/build/native/hvigor.js +83 -0
  70. package/build/native/install.d.ts +5 -0
  71. package/build/native/install.js +13 -0
  72. package/build/native/prepareProject.d.ts +11 -0
  73. package/build/native/prepareProject.js +68 -0
  74. package/build/native/project.d.ts +8 -0
  75. package/build/native/project.js +58 -0
  76. package/build/native/toolchain.d.ts +16 -0
  77. package/build/native/toolchain.js +172 -0
  78. package/build/native/types.d.ts +47 -0
  79. package/build/native/types.js +2 -0
  80. package/build/path.d.ts +4 -0
  81. package/build/path.js +27 -0
  82. package/build/prebuild/check.d.ts +10 -0
  83. package/build/prebuild/check.js +151 -0
  84. package/build/prebuild/clean.d.ts +2 -0
  85. package/build/prebuild/clean.js +50 -0
  86. package/build/prebuild/index.d.ts +2 -0
  87. package/build/prebuild/index.js +51 -0
  88. package/build/prebuild/options.d.ts +10 -0
  89. package/build/prebuild/options.js +50 -0
  90. package/build/prebuild/prebuild.d.ts +6 -0
  91. package/build/prebuild/prebuild.js +70 -0
  92. package/build/prebuild/template.d.ts +8 -0
  93. package/build/prebuild/template.js +86 -0
  94. package/build/process.d.ts +41 -0
  95. package/build/process.js +324 -0
  96. package/build/profile.d.ts +1 -0
  97. package/build/profile.js +12 -0
  98. package/build/project.d.ts +2 -0
  99. package/build/project.js +23 -0
  100. package/build/projectLock.d.ts +2 -0
  101. package/build/projectLock.js +185 -0
  102. package/build/run/devices.d.ts +29 -0
  103. package/build/run/devices.js +223 -0
  104. package/build/run/emulators.d.ts +13 -0
  105. package/build/run/emulators.js +82 -0
  106. package/build/run/index.d.ts +2 -0
  107. package/build/run/index.js +25 -0
  108. package/build/run/metro.d.ts +21 -0
  109. package/build/run/metro.js +162 -0
  110. package/build/run/options.d.ts +14 -0
  111. package/build/run/options.js +62 -0
  112. package/build/run/run.d.ts +46 -0
  113. package/build/run/run.js +135 -0
  114. package/build/runtime/config.d.ts +37 -0
  115. package/build/runtime/config.js +169 -0
  116. package/build/runtime/contract.d.ts +5 -0
  117. package/build/runtime/contract.js +109 -0
  118. package/build/runtime/fingerprint.d.ts +1 -0
  119. package/build/runtime/fingerprint.js +49 -0
  120. package/build/runtime/index.d.ts +2 -0
  121. package/build/runtime/index.js +50 -0
  122. package/build/start/index.d.ts +2 -0
  123. package/build/start/index.js +38 -0
  124. package/build/start/options.d.ts +9 -0
  125. package/build/start/options.js +38 -0
  126. package/build/updates/export.d.ts +14 -0
  127. package/build/updates/export.js +46 -0
  128. package/build/updates/index.d.ts +2 -0
  129. package/build/updates/index.js +96 -0
  130. package/build/upstream.d.ts +9 -0
  131. package/build/upstream.js +17 -0
  132. package/package.json +21 -5
  133. package/src/bin/expo-harmony.ts +2 -2
  134. package/src/buildHap/build.ts +13 -105
  135. package/src/buildHap/index.ts +19 -0
  136. package/src/cli.ts +27 -225
  137. package/src/command.ts +24 -0
  138. package/src/development/manifest.ts +127 -0
  139. package/src/development/protocol.ts +42 -0
  140. package/src/development/session.ts +83 -0
  141. package/src/doctor/doctor.ts +67 -62
  142. package/src/doctor/index.ts +16 -0
  143. package/src/exportEmbed/export.ts +73 -72
  144. package/src/exportEmbed/index.ts +17 -0
  145. package/src/file.ts +20 -11
  146. package/src/help.ts +41 -0
  147. package/src/index.ts +5 -3
  148. package/src/internal/development.ts +8 -0
  149. package/src/internal/export.ts +82 -0
  150. package/src/internal/prebuild.ts +3 -0
  151. package/src/log.ts +5 -0
  152. package/src/modules/index.ts +16 -0
  153. package/src/native/bare.ts +145 -0
  154. package/src/native/build.ts +83 -0
  155. package/src/{run → native}/cache.ts +10 -9
  156. package/src/native/hvigor.ts +88 -0
  157. package/src/native/install.ts +17 -0
  158. package/src/native/prepareProject.ts +99 -0
  159. package/src/native/project.ts +88 -0
  160. package/src/{tools.ts → native/toolchain.ts} +38 -140
  161. package/src/native/types.ts +44 -0
  162. package/src/prebuild/check.ts +56 -31
  163. package/src/prebuild/clean.ts +3 -3
  164. package/src/prebuild/index.ts +56 -0
  165. package/src/prebuild/prebuild.ts +11 -1
  166. package/src/prebuild/template.ts +2 -2
  167. package/src/process.ts +175 -64
  168. package/src/profile.ts +13 -0
  169. package/src/projectLock.ts +9 -1
  170. package/src/run/devices.ts +24 -11
  171. package/src/run/emulators.ts +4 -6
  172. package/src/run/index.ts +25 -0
  173. package/src/run/metro.ts +10 -1
  174. package/src/run/options.ts +2 -0
  175. package/src/run/run.ts +63 -129
  176. package/src/runtime/config.ts +207 -0
  177. package/src/runtime/contract.ts +167 -0
  178. package/src/runtime/fingerprint.ts +49 -0
  179. package/src/runtime/index.ts +54 -0
  180. package/src/start/index.ts +37 -0
  181. package/src/start/options.ts +12 -0
  182. package/src/updates/export.ts +60 -0
  183. package/src/updates/index.ts +89 -0
  184. package/src/buildHap/common.ts +0 -145
  185. package/src/run/install.ts +0 -41
@@ -0,0 +1,223 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.configureMetroPortAsync = configureMetroPortAsync;
4
+ exports.installHapAsync = installHapAsync;
5
+ exports.launchAppAsync = launchAppAsync;
6
+ exports.selectDeviceAsync = selectDeviceAsync;
7
+ const promises_1 = require("node:timers/promises");
8
+ const emulators_1 = require("./emulators");
9
+ const errors_1 = require("../errors");
10
+ const process_1 = require("../process");
11
+ function parseHdcTargets(output) {
12
+ const trimmed = String(output).trim();
13
+ if (!trimmed || trimmed === '[Empty]')
14
+ return [];
15
+ return trimmed.split(/\r?\n/u).filter(Boolean).map((line) => {
16
+ const fields = line.trim().split(/\s+/u);
17
+ if (fields.length < 3 || !fields[0]) {
18
+ throw new errors_1.HarmonyCliError('ERR_HARMONY_DEVICE_OUTPUT', 'HDC returned an unsupported target-list format.', { operation: 'list-devices' });
19
+ }
20
+ return {
21
+ aliases: [fields[0]],
22
+ connectTool: fields[4] || null,
23
+ id: fields[0],
24
+ location: fields[3] || null,
25
+ state: fields[2],
26
+ transport: fields[1],
27
+ };
28
+ });
29
+ }
30
+ function hasCommandFailure(result) {
31
+ const output = `${result.stdout || ''}\n${result.stderr || ''}`;
32
+ return result.code !== 0 || result.timedOut
33
+ // HDC can print failures to stdout while returning exit code 0.
34
+ || /\[(?:Fail|Error)\]|\b(?:Failure|failed)\b|(?:失败|错误)/iu.test(output);
35
+ }
36
+ async function runHdcAsync(hdc, args, options = {}) {
37
+ const result = await (0, process_1.spawnAsync)(hdc.command, [
38
+ ...hdc.args,
39
+ ...args,
40
+ ], {
41
+ capture: true,
42
+ cwd: options.cwd,
43
+ operation: options.operation || 'hdc',
44
+ outputLimit: options.outputLimit || 256 * 1024,
45
+ timeoutMs: options.timeoutMs || 60_000,
46
+ });
47
+ if (!options.allowFailure && hasCommandFailure(result)) {
48
+ const diagnostics = (0, process_1.formatDiagnostics)(result, 2_000);
49
+ throw new errors_1.HarmonyCliError(options.code || 'ERR_HARMONY_DEVICE_COMMAND', `${options.message || 'HDC command failed'}${diagnostics ? `: ${diagnostics}` : '.'}`, { exitCode: result.code || 1, operation: options.operation || 'hdc' });
50
+ }
51
+ return result;
52
+ }
53
+ async function listConnectedDevicesAsync(hdc, options) {
54
+ const result = await runHdcAsync(hdc, ['list', 'targets', '-v'], {
55
+ code: 'ERR_HARMONY_DEVICE_LIST',
56
+ cwd: options.cwd,
57
+ message: 'Cannot list Harmony devices',
58
+ operation: 'list-devices',
59
+ timeoutMs: options.timeoutMs || 15_000,
60
+ });
61
+ const targets = parseHdcTargets(result.stdout || result.stderr);
62
+ return targets.filter(target => target.state.toLowerCase() === 'connected');
63
+ }
64
+ async function selectDeviceAsync(hdc, requested, options = {}) {
65
+ if (requested !== undefined && (!requested || /[\0\r\n]/u.test(requested) || requested.trim() !== requested)) {
66
+ throw new errors_1.HarmonyCliError('ERR_HARMONY_CONFIG_INVALID', '--device must be a non-empty HDC id or exact emulator name.', { operation: 'select-device' });
67
+ }
68
+ const connected = await listConnectedDevicesAsync(hdc, { cwd: options.cwd });
69
+ if (requested) {
70
+ const matches = connected.filter(target => target.id === requested || target.aliases.includes(requested));
71
+ if (matches.length === 1)
72
+ return matches[0];
73
+ if (matches.length > 1) {
74
+ throw new errors_1.HarmonyCliError('ERR_HARMONY_DEVICE_AMBIGUOUS', `The requested device name matches multiple connected targets: ${requested}.`, { operation: 'select-device' });
75
+ }
76
+ if (!options.emulator) {
77
+ throw new errors_1.HarmonyCliError('ERR_HARMONY_DEVICE_NOT_FOUND', `The requested Harmony device is not connected: ${requested}.`, { operation: 'select-device' });
78
+ }
79
+ }
80
+ else {
81
+ if (connected.length === 1)
82
+ return connected[0];
83
+ if (connected.length > 1) {
84
+ throw new errors_1.HarmonyCliError('ERR_HARMONY_DEVICE_AMBIGUOUS', `Multiple Harmony devices are connected (${connected.map(target => target.id).join(', ')}); use --device.`, { operation: 'select-device' });
85
+ }
86
+ }
87
+ if (!options.emulator) {
88
+ throw new errors_1.HarmonyCliError('ERR_HARMONY_DEVICE_NOT_FOUND', 'No connected Harmony device was reported by HDC.', { operation: 'select-device' });
89
+ }
90
+ const instances = await (0, emulators_1.listEmulatorsAsync)(options.emulator, { cwd: options.cwd });
91
+ const running = instances.filter(instance => instance.running);
92
+ const candidates = requested
93
+ ? instances.filter(instance => instance.name === requested)
94
+ : running.length ? running : instances;
95
+ if (candidates.length === 0) {
96
+ throw new errors_1.HarmonyCliError('ERR_HARMONY_DEVICE_NOT_FOUND', requested
97
+ ? `No connected HDC target or local emulator matches ${requested}. Available emulators: ${instances.map(instance => instance.name).join(', ') || 'none'}.`
98
+ : 'No connected Harmony device or local emulator was found. Create an emulator in DevEco Studio Device Manager first.', { operation: 'select-device' });
99
+ }
100
+ if (candidates.length > 1) {
101
+ throw new errors_1.HarmonyCliError('ERR_HARMONY_DEVICE_AMBIGUOUS', `Multiple Harmony emulators are available (${candidates.map(instance => JSON.stringify(instance.name)).join(', ')}); use --device <emulator-name>.`, { operation: 'select-device' });
102
+ }
103
+ const instance = candidates[0];
104
+ let launch;
105
+ if (!instance.running) {
106
+ if (!options.emulatorLogFile) {
107
+ throw new errors_1.HarmonyCliError('ERR_HARMONY_CONFIG_INVALID', 'An emulator log file is required to start a Harmony emulator.', { operation: 'start-emulator' });
108
+ }
109
+ options.onProgress?.(`Starting Harmony emulator ${instance.name}`);
110
+ launch = (0, emulators_1.startEmulator)(options.emulator, instance.name, options.emulatorLogFile, options.cwd);
111
+ }
112
+ options.onProgress?.(`Waiting for Harmony emulator ${instance.name} to connect`);
113
+ const deadline = Date.now() + (options.timeoutMs || 120_000);
114
+ while (Date.now() < deadline) {
115
+ launch?.assertRunning();
116
+ const devices = await listConnectedDevicesAsync(hdc, {
117
+ cwd: options.cwd,
118
+ timeoutMs: Math.max(1, Math.min(15_000, deadline - Date.now())),
119
+ });
120
+ for (const device of devices) {
121
+ if (Date.now() >= deadline)
122
+ break;
123
+ // DevEco may report hw.hdc.port as "notset" even while running. Query
124
+ // the guest's instance name instead of guessing from a newly seen ID.
125
+ if (!/^(?:127\.0\.0\.1|localhost|\[::1\]):\d+$/u.test(device.id))
126
+ continue;
127
+ const identity = await runHdcAsync(hdc, ['-t', device.id, 'shell', 'param', 'get', 'ohos.qemu.hvd.name'], {
128
+ allowFailure: true,
129
+ cwd: options.cwd,
130
+ operation: 'identify-emulator',
131
+ timeoutMs: Math.max(1, Math.min(5_000, deadline - Date.now())),
132
+ });
133
+ if (identity.code !== 0 || identity.timedOut || identity.stdout.trim() !== instance.name)
134
+ continue;
135
+ if (Date.now() >= deadline)
136
+ break;
137
+ const boot = await runHdcAsync(hdc, ['-t', device.id, 'shell', 'param', 'get', 'bootevent.boot.completed'], {
138
+ allowFailure: true,
139
+ cwd: options.cwd,
140
+ operation: 'wait-emulator-boot',
141
+ timeoutMs: Math.max(1, Math.min(5_000, deadline - Date.now())),
142
+ });
143
+ if (!hasCommandFailure(boot) && boot.stdout.trim() === 'true')
144
+ return device;
145
+ }
146
+ if (Date.now() >= deadline)
147
+ break;
148
+ await (0, promises_1.setTimeout)(Math.min(1_000, deadline - Date.now()));
149
+ }
150
+ throw new errors_1.HarmonyCliError('ERR_HARMONY_EMULATOR_TIMEOUT', `Timed out waiting for Harmony emulator ${instance.name} to connect to HDC.${launch ? ` See ${options.emulatorLogFile}.` : ''} Try starting it in DevEco Studio to resolve any license or login requirements.`, { operation: 'start-emulator' });
151
+ }
152
+ async function installHapAsync(hdc, device, hap, options = {}) {
153
+ await runHdcAsync(hdc, ['-t', device.id, 'install', '-r', hap], {
154
+ code: 'ERR_HARMONY_INSTALL_FAILED',
155
+ cwd: options.cwd,
156
+ message: `Cannot install the Harmony HAP on ${device.id}`,
157
+ operation: 'install-hap',
158
+ timeoutMs: options.timeoutMs || 2 * 60_000,
159
+ });
160
+ }
161
+ async function configureMetroPortAsync(hdc, device, port, options = {}) {
162
+ if (options.devicePort === undefined && port !== 8081) {
163
+ // RNOH's default provider uses 8081; launcher manifests retain Metro's
164
+ // advertised port for the bundle, assets, HMR and inspector URLs.
165
+ await configureMetroPortAsync(hdc, device, port, { ...options, devicePort: 8081 });
166
+ await configureMetroPortAsync(hdc, device, port, { ...options, devicePort: port });
167
+ return;
168
+ }
169
+ const deviceEndpoint = `tcp:${options.devicePort || 8081}`;
170
+ const hostEndpoint = `tcp:${port}`;
171
+ const forwards = await runHdcAsync(hdc, ['-t', device.id, 'fport', 'ls'], {
172
+ cwd: options.cwd,
173
+ operation: 'list-metro-ports',
174
+ timeoutMs: 15_000,
175
+ });
176
+ for (const line of forwards.stdout.split(/\r?\n/u)) {
177
+ const [target, remote, local, direction] = line.trim().split(/\s+/u);
178
+ if (target !== device.id || remote !== deviceEndpoint || direction !== '[Reverse]')
179
+ continue;
180
+ if (local === hostEndpoint)
181
+ return;
182
+ await runHdcAsync(hdc, ['-t', device.id, 'fport', 'rm', remote, local], {
183
+ cwd: options.cwd,
184
+ operation: 'remove-metro-port',
185
+ timeoutMs: 15_000,
186
+ });
187
+ }
188
+ await runHdcAsync(hdc, ['-t', device.id, 'rport', deviceEndpoint, hostEndpoint], {
189
+ code: 'ERR_HARMONY_METRO_FORWARD',
190
+ cwd: options.cwd,
191
+ message: `Cannot reverse Metro port ${port} for ${device.id}`,
192
+ operation: 'reverse-metro-port',
193
+ timeoutMs: 15_000,
194
+ });
195
+ }
196
+ async function launchAppAsync(hdc, device, bundleName, abilityName, options = {}) {
197
+ await runHdcAsync(hdc, [
198
+ '-t', device.id,
199
+ 'shell',
200
+ 'aa',
201
+ 'force-stop',
202
+ bundleName,
203
+ ], {
204
+ allowFailure: true,
205
+ cwd: options.cwd,
206
+ operation: 'force-stop-app',
207
+ timeoutMs: 15_000,
208
+ });
209
+ await runHdcAsync(hdc, [
210
+ '-t', device.id,
211
+ 'shell',
212
+ 'aa',
213
+ 'start',
214
+ '-a', abilityName,
215
+ '-b', bundleName,
216
+ ], {
217
+ code: 'ERR_HARMONY_LAUNCH_FAILED',
218
+ cwd: options.cwd,
219
+ message: `Cannot launch ${bundleName} on ${device.id}`,
220
+ operation: 'launch-app',
221
+ timeoutMs: 30_000,
222
+ });
223
+ }
@@ -0,0 +1,13 @@
1
+ import { type HarmonyTool } from '../native/toolchain';
2
+ interface HarmonyEmulator {
3
+ name: string;
4
+ running: boolean;
5
+ }
6
+ declare function listEmulatorsAsync(tool: HarmonyTool, options?: {
7
+ cwd?: string;
8
+ timeoutMs?: number;
9
+ }): Promise<HarmonyEmulator[]>;
10
+ declare function startEmulator(tool: HarmonyTool, name: string, logFile: string, cwd?: string): {
11
+ assertRunning(): void;
12
+ };
13
+ export { listEmulatorsAsync, startEmulator };
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.listEmulatorsAsync = listEmulatorsAsync;
7
+ exports.startEmulator = startEmulator;
8
+ const node_fs_1 = __importDefault(require("node:fs"));
9
+ const node_path_1 = __importDefault(require("node:path"));
10
+ const cross_spawn_1 = __importDefault(require("cross-spawn"));
11
+ const errors_1 = require("../errors");
12
+ const process_1 = require("../process");
13
+ async function listEmulatorsAsync(tool, options = {}) {
14
+ let result;
15
+ try {
16
+ result = await (0, process_1.spawnAsync)(tool.command, [...tool.args, '-list', '-details'], {
17
+ capture: true,
18
+ cwd: options.cwd,
19
+ operation: 'list-emulators',
20
+ timeoutMs: options.timeoutMs || 15_000,
21
+ });
22
+ }
23
+ catch (cause) {
24
+ throw new errors_1.HarmonyCliError('ERR_HARMONY_EMULATOR_LIST', 'Cannot run Emulator. Install DevEco Studio 6.1.0 or newer, add tools/emulator to PATH, or set HARMONY_EMULATOR.', { cause, operation: 'list-emulators' });
25
+ }
26
+ if (result.code !== 0 || result.timedOut) {
27
+ throw new errors_1.HarmonyCliError('ERR_HARMONY_EMULATOR_LIST', `Cannot list Harmony emulators: ${(0, process_1.formatDiagnostics)(result, 2_000) || 'Emulator timed out or exited unsuccessfully.'}`, { operation: 'list-emulators' });
28
+ }
29
+ try {
30
+ const instances = JSON.parse(result.stdout);
31
+ if (!Array.isArray(instances))
32
+ throw new Error('Expected an array.');
33
+ return instances.map((instance) => {
34
+ if (!instance || typeof instance.name !== 'string' || !instance.name.trim()
35
+ || /[\0\r\n]/u.test(instance.name)
36
+ || ![true, false, 'true', 'false'].includes(instance.isRunning)) {
37
+ throw new Error('Invalid emulator instance.');
38
+ }
39
+ return {
40
+ name: instance.name,
41
+ running: instance.isRunning === true || instance.isRunning === 'true',
42
+ };
43
+ });
44
+ }
45
+ catch (cause) {
46
+ throw new errors_1.HarmonyCliError('ERR_HARMONY_EMULATOR_OUTPUT', 'Emulator returned an unsupported instance list. Use DevEco Studio 6.1.0 or newer with Emulator -list -details support.', { cause, operation: 'list-emulators' });
47
+ }
48
+ }
49
+ function startEmulator(tool, name, logFile, cwd) {
50
+ node_fs_1.default.mkdirSync(node_path_1.default.dirname(logFile), { recursive: true });
51
+ const log = node_fs_1.default.openSync(logFile, 'w', 0o600);
52
+ let failure = null;
53
+ try {
54
+ // A detached process with file output lets the emulator survive CLI exit.
55
+ const child = (0, cross_spawn_1.default)(tool.command, [...tool.args, '-start', name], {
56
+ cwd,
57
+ detached: true,
58
+ shell: false,
59
+ stdio: ['ignore', log, log],
60
+ windowsHide: false,
61
+ });
62
+ child.once('error', (cause) => {
63
+ failure = cause.message;
64
+ });
65
+ child.once('exit', (code, signal) => {
66
+ // Some launchers exit before the guest boots; HDC still has to confirm readiness.
67
+ if (code !== 0)
68
+ failure = signal ? `terminated by ${signal}` : `exited with code ${code}`;
69
+ });
70
+ child.unref();
71
+ }
72
+ finally {
73
+ node_fs_1.default.closeSync(log);
74
+ }
75
+ return {
76
+ assertRunning() {
77
+ if (failure) {
78
+ throw new errors_1.HarmonyCliError('ERR_HARMONY_EMULATOR_START', `Cannot start Harmony emulator ${name}: ${failure}. See ${logFile}. Try starting it in DevEco Studio to resolve any license or login requirements.`, { operation: 'start-emulator' });
79
+ }
80
+ },
81
+ };
82
+ }
@@ -0,0 +1,2 @@
1
+ import { type Command } from '../command';
2
+ export declare const command: Command;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.command = void 0;
4
+ const command_1 = require("../command");
5
+ const options_1 = require("./options");
6
+ const command = async (argv, io) => {
7
+ const args = (0, command_1.parseCommandArgs)(options_1.parseRunArgs, argv, io);
8
+ if (!args)
9
+ return 0;
10
+ const { projectRoot: root, options } = args;
11
+ const { runHarmonySessionAsync } = await import('./run.js');
12
+ const session = await runHarmonySessionAsync(root, {
13
+ ...options,
14
+ interactiveBundler: true,
15
+ io,
16
+ });
17
+ const { result } = session;
18
+ io.log(`Launched ${result.bundleName} on ${result.device.id} (${result.variant}).`);
19
+ if (session.metro.owner === 'started') {
20
+ io.log('\n› Logs for your project will appear below. Press Ctrl+C to exit.');
21
+ await session.metro.waitAsync();
22
+ }
23
+ return 0;
24
+ };
25
+ exports.command = command;
@@ -0,0 +1,21 @@
1
+ import { readDevelopmentSessionAsync } from '../development/session';
2
+ import { startManagedProcess } from '../process';
3
+ interface MetroOptions {
4
+ interactive?: boolean;
5
+ host?: string;
6
+ port: number;
7
+ privateKeyPath?: string;
8
+ readyTimeoutMs?: number;
9
+ resetCache?: boolean;
10
+ }
11
+ export interface MetroSession {
12
+ owner: 'existing' | 'started';
13
+ development?: Awaited<ReturnType<typeof readDevelopmentSessionAsync>>;
14
+ port: number;
15
+ process?: ReturnType<typeof startManagedProcess>;
16
+ stop(): Promise<unknown>;
17
+ waitAsync(): Promise<void>;
18
+ }
19
+ declare function requireExistingMetroAsync(port: number): Promise<MetroSession>;
20
+ declare function startExpoMetroAsync(projectRoot: string, options: MetroOptions): Promise<MetroSession>;
21
+ export { requireExistingMetroAsync, startExpoMetroAsync, };
@@ -0,0 +1,162 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.requireExistingMetroAsync = requireExistingMetroAsync;
7
+ exports.startExpoMetroAsync = startExpoMetroAsync;
8
+ const node_http_1 = __importDefault(require("node:http"));
9
+ const session_1 = require("../development/session");
10
+ const errors_1 = require("../errors");
11
+ const process_1 = require("../process");
12
+ const expo_1 = require("../expo");
13
+ function delay(milliseconds) {
14
+ return new Promise(resolve => setTimeout(resolve, milliseconds));
15
+ }
16
+ function probeMetroAsync(port, options = {}) {
17
+ const timeoutMs = options.timeoutMs || 750;
18
+ return new Promise((resolve) => {
19
+ let settled = false;
20
+ const finish = (value) => {
21
+ if (settled)
22
+ return;
23
+ settled = true;
24
+ resolve(value);
25
+ };
26
+ const request = node_http_1.default.get({
27
+ headers: { Accept: 'text/plain' },
28
+ host: options.host || '127.0.0.1',
29
+ path: '/status',
30
+ port,
31
+ }, (response) => {
32
+ let body = '';
33
+ response.setEncoding('utf8');
34
+ response.on('data', (chunk) => {
35
+ if (body.length < 1_024)
36
+ body += chunk;
37
+ });
38
+ response.on('end', () => finish(response.statusCode === 200 && body.trim() === 'packager-status:running'
39
+ ? 'metro'
40
+ : 'occupied'));
41
+ });
42
+ request.setTimeout(timeoutMs, () => {
43
+ request.destroy();
44
+ finish('occupied');
45
+ });
46
+ request.on('error', (error) => finish(error.code === 'ECONNREFUSED' || error.code === 'EHOSTUNREACH'
47
+ ? 'free'
48
+ : 'occupied'));
49
+ });
50
+ }
51
+ async function requireExistingMetroAsync(port) {
52
+ const status = await probeMetroAsync(port);
53
+ if (status === 'metro') {
54
+ return {
55
+ owner: 'existing',
56
+ port,
57
+ stop: async () => { },
58
+ waitAsync: async () => { },
59
+ };
60
+ }
61
+ const code = status === 'free' ? 'ERR_HARMONY_METRO_UNAVAILABLE' : 'ERR_HARMONY_METRO_PORT_IN_USE';
62
+ const message = status === 'free'
63
+ ? `No Metro server is running on port ${port}; start Expo Metro before using --no-bundler.`
64
+ : `Port ${port} is occupied by a process that is not a compatible Metro server.`;
65
+ throw new errors_1.HarmonyCliError(code, message, { operation: 'metro-probe' });
66
+ }
67
+ async function startExpoMetroAsync(projectRoot, options) {
68
+ const before = await probeMetroAsync(options.port);
69
+ if (before === 'metro') {
70
+ return {
71
+ development: await (0, session_1.readDevelopmentSessionAsync)(projectRoot, options.port, options.host),
72
+ owner: 'existing',
73
+ port: options.port,
74
+ stop: async () => { },
75
+ waitAsync: async () => { },
76
+ };
77
+ }
78
+ if (before === 'occupied') {
79
+ throw new errors_1.HarmonyCliError('ERR_HARMONY_METRO_PORT_IN_USE', `Port ${options.port} is occupied by a process that is not a compatible Metro server.`, { operation: 'metro-probe' });
80
+ }
81
+ const expo = (0, expo_1.resolveExpoCli)(projectRoot);
82
+ const managed = (0, process_1.startManagedProcess)(process.execPath, [
83
+ require.resolve('../internal/development'),
84
+ expo.cliPath,
85
+ 'start',
86
+ projectRoot,
87
+ '--dev-client',
88
+ '--port', String(options.port),
89
+ ...(options.privateKeyPath ? ['--private-key-path', options.privateKeyPath] : []),
90
+ ...(options.resetCache ? ['--clear'] : []),
91
+ ], {
92
+ cwd: projectRoot,
93
+ env: {
94
+ ...process.env,
95
+ EXPO_METRO_TARGET: 'harmony',
96
+ ...(options.host ? { REACT_NATIVE_PACKAGER_HOSTNAME: options.host } : {}),
97
+ },
98
+ operation: 'expo-metro',
99
+ outputLimit: 1024 * 1024,
100
+ stdio: options.interactive ? 'inherit' : 'pipe',
101
+ });
102
+ let exitResult = null;
103
+ let exitError = null;
104
+ managed.completion.then((result) => {
105
+ exitResult = result;
106
+ }, (error) => {
107
+ exitError = error;
108
+ });
109
+ const startedAt = Date.now();
110
+ const timeoutMs = options.readyTimeoutMs || 1800000;
111
+ try {
112
+ while (Date.now() - startedAt < timeoutMs) {
113
+ if (exitError instanceof errors_1.HarmonyCliError) {
114
+ throw new errors_1.HarmonyCliError(exitError.code, exitError.message, {
115
+ cause: exitError,
116
+ exitCode: exitError.exitCode,
117
+ operation: exitError.operation,
118
+ });
119
+ }
120
+ if (exitError) {
121
+ const failure = exitError;
122
+ throw new errors_1.HarmonyCliError(failure.code || 'ERR_HARMONY_METRO_EXITED', failure.message || 'Expo Metro failed before becoming ready.', { cause: exitError, exitCode: failure.exitCode, operation: failure.operation });
123
+ }
124
+ if (exitResult) {
125
+ const diagnostics = (0, process_1.formatDiagnostics)(exitResult);
126
+ throw new errors_1.HarmonyCliError('ERR_HARMONY_METRO_EXITED', `Expo Metro exited before becoming ready with code ${exitResult.code}.${diagnostics ? `\n${diagnostics}` : ''}`, { exitCode: exitResult.code || 1, operation: 'expo-metro' });
127
+ }
128
+ if (await probeMetroAsync(options.port) === 'metro') {
129
+ return {
130
+ development: await (0, session_1.readDevelopmentSessionAsync)(projectRoot, options.port, options.host),
131
+ owner: 'started',
132
+ port: options.port,
133
+ process: managed,
134
+ stop: () => managed.stop(),
135
+ async waitAsync() {
136
+ const result = await managed.completion;
137
+ if (managed.wasStopped())
138
+ return;
139
+ const interrupted = result.signal === 'SIGINT' || result.signal === 'SIGTERM';
140
+ if (result.code === 0 || interrupted)
141
+ return;
142
+ const diagnostics = (0, process_1.formatDiagnostics)(result);
143
+ throw new errors_1.HarmonyCliError('ERR_HARMONY_METRO_EXITED', `Expo Metro exited with code ${result.code}.${diagnostics ? `\n${diagnostics}` : ''}`, { exitCode: result.code || 1, operation: 'expo-metro' });
144
+ },
145
+ };
146
+ }
147
+ await delay(150);
148
+ }
149
+ throw new errors_1.HarmonyCliError('ERR_HARMONY_METRO_TIMEOUT', `Expo Metro did not become ready on port ${options.port} within ${timeoutMs}ms.`, { operation: 'expo-metro' });
150
+ }
151
+ catch (error) {
152
+ await managed.stop();
153
+ if (error instanceof errors_1.HarmonyCliError) {
154
+ throw new errors_1.HarmonyCliError(error.code, error.message, {
155
+ cause: error,
156
+ exitCode: error.exitCode,
157
+ operation: error.operation,
158
+ });
159
+ }
160
+ throw new errors_1.HarmonyCliError(error.code || 'ERR_HARMONY_METRO_EXITED', error.message || 'Expo Metro failed while waiting for the server to become ready.', { cause: error, exitCode: error.exitCode, operation: error.operation });
161
+ }
162
+ }
@@ -0,0 +1,14 @@
1
+ declare function parseRunArgs(argv: string[]): {
2
+ appId: string;
3
+ device: string;
4
+ help: boolean;
5
+ noBundler: boolean;
6
+ noInstall: boolean;
7
+ port: number;
8
+ privateKeyPath: string;
9
+ project: string;
10
+ resetCache: boolean;
11
+ sync: boolean;
12
+ variant: "debug" | "release";
13
+ };
14
+ export { parseRunArgs };
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseRunArgs = parseRunArgs;
4
+ const errors_1 = require("../errors");
5
+ const args_1 = require("../args");
6
+ const RunOptions = {
7
+ ...args_1.CommonOptions,
8
+ 'app-id': { type: 'string' },
9
+ 'device': { type: 'string' },
10
+ 'no-bundler': { type: 'boolean' },
11
+ 'no-install': { type: 'boolean' },
12
+ 'port': { type: 'string' },
13
+ 'private-key-path': { type: 'string' },
14
+ 'reset-cache': { type: 'boolean' },
15
+ 'sync': { type: 'boolean' },
16
+ 'variant': { type: 'string' },
17
+ };
18
+ function parseRunArgs(argv) {
19
+ const { positionals, values } = (0, args_1.parseArgs)(RunOptions, argv);
20
+ if (positionals.length > 1) {
21
+ throw new errors_1.HarmonyCliError('ERR_HARMONY_CONFIG_INVALID', `Unexpected run positional argument: ${positionals[1]}`, {
22
+ operation: 'parse-arguments',
23
+ });
24
+ }
25
+ const variant = values.variant || 'debug';
26
+ if (variant !== 'debug' && variant !== 'release') {
27
+ throw new errors_1.HarmonyCliError('ERR_HARMONY_CONFIG_INVALID', '--variant must be debug or release.', {
28
+ operation: 'parse-arguments',
29
+ });
30
+ }
31
+ const port = values.port === undefined ? 8081 : Number(values.port);
32
+ if (!Number.isInteger(port) || port < 1 || port > 65_535) {
33
+ throw new errors_1.HarmonyCliError('ERR_HARMONY_CONFIG_INVALID', '--port must be an integer between 1 and 65535.', {
34
+ operation: 'parse-arguments',
35
+ });
36
+ }
37
+ const appId = values['app-id'];
38
+ if (appId !== undefined && (!appId || appId.trim() !== appId || appId.includes('\0'))) {
39
+ throw new errors_1.HarmonyCliError('ERR_HARMONY_CONFIG_INVALID', '--app-id must be a non-empty Harmony bundle name.', {
40
+ operation: 'parse-arguments',
41
+ });
42
+ }
43
+ const device = values.device;
44
+ if (device !== undefined && (!device || device.trim() !== device || /[\0\r\n]/u.test(device))) {
45
+ throw new errors_1.HarmonyCliError('ERR_HARMONY_CONFIG_INVALID', '--device must be a non-empty HDC id or exact emulator name.', {
46
+ operation: 'parse-arguments',
47
+ });
48
+ }
49
+ return {
50
+ appId,
51
+ device,
52
+ help: Boolean(values.help),
53
+ noBundler: Boolean(values['no-bundler']),
54
+ noInstall: Boolean(values['no-install']),
55
+ port,
56
+ privateKeyPath: values['private-key-path'],
57
+ project: positionals[0],
58
+ resetCache: Boolean(values['reset-cache']),
59
+ sync: Boolean(values.sync),
60
+ variant: variant,
61
+ };
62
+ }
@@ -0,0 +1,46 @@
1
+ import { type MetroSession } from './metro';
2
+ import { type NativeBuildResult } from '../native/build';
3
+ export interface HarmonyRunOptions {
4
+ appId?: string;
5
+ device?: string;
6
+ io?: Pick<Console, 'error' | 'log' | 'warn'>;
7
+ noBundler?: boolean;
8
+ noInstall?: boolean;
9
+ port?: number;
10
+ privateKeyPath?: string;
11
+ resetCache?: boolean;
12
+ sync?: boolean;
13
+ variant?: 'debug' | 'release';
14
+ }
15
+ export interface HarmonyRunResult extends NativeBuildResult {
16
+ bundleName: string;
17
+ device: {
18
+ id: string;
19
+ transport: string;
20
+ };
21
+ installed: boolean;
22
+ launched: true;
23
+ metro: {
24
+ owner: 'disabled' | 'existing' | 'started';
25
+ port: number;
26
+ };
27
+ ok: true;
28
+ schemaVersion: 1;
29
+ steps: Record<string, number>;
30
+ variant: 'debug' | 'release';
31
+ }
32
+ interface HarmonyRunSessionOptions extends HarmonyRunOptions {
33
+ interactiveBundler?: boolean;
34
+ }
35
+ interface HarmonyRunSession {
36
+ metro: MetroSession | {
37
+ owner: 'disabled';
38
+ port: number;
39
+ stop(): Promise<void>;
40
+ waitAsync(): Promise<void>;
41
+ };
42
+ result: HarmonyRunResult;
43
+ }
44
+ declare function runHarmonySessionAsync(root: string, options?: HarmonyRunSessionOptions): Promise<HarmonyRunSession>;
45
+ declare function runHarmonyAsync(root: string, options?: HarmonyRunOptions): Promise<HarmonyRunResult>;
46
+ export { runHarmonyAsync, runHarmonySessionAsync };