@androperator/emulator 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.
- package/LICENSE +202 -0
- package/NOTICE +5 -0
- package/README.md +110 -0
- package/dist/adbClient.d.ts +10 -0
- package/dist/adbClient.js +15 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.js +112 -0
- package/dist/configuredAvds.d.ts +7 -0
- package/dist/configuredAvds.js +135 -0
- package/dist/constants.d.ts +5 -0
- package/dist/constants.js +5 -0
- package/dist/errors.d.ts +21 -0
- package/dist/errors.js +16 -0
- package/dist/hostRequirements.d.ts +9 -0
- package/dist/hostRequirements.js +28 -0
- package/dist/hostToolClient.d.ts +10 -0
- package/dist/hostToolClient.js +23 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +11 -0
- package/dist/lifecycle.d.ts +22 -0
- package/dist/lifecycle.js +209 -0
- package/dist/processRunner.d.ts +33 -0
- package/dist/processRunner.js +65 -0
- package/dist/runningEmulators.d.ts +8 -0
- package/dist/runningEmulators.js +55 -0
- package/dist/runtimeConfig.d.ts +15 -0
- package/dist/runtimeConfig.js +28 -0
- package/dist/types.d.ts +17 -0
- package/dist/types.js +2 -0
- package/dist/validation.d.ts +3 -0
- package/dist/validation.js +12 -0
- package/docs/provenance.md +47 -0
- package/package.json +50 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright 2026 Action Launcher Pty Ltd
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/NOTICE
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
androperator-emulator includes code extracted and adapted from Androperator
|
|
2
|
+
https://github.com/androperator/androperator
|
|
3
|
+
Source revision: 43210a51315ad9e0badd80311bf686596a46b154
|
|
4
|
+
Licensed under the Apache License, Version 2.0. See LICENSE.
|
|
5
|
+
Source paths, ported tests and modifications are recorded in docs/provenance.md.
|
package/README.md
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
# @androperator/emulator
|
|
2
|
+
|
|
3
|
+
Android SDK emulator lifecycle library and JSON CLI, extracted from Androperator.
|
|
4
|
+
Requires Node.js 24+, Android SDK tools (`adb`, `emulator`, `sdkmanager`,
|
|
5
|
+
`avdmanager`) and the Java runtime required by your SDK tools. macOS and Linux
|
|
6
|
+
are the initial targets. No Node runtime or native ADBuddy UI is bundled.
|
|
7
|
+
|
|
8
|
+
## Build and install locally
|
|
9
|
+
|
|
10
|
+
```sh
|
|
11
|
+
npm ci
|
|
12
|
+
npm run build
|
|
13
|
+
npm test
|
|
14
|
+
npm pack
|
|
15
|
+
npm install -g ./androperator-emulator-0.1.0.tgz
|
|
16
|
+
androperator-emulator --version
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
The package is not yet published. Consumers can install the packed archive without
|
|
20
|
+
this checkout or development dependencies. After publication, install with
|
|
21
|
+
`npm install -g @androperator/emulator`.
|
|
22
|
+
The executable remains `androperator-emulator`. See [provenance](docs/provenance.md)
|
|
23
|
+
for source history and [plan](docs/plan.md) for follow-on work.
|
|
24
|
+
|
|
25
|
+
## CLI
|
|
26
|
+
|
|
27
|
+
```sh
|
|
28
|
+
androperator-emulator list
|
|
29
|
+
androperator-emulator inspect My_AVD
|
|
30
|
+
androperator-emulator status
|
|
31
|
+
androperator-emulator download 'system-images;android-35;google_apis;arm64-v8a'
|
|
32
|
+
androperator-emulator create My_AVD \
|
|
33
|
+
--image 'system-images;android-35;google_apis;arm64-v8a' \
|
|
34
|
+
--profile pixel_7 --storage-size 12G
|
|
35
|
+
androperator-emulator start My_AVD --headless
|
|
36
|
+
androperator-emulator wait emulator-5554 --timeout-ms 180000
|
|
37
|
+
androperator-emulator stop My_AVD
|
|
38
|
+
androperator-emulator delete My_AVD
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
`--help` describes all commands. `--json` or `--output json` can appear before or
|
|
42
|
+
after a command; JSON is always the default. Each invocation writes exactly one
|
|
43
|
+
JSON document on stdout. SDK output is captured, not mixed into the response.
|
|
44
|
+
Exit codes: 0 success, 1 operation failure, 2 invalid command arguments.
|
|
45
|
+
|
|
46
|
+
```json
|
|
47
|
+
{"protocolVersion":1,"ok":true,"data":{"devices":[]}}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Failures have `ok: false` and `error: {code, message, details?}` instead of `data`.
|
|
51
|
+
The `--version` response identifies the package and machine protocol without SDK
|
|
52
|
+
access. ADBuddy can probe this command after finding the separately installed
|
|
53
|
+
executable; native discovery and installation UI are separate work.
|
|
54
|
+
|
|
55
|
+
Create requires explicit image, hardware profile and storage capacity. Existing
|
|
56
|
+
AVDs are refused unless `--replace` is supplied, which discards existing data.
|
|
57
|
+
Replacement and deletion refuse running AVDs and fail closed if ADB discovery
|
|
58
|
+
fails or an emulator is offline/unauthorized. These are point-in-time checks;
|
|
59
|
+
callers must serialize concurrent lifecycle changes for the same AVD.
|
|
60
|
+
|
|
61
|
+
Installed-image detection accepts both the legacy semicolon-and-pipe table and
|
|
62
|
+
the Android CLI shim's slash-separated package IDs and whitespace columns.
|
|
63
|
+
|
|
64
|
+
Downloads do not automatically accept SDK licenses. Add `--accept-licenses` to
|
|
65
|
+
`download` or `create` only to authorize `sdkmanager --licenses` acceptance for
|
|
66
|
+
all outstanding SDK licenses. Otherwise licenses must already be accepted.
|
|
67
|
+
|
|
68
|
+
`start` waits for registration (60 seconds) and both Android boot properties
|
|
69
|
+
(180 seconds). `stop` requests emulator shutdown; it does not wait for process
|
|
70
|
+
exit. Launch disables snapshot loading, but does not disable snapshot saving.
|
|
71
|
+
It does not change guest developer settings.
|
|
72
|
+
|
|
73
|
+
`storage <name> --storage-size 24G` updates the configured `/data` capacity only.
|
|
74
|
+
It does not resize existing userdata, guarantee free space, or impose a host disk
|
|
75
|
+
quota. Use a fresh AVD when changing capacity. Dynamic qcow2 allocation and
|
|
76
|
+
snapshot-free storage are follow-on capabilities.
|
|
77
|
+
|
|
78
|
+
## Library
|
|
79
|
+
|
|
80
|
+
```js
|
|
81
|
+
import { getDefaultRuntimeConfig, createAvd, listConfiguredAvds } from '@androperator/emulator';
|
|
82
|
+
const config = getDefaultRuntimeConfig();
|
|
83
|
+
await createAvd(config, {
|
|
84
|
+
name: 'My_AVD',
|
|
85
|
+
systemImage: 'system-images;android-35;google_apis;arm64-v8a',
|
|
86
|
+
deviceProfile: 'pixel_7',
|
|
87
|
+
dataPartitionSize: '12G',
|
|
88
|
+
});
|
|
89
|
+
console.log(await listConfiguredAvds(config));
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
The root export includes inspection, running discovery, installation, creation,
|
|
93
|
+
storage configuration, start/stop/delete, registration/boot waits, host-tool
|
|
94
|
+
checks and process/runtime contracts. `startAvd` is asynchronous and confirms
|
|
95
|
+
spawn only; use the wait functions to confirm readiness. SDK operation failures
|
|
96
|
+
use `{code, message, details?}`; filesystem and process errors may be native
|
|
97
|
+
Errors. Inject a `ProcessRunner` for tests or an `adb` callback for consumer
|
|
98
|
+
logging. The library contains no Androperator compatibility or provisioning policy.
|
|
99
|
+
|
|
100
|
+
Explicit runtime paths override defaults. CLI environment overrides are
|
|
101
|
+
`ADB_PATH`, `EMULATOR_PATH`, `SDKMANAGER_PATH`, `AVDMANAGER_PATH`. Discovery checks
|
|
102
|
+
`ANDROID_HOME`, `ANDROID_SDK_ROOT`, standard macOS/Linux SDK roots, then PATH.
|
|
103
|
+
AVDs use `ANDROID_AVD_HOME` or `~/.android/avd`; locator `.ini` entries with
|
|
104
|
+
`path` or `path.rel` can redirect each AVD's config. Other Android home conventions
|
|
105
|
+
are not yet supported. Export `ANDROID_AVD_HOME` consistently for those setups.
|
|
106
|
+
|
|
107
|
+
## Tests and releases
|
|
108
|
+
|
|
109
|
+
GitHub Actions builds, tests and checks a packed installation on Linux and macOS.
|
|
110
|
+
See [releasing](docs/releasing.md) for the first publication and subsequent releases.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { RuntimeConfig } from "./runtimeConfig.js";
|
|
2
|
+
export declare function runAdb(config: RuntimeConfig, args: string[], options?: {
|
|
3
|
+
timeoutMs?: number;
|
|
4
|
+
}): Promise<import("./processRunner.js").ProcessResult>;
|
|
5
|
+
export interface DeviceInfo {
|
|
6
|
+
serial: string;
|
|
7
|
+
state: string;
|
|
8
|
+
}
|
|
9
|
+
export declare function listDevices(config: RuntimeConfig): Promise<DeviceInfo[]>;
|
|
10
|
+
//# sourceMappingURL=adbClient.d.ts.map
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { ERROR_CODES } from "./errors.js";
|
|
2
|
+
export function runAdb(config, args, options) {
|
|
3
|
+
const deviceArgs = config.deviceId ? ["-s", config.deviceId, ...args] : args;
|
|
4
|
+
return config.adb ? config.adb(deviceArgs, options) : config.runner.run(config.adbPath, deviceArgs, options);
|
|
5
|
+
}
|
|
6
|
+
export async function listDevices(config) {
|
|
7
|
+
const result = await runAdb({ ...config, deviceId: undefined }, ["devices"]);
|
|
8
|
+
if (result.code !== 0 || !result.stdout.includes("List of devices attached")) {
|
|
9
|
+
throw { code: ERROR_CODES.ADB_QUERY_FAILED, message: result.stderr || "Failed to query adb devices; verify adb is available" };
|
|
10
|
+
}
|
|
11
|
+
return result.stdout.split("List of devices attached")[1].split("\n")
|
|
12
|
+
.map((line) => line.trim()).filter(Boolean)
|
|
13
|
+
.map((line) => { const [serial, state] = line.split(/\s+/); return { serial, state: state ?? "unknown" }; });
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=adbClient.js.map
|
package/dist/cli.d.ts
ADDED
package/dist/cli.js
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import { parseArgs } from "node:util";
|
|
4
|
+
import { getDefaultRuntimeConfig, listRunningEmulators, listConfiguredAvds, inspectConfiguredAvd, createAvd, startAvd, stopAvd, deleteAvd, waitForBootCompletion, waitForEmulatorRegistration, ensureSystemImageInstalled, setAvdDataPartitionSize } from "./index.js";
|
|
5
|
+
const commands = {
|
|
6
|
+
list: "list", inspect: "inspect <name>", status: "status",
|
|
7
|
+
download: "download <system-image> [--accept-licenses]",
|
|
8
|
+
create: "create <name> --image <system-image> --profile <hardware-profile> --storage-size <12G> [--replace] [--accept-licenses]",
|
|
9
|
+
start: "start <name> [--headless]", stop: "stop <name>", delete: "delete <name>",
|
|
10
|
+
wait: "wait <serial> [--timeout-ms <180000>]", storage: "storage <name> --storage-size <12G>",
|
|
11
|
+
};
|
|
12
|
+
function usage(message) {
|
|
13
|
+
throw { code: "USAGE", message, details: { commands, example: "androperator-emulator inspect My_AVD" } };
|
|
14
|
+
}
|
|
15
|
+
async function main() {
|
|
16
|
+
const { values, positionals } = parseArgs({ allowPositionals: true, strict: true, options: {
|
|
17
|
+
help: { type: "boolean", short: "h" }, version: { type: "boolean" }, json: { type: "boolean" },
|
|
18
|
+
output: { type: "string" }, image: { type: "string" }, profile: { type: "string" },
|
|
19
|
+
"storage-size": { type: "string" }, replace: { type: "boolean" },
|
|
20
|
+
"accept-licenses": { type: "boolean" }, headless: { type: "boolean" }, "timeout-ms": { type: "string" },
|
|
21
|
+
} });
|
|
22
|
+
if (values.output !== undefined && values.output !== "json")
|
|
23
|
+
usage("--output supports json; for example --output json list");
|
|
24
|
+
if (values.version) {
|
|
25
|
+
const { name, version } = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8"));
|
|
26
|
+
return { name, version, protocolVersion: 1 };
|
|
27
|
+
}
|
|
28
|
+
if (values.help)
|
|
29
|
+
return { commands, output: "One JSON envelope on stdout; exit 0 success, 1 operation failure, 2 usage error" };
|
|
30
|
+
const [command, target, ...extra] = positionals;
|
|
31
|
+
if (!Object.hasOwn(commands, command ?? ""))
|
|
32
|
+
usage(`Unknown or missing command: ${command ?? ""}; use --help`);
|
|
33
|
+
const needsTarget = !["list", "status"].includes(command);
|
|
34
|
+
if (extra.length || (needsTarget ? target === undefined || target.trim() === "" : target !== undefined))
|
|
35
|
+
usage(`Use ${commands[command]}`);
|
|
36
|
+
const allowed = {
|
|
37
|
+
create: ["image", "profile", "storage-size", "replace", "accept-licenses"],
|
|
38
|
+
download: ["accept-licenses"], start: ["headless"], wait: ["timeout-ms"], storage: ["storage-size"],
|
|
39
|
+
};
|
|
40
|
+
for (const flag of Object.keys(values)) {
|
|
41
|
+
if (!["json", "output", ...(allowed[command] ?? [])].includes(flag))
|
|
42
|
+
usage(`--${flag} is not valid for ${command}; use ${commands[command]}`);
|
|
43
|
+
}
|
|
44
|
+
function required(flag) {
|
|
45
|
+
const value = values[flag];
|
|
46
|
+
if (value === undefined || !value.trim())
|
|
47
|
+
usage(`--${flag} requires a nonblank value; use ${commands[command]}`);
|
|
48
|
+
return value;
|
|
49
|
+
}
|
|
50
|
+
const config = getDefaultRuntimeConfig();
|
|
51
|
+
switch (command) {
|
|
52
|
+
case "status": return { devices: await listRunningEmulators(config) };
|
|
53
|
+
case "list": {
|
|
54
|
+
const running = await listRunningEmulators(config);
|
|
55
|
+
return { avds: await listConfiguredAvds(config, new Set(running.map((item) => item.avdName))) };
|
|
56
|
+
}
|
|
57
|
+
case "inspect": {
|
|
58
|
+
const running = await listRunningEmulators(config);
|
|
59
|
+
return inspectConfiguredAvd(target, new Set(running.map((item) => item.avdName)));
|
|
60
|
+
}
|
|
61
|
+
case "download":
|
|
62
|
+
await ensureSystemImageInstalled(config, target, { acceptLicenses: values["accept-licenses"] });
|
|
63
|
+
return { systemImage: target, installed: true };
|
|
64
|
+
case "create":
|
|
65
|
+
await createAvd(config, { name: target, systemImage: required("image"), deviceProfile: required("profile"),
|
|
66
|
+
dataPartitionSize: required("storage-size"), replace: values.replace, acceptLicenses: values["accept-licenses"] });
|
|
67
|
+
return inspectConfiguredAvd(target);
|
|
68
|
+
case "start": {
|
|
69
|
+
if (!(await inspectConfiguredAvd(target)).exists)
|
|
70
|
+
throw { code: "EMULATOR_NOT_FOUND", message: `AVD ${target} not found; use list` };
|
|
71
|
+
if ((await listRunningEmulators(config)).some((item) => item.avdName === target))
|
|
72
|
+
throw { code: "EMULATOR_ALREADY_RUNNING", message: `AVD ${target} is already running; use status` };
|
|
73
|
+
await startAvd(config, target, values.headless ? ["-no-window", "-no-audio"] : []);
|
|
74
|
+
const serial = await waitForEmulatorRegistration(config, target);
|
|
75
|
+
await waitForBootCompletion(config, serial);
|
|
76
|
+
return { avdName: target, serial, booted: true };
|
|
77
|
+
}
|
|
78
|
+
case "wait": {
|
|
79
|
+
const timeout = values["timeout-ms"];
|
|
80
|
+
if (timeout !== undefined && (!/^[1-9]\d*$/.test(timeout) || !Number.isSafeInteger(Number(timeout))))
|
|
81
|
+
usage("--timeout-ms must be a positive integer, for example 180000");
|
|
82
|
+
if (!/^emulator-\d+$/.test(target))
|
|
83
|
+
usage("wait requires an emulator serial, for example emulator-5554");
|
|
84
|
+
await waitForBootCompletion(config, target, timeout === undefined ? undefined : Number(timeout));
|
|
85
|
+
return { serial: target, booted: true };
|
|
86
|
+
}
|
|
87
|
+
case "stop":
|
|
88
|
+
await stopAvd(config, target);
|
|
89
|
+
return { avdName: target, stopped: true };
|
|
90
|
+
case "delete":
|
|
91
|
+
await deleteAvd(config, target);
|
|
92
|
+
return { avdName: target, deleted: true };
|
|
93
|
+
case "storage":
|
|
94
|
+
await setAvdDataPartitionSize(target, required("storage-size"));
|
|
95
|
+
return { avdName: target, configured: true, resized: false };
|
|
96
|
+
default: return usage("Use --help");
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
try {
|
|
100
|
+
const data = await main();
|
|
101
|
+
console.log(JSON.stringify({ protocolVersion: 1, ok: true, data }));
|
|
102
|
+
}
|
|
103
|
+
catch (error) {
|
|
104
|
+
const value = error;
|
|
105
|
+
const isUsage = value.code === "USAGE" || value.code?.startsWith("ERR_PARSE_ARGS");
|
|
106
|
+
console.log(JSON.stringify({ protocolVersion: 1, ok: false, error: {
|
|
107
|
+
code: isUsage ? "USAGE" : value.code ?? "EMULATOR_ERROR",
|
|
108
|
+
message: value.message ?? String(error), ...(value.details === undefined ? {} : { details: value.details }),
|
|
109
|
+
} }));
|
|
110
|
+
process.exitCode = isUsage ? 2 : 1;
|
|
111
|
+
}
|
|
112
|
+
//# sourceMappingURL=cli.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { RuntimeConfig } from "./runtimeConfig.js";
|
|
2
|
+
import type { ConfiguredAvd } from "./types.js";
|
|
3
|
+
export declare function getAvdRoot(): string;
|
|
4
|
+
export declare function inspectConfiguredAvd(name: string, runningNames?: Set<string>): Promise<ConfiguredAvd>;
|
|
5
|
+
export declare function listConfiguredAvds(config: RuntimeConfig, runningNames?: Set<string>): Promise<ConfiguredAvd[]>;
|
|
6
|
+
export declare function getAvdConfigPath(name: string): Promise<string>;
|
|
7
|
+
//# sourceMappingURL=configuredAvds.d.ts.map
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
// Ported from Androperator; adaptations recorded in docs/provenance.md.
|
|
2
|
+
import { readFile, stat } from "node:fs/promises";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { join, isAbsolute, resolve, dirname } from "node:path";
|
|
5
|
+
import { runAndroidSdkTool } from "./hostToolClient.js";
|
|
6
|
+
import { validateAvdName } from "./validation.js";
|
|
7
|
+
export function getAvdRoot() {
|
|
8
|
+
const androidAvdHome = process.env.ANDROID_AVD_HOME;
|
|
9
|
+
if (typeof androidAvdHome === "string" && androidAvdHome.trim().length > 0) {
|
|
10
|
+
return androidAvdHome.trim();
|
|
11
|
+
}
|
|
12
|
+
return join(homedir(), ".android", "avd");
|
|
13
|
+
}
|
|
14
|
+
function parseIni(contents) {
|
|
15
|
+
const map = {};
|
|
16
|
+
for (const rawLine of contents.split("\n")) {
|
|
17
|
+
const line = rawLine.trim();
|
|
18
|
+
if (!line || line.startsWith("#")) {
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
const separatorIndex = line.indexOf("=");
|
|
22
|
+
if (separatorIndex <= 0) {
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
const key = line.slice(0, separatorIndex).trim();
|
|
26
|
+
const value = line.slice(separatorIndex + 1).trim();
|
|
27
|
+
map[key] = value;
|
|
28
|
+
}
|
|
29
|
+
return map;
|
|
30
|
+
}
|
|
31
|
+
function parseApiLevel(config, root) {
|
|
32
|
+
const target = config.target ?? root.target;
|
|
33
|
+
if (target) {
|
|
34
|
+
const match = target.match(/android-(\d+)/);
|
|
35
|
+
if (match) {
|
|
36
|
+
return Number(match[1]);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
const systemImage = config["image.sysdir.1"];
|
|
40
|
+
if (!systemImage) {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
const match = systemImage.match(/android-(\d+)/);
|
|
44
|
+
return match ? Number(match[1]) : null;
|
|
45
|
+
}
|
|
46
|
+
function parseSystemImage(config) {
|
|
47
|
+
const sysdir = config["image.sysdir.1"];
|
|
48
|
+
if (!sysdir) {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
return sysdir.replace(/\/+$/, "").replaceAll("/", ";");
|
|
52
|
+
}
|
|
53
|
+
function parsePlayStore(config) {
|
|
54
|
+
const explicit = config["PlayStore.enabled"]?.toLowerCase();
|
|
55
|
+
if (explicit === "true" || explicit === "yes") {
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
const tagId = config["tag.id"]?.toLowerCase();
|
|
59
|
+
if (tagId === "google_apis_playstore") {
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
const systemImage = config["image.sysdir.1"]?.toLowerCase();
|
|
63
|
+
return systemImage?.includes("google_apis_playstore") ?? false;
|
|
64
|
+
}
|
|
65
|
+
function parseDeviceProfile(config) {
|
|
66
|
+
return config["hw.device.name"] ?? null;
|
|
67
|
+
}
|
|
68
|
+
async function readIniIfPresent(path) {
|
|
69
|
+
try {
|
|
70
|
+
const contents = await readFile(path, "utf8");
|
|
71
|
+
return parseIni(contents);
|
|
72
|
+
}
|
|
73
|
+
catch (error) {
|
|
74
|
+
if (error.code !== "ENOENT")
|
|
75
|
+
throw error;
|
|
76
|
+
return {};
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
export async function inspectConfiguredAvd(name, runningNames = new Set()) {
|
|
80
|
+
const avdRoot = getAvdRoot();
|
|
81
|
+
validateAvdName(name);
|
|
82
|
+
const config = await readIniIfPresent(await getAvdConfigPath(name));
|
|
83
|
+
const root = await readIniIfPresent(join(avdRoot, `${name}.ini`));
|
|
84
|
+
const exists = await pathExists(join(avdRoot, `${name}.ini`)) || await pathExists(await getAvdConfigPath(name));
|
|
85
|
+
return {
|
|
86
|
+
name,
|
|
87
|
+
exists,
|
|
88
|
+
running: runningNames.has(name),
|
|
89
|
+
apiLevel: parseApiLevel(config, root),
|
|
90
|
+
abi: config["abi.type"] ?? null,
|
|
91
|
+
playStore: parsePlayStore(config),
|
|
92
|
+
deviceProfile: parseDeviceProfile(config),
|
|
93
|
+
systemImage: parseSystemImage(config),
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
export async function listConfiguredAvds(config, runningNames = new Set()) {
|
|
97
|
+
const result = await runAndroidSdkTool(config, "emulator", ["-list-avds"], { timeoutMs: 10_000 });
|
|
98
|
+
if (result.code !== 0) {
|
|
99
|
+
throw new Error(result.stderr || "Failed to list configured AVDs");
|
|
100
|
+
}
|
|
101
|
+
const names = result.stdout
|
|
102
|
+
.split("\n")
|
|
103
|
+
.map((line) => line.trim())
|
|
104
|
+
.filter((line) => line.length > 0);
|
|
105
|
+
const avds = [];
|
|
106
|
+
for (const name of names) {
|
|
107
|
+
avds.push(await inspectConfiguredAvd(name, runningNames));
|
|
108
|
+
}
|
|
109
|
+
return avds;
|
|
110
|
+
}
|
|
111
|
+
export async function getAvdConfigPath(name) {
|
|
112
|
+
validateAvdName(name);
|
|
113
|
+
const avdRoot = getAvdRoot();
|
|
114
|
+
const root = await readIniIfPresent(join(avdRoot, `${name}.ini`));
|
|
115
|
+
if (root.path) {
|
|
116
|
+
if (!isAbsolute(root.path))
|
|
117
|
+
throw new Error(`AVD ${name} has a non-absolute path entry`);
|
|
118
|
+
return join(root.path, "config.ini");
|
|
119
|
+
}
|
|
120
|
+
if (root["path.rel"])
|
|
121
|
+
return join(resolve(dirname(avdRoot), root["path.rel"]), "config.ini");
|
|
122
|
+
return join(avdRoot, `${name}.avd`, "config.ini");
|
|
123
|
+
}
|
|
124
|
+
async function pathExists(path) {
|
|
125
|
+
try {
|
|
126
|
+
await stat(path);
|
|
127
|
+
return true;
|
|
128
|
+
}
|
|
129
|
+
catch (error) {
|
|
130
|
+
if (error.code === "ENOENT")
|
|
131
|
+
return false;
|
|
132
|
+
throw error;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
//# sourceMappingURL=configuredAvds.js.map
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare const EMULATOR_DATA_PARTITION_SIZE_PATTERN: RegExp;
|
|
2
|
+
export declare const EMULATOR_BOOT_TIMEOUT_MS = 180000;
|
|
3
|
+
export declare const ADB_REGISTRATION_TIMEOUT_MS = 60000;
|
|
4
|
+
export declare const BOOT_POLL_INTERVAL_MS = 2000;
|
|
5
|
+
//# sourceMappingURL=constants.d.ts.map
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export declare const ERROR_CODES: {
|
|
2
|
+
readonly ADB_QUERY_FAILED: "ADB_QUERY_FAILED";
|
|
3
|
+
readonly ANDROID_AVD_CREATE_FAILED: "ANDROID_AVD_CREATE_FAILED";
|
|
4
|
+
readonly ANDROID_SDK_TOOL_MISSING: "ANDROID_SDK_TOOL_MISSING";
|
|
5
|
+
readonly ANDROID_SYSTEM_IMAGE_INSTALL_FAILED: "ANDROID_SYSTEM_IMAGE_INSTALL_FAILED";
|
|
6
|
+
readonly EMULATOR_ALREADY_RUNNING: "EMULATOR_ALREADY_RUNNING";
|
|
7
|
+
readonly EMULATOR_BOOT_TIMEOUT: "EMULATOR_BOOT_TIMEOUT";
|
|
8
|
+
readonly EMULATOR_DELETE_FAILED: "EMULATOR_DELETE_FAILED";
|
|
9
|
+
readonly EMULATOR_NOT_FOUND: "EMULATOR_NOT_FOUND";
|
|
10
|
+
readonly EMULATOR_NOT_RUNNING: "EMULATOR_NOT_RUNNING";
|
|
11
|
+
readonly EMULATOR_START_FAILED: "EMULATOR_START_FAILED";
|
|
12
|
+
readonly EMULATOR_STOP_FAILED: "EMULATOR_STOP_FAILED";
|
|
13
|
+
readonly INVALID_ARGUMENT: "INVALID_ARGUMENT";
|
|
14
|
+
readonly ANDROID_AVD_ALREADY_EXISTS: "ANDROID_AVD_ALREADY_EXISTS";
|
|
15
|
+
};
|
|
16
|
+
export interface EmulatorError {
|
|
17
|
+
code: (typeof ERROR_CODES)[keyof typeof ERROR_CODES];
|
|
18
|
+
message: string;
|
|
19
|
+
details?: Record<string, unknown>;
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=errors.d.ts.map
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export const ERROR_CODES = {
|
|
2
|
+
ADB_QUERY_FAILED: "ADB_QUERY_FAILED",
|
|
3
|
+
ANDROID_AVD_CREATE_FAILED: "ANDROID_AVD_CREATE_FAILED",
|
|
4
|
+
ANDROID_SDK_TOOL_MISSING: "ANDROID_SDK_TOOL_MISSING",
|
|
5
|
+
ANDROID_SYSTEM_IMAGE_INSTALL_FAILED: "ANDROID_SYSTEM_IMAGE_INSTALL_FAILED",
|
|
6
|
+
EMULATOR_ALREADY_RUNNING: "EMULATOR_ALREADY_RUNNING",
|
|
7
|
+
EMULATOR_BOOT_TIMEOUT: "EMULATOR_BOOT_TIMEOUT",
|
|
8
|
+
EMULATOR_DELETE_FAILED: "EMULATOR_DELETE_FAILED",
|
|
9
|
+
EMULATOR_NOT_FOUND: "EMULATOR_NOT_FOUND",
|
|
10
|
+
EMULATOR_NOT_RUNNING: "EMULATOR_NOT_RUNNING",
|
|
11
|
+
EMULATOR_START_FAILED: "EMULATOR_START_FAILED",
|
|
12
|
+
EMULATOR_STOP_FAILED: "EMULATOR_STOP_FAILED",
|
|
13
|
+
INVALID_ARGUMENT: "INVALID_ARGUMENT",
|
|
14
|
+
ANDROID_AVD_ALREADY_EXISTS: "ANDROID_AVD_ALREADY_EXISTS",
|
|
15
|
+
};
|
|
16
|
+
//# sourceMappingURL=errors.js.map
|