appium 2.0.0-beta.7 → 2.0.0-beta.71

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 (206) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +149 -58
  3. package/build/lib/appium.d.ts +229 -0
  4. package/build/lib/appium.d.ts.map +1 -0
  5. package/build/lib/appium.js +678 -439
  6. package/build/lib/appium.js.map +1 -0
  7. package/build/lib/cli/args.d.ts +17 -0
  8. package/build/lib/cli/args.d.ts.map +1 -0
  9. package/build/lib/cli/args.js +263 -300
  10. package/build/lib/cli/args.js.map +1 -0
  11. package/build/lib/cli/driver-command.d.ts +102 -0
  12. package/build/lib/cli/driver-command.d.ts.map +1 -0
  13. package/build/lib/cli/driver-command.js +131 -81
  14. package/build/lib/cli/driver-command.js.map +1 -0
  15. package/build/lib/cli/extension-command.d.ts +402 -0
  16. package/build/lib/cli/extension-command.d.ts.map +1 -0
  17. package/build/lib/cli/extension-command.js +799 -383
  18. package/build/lib/cli/extension-command.js.map +1 -0
  19. package/build/lib/cli/extension.d.ts +23 -0
  20. package/build/lib/cli/extension.d.ts.map +1 -0
  21. package/build/lib/cli/extension.js +71 -60
  22. package/build/lib/cli/extension.js.map +1 -0
  23. package/build/lib/cli/parser.d.ts +84 -0
  24. package/build/lib/cli/parser.d.ts.map +1 -0
  25. package/build/lib/cli/parser.js +252 -148
  26. package/build/lib/cli/parser.js.map +1 -0
  27. package/build/lib/cli/plugin-command.d.ts +99 -0
  28. package/build/lib/cli/plugin-command.d.ts.map +1 -0
  29. package/build/lib/cli/plugin-command.js +125 -81
  30. package/build/lib/cli/plugin-command.js.map +1 -0
  31. package/build/lib/cli/utils.d.ts +29 -0
  32. package/build/lib/cli/utils.d.ts.map +1 -0
  33. package/build/lib/cli/utils.js +72 -51
  34. package/build/lib/cli/utils.js.map +1 -0
  35. package/build/lib/config-file.d.ts +100 -0
  36. package/build/lib/config-file.d.ts.map +1 -0
  37. package/build/lib/config-file.js +207 -0
  38. package/build/lib/config-file.js.map +1 -0
  39. package/build/lib/config.d.ts +49 -0
  40. package/build/lib/config.d.ts.map +1 -0
  41. package/build/lib/config.js +262 -223
  42. package/build/lib/config.js.map +1 -0
  43. package/build/lib/constants.d.ts +56 -0
  44. package/build/lib/constants.d.ts.map +1 -0
  45. package/build/lib/constants.js +73 -0
  46. package/build/lib/constants.js.map +1 -0
  47. package/build/lib/extension/driver-config.d.ts +82 -0
  48. package/build/lib/extension/driver-config.d.ts.map +1 -0
  49. package/build/lib/extension/driver-config.js +210 -0
  50. package/build/lib/extension/driver-config.js.map +1 -0
  51. package/build/lib/extension/extension-config.d.ts +270 -0
  52. package/build/lib/extension/extension-config.d.ts.map +1 -0
  53. package/build/lib/extension/extension-config.js +601 -0
  54. package/build/lib/extension/extension-config.js.map +1 -0
  55. package/build/lib/extension/index.d.ts +48 -0
  56. package/build/lib/extension/index.d.ts.map +1 -0
  57. package/build/lib/extension/index.js +105 -0
  58. package/build/lib/extension/index.js.map +1 -0
  59. package/build/lib/extension/manifest-migrations.d.ts +27 -0
  60. package/build/lib/extension/manifest-migrations.d.ts.map +1 -0
  61. package/build/lib/extension/manifest-migrations.js +134 -0
  62. package/build/lib/extension/manifest-migrations.js.map +1 -0
  63. package/build/lib/extension/manifest.d.ts +145 -0
  64. package/build/lib/extension/manifest.d.ts.map +1 -0
  65. package/build/lib/extension/manifest.js +528 -0
  66. package/build/lib/extension/manifest.js.map +1 -0
  67. package/build/lib/extension/package-changed.d.ts +11 -0
  68. package/build/lib/extension/package-changed.d.ts.map +1 -0
  69. package/build/lib/extension/package-changed.js +62 -0
  70. package/build/lib/extension/package-changed.js.map +1 -0
  71. package/build/lib/extension/plugin-config.d.ts +56 -0
  72. package/build/lib/extension/plugin-config.d.ts.map +1 -0
  73. package/build/lib/extension/plugin-config.js +102 -0
  74. package/build/lib/extension/plugin-config.js.map +1 -0
  75. package/build/lib/grid-register.d.ts +10 -0
  76. package/build/lib/grid-register.d.ts.map +1 -0
  77. package/build/lib/grid-register.js +122 -144
  78. package/build/lib/grid-register.js.map +1 -0
  79. package/build/lib/logger.d.ts +3 -0
  80. package/build/lib/logger.d.ts.map +1 -0
  81. package/build/lib/logger.js +5 -17
  82. package/build/lib/logger.js.map +1 -0
  83. package/build/lib/logsink.d.ts +4 -0
  84. package/build/lib/logsink.d.ts.map +1 -0
  85. package/build/lib/logsink.js +189 -184
  86. package/build/lib/logsink.js.map +1 -0
  87. package/build/lib/main.d.ts +62 -0
  88. package/build/lib/main.d.ts.map +1 -0
  89. package/build/lib/main.js +388 -234
  90. package/build/lib/main.js.map +1 -0
  91. package/build/lib/schema/arg-spec.d.ts +143 -0
  92. package/build/lib/schema/arg-spec.d.ts.map +1 -0
  93. package/build/lib/schema/arg-spec.js +164 -0
  94. package/build/lib/schema/arg-spec.js.map +1 -0
  95. package/build/lib/schema/cli-args.d.ts +19 -0
  96. package/build/lib/schema/cli-args.d.ts.map +1 -0
  97. package/build/lib/schema/cli-args.js +220 -0
  98. package/build/lib/schema/cli-args.js.map +1 -0
  99. package/build/lib/schema/cli-transformers.d.ts +5 -0
  100. package/build/lib/schema/cli-transformers.d.ts.map +1 -0
  101. package/build/lib/schema/cli-transformers.js +124 -0
  102. package/build/lib/schema/cli-transformers.js.map +1 -0
  103. package/build/lib/schema/index.d.ts +3 -0
  104. package/build/lib/schema/index.d.ts.map +1 -0
  105. package/build/lib/schema/index.js +19 -0
  106. package/build/lib/schema/index.js.map +1 -0
  107. package/build/lib/schema/keywords.d.ts +24 -0
  108. package/build/lib/schema/keywords.d.ts.map +1 -0
  109. package/build/lib/schema/keywords.js +128 -0
  110. package/build/lib/schema/keywords.js.map +1 -0
  111. package/build/lib/schema/schema.d.ts +260 -0
  112. package/build/lib/schema/schema.d.ts.map +1 -0
  113. package/build/lib/schema/schema.js +640 -0
  114. package/build/lib/schema/schema.js.map +1 -0
  115. package/build/lib/utils.d.ts +266 -0
  116. package/build/lib/utils.d.ts.map +1 -0
  117. package/build/lib/utils.js +349 -273
  118. package/build/lib/utils.js.map +1 -0
  119. package/build/types/cli.d.ts +134 -0
  120. package/build/types/cli.d.ts.map +1 -0
  121. package/build/types/cli.js +3 -0
  122. package/build/types/cli.js.map +1 -0
  123. package/build/types/index.d.ts +15 -0
  124. package/build/types/index.d.ts.map +1 -0
  125. package/build/types/index.js +19 -0
  126. package/build/types/index.js.map +1 -0
  127. package/build/types/manifest/base.d.ts +135 -0
  128. package/build/types/manifest/base.d.ts.map +1 -0
  129. package/build/types/manifest/base.js +3 -0
  130. package/build/types/manifest/base.js.map +1 -0
  131. package/build/types/manifest/index.d.ts +21 -0
  132. package/build/types/manifest/index.d.ts.map +1 -0
  133. package/build/types/manifest/index.js +42 -0
  134. package/build/types/manifest/index.js.map +1 -0
  135. package/build/types/manifest/v3.d.ts +139 -0
  136. package/build/types/manifest/v3.d.ts.map +1 -0
  137. package/build/types/manifest/v3.js +3 -0
  138. package/build/types/manifest/v3.js.map +1 -0
  139. package/build/types/manifest/v4.d.ts +139 -0
  140. package/build/types/manifest/v4.d.ts.map +1 -0
  141. package/build/types/manifest/v4.js +3 -0
  142. package/build/types/manifest/v4.js.map +1 -0
  143. package/driver.d.ts +1 -0
  144. package/driver.js +14 -0
  145. package/index.js +11 -0
  146. package/lib/appium.js +558 -186
  147. package/lib/cli/args.js +275 -407
  148. package/lib/cli/driver-command.js +132 -24
  149. package/lib/cli/extension-command.js +751 -272
  150. package/lib/cli/extension.js +47 -20
  151. package/lib/cli/parser.js +267 -95
  152. package/lib/cli/plugin-command.js +122 -22
  153. package/lib/cli/utils.js +24 -10
  154. package/lib/config-file.js +220 -0
  155. package/lib/config.js +243 -132
  156. package/lib/constants.js +79 -0
  157. package/lib/extension/driver-config.js +247 -0
  158. package/lib/extension/extension-config.js +709 -0
  159. package/lib/extension/index.js +116 -0
  160. package/lib/extension/manifest-migrations.js +136 -0
  161. package/lib/extension/manifest.js +580 -0
  162. package/lib/extension/package-changed.js +64 -0
  163. package/lib/extension/plugin-config.js +112 -0
  164. package/lib/grid-register.js +49 -35
  165. package/lib/logger.js +1 -2
  166. package/lib/logsink.js +59 -36
  167. package/lib/main.js +369 -104
  168. package/lib/schema/arg-spec.js +229 -0
  169. package/lib/schema/cli-args.js +241 -0
  170. package/lib/schema/cli-transformers.js +119 -0
  171. package/lib/schema/index.js +2 -0
  172. package/lib/schema/keywords.js +136 -0
  173. package/lib/schema/schema.js +725 -0
  174. package/lib/utils.js +289 -167
  175. package/package.json +84 -83
  176. package/plugin.d.ts +1 -0
  177. package/plugin.js +13 -0
  178. package/scripts/autoinstall-extensions.js +243 -0
  179. package/support.d.ts +1 -0
  180. package/support.js +13 -0
  181. package/tsconfig.json +25 -0
  182. package/types/cli.ts +193 -0
  183. package/types/index.ts +20 -0
  184. package/types/manifest/README.md +30 -0
  185. package/types/manifest/base.ts +158 -0
  186. package/types/manifest/index.ts +28 -0
  187. package/types/manifest/v3.ts +161 -0
  188. package/types/manifest/v4.ts +161 -0
  189. package/CHANGELOG.md +0 -3594
  190. package/bin/ios-webkit-debug-proxy-launcher.js +0 -71
  191. package/build/lib/cli/argparse-actions.js +0 -104
  192. package/build/lib/cli/npm.js +0 -200
  193. package/build/lib/cli/parser-helpers.js +0 -93
  194. package/build/lib/driver-config.js +0 -77
  195. package/build/lib/drivers.js +0 -99
  196. package/build/lib/extension-config.js +0 -253
  197. package/build/lib/plugin-config.js +0 -59
  198. package/build/lib/plugins.js +0 -14
  199. package/lib/cli/argparse-actions.js +0 -77
  200. package/lib/cli/npm.js +0 -175
  201. package/lib/cli/parser-helpers.js +0 -91
  202. package/lib/driver-config.js +0 -46
  203. package/lib/drivers.js +0 -84
  204. package/lib/extension-config.js +0 -209
  205. package/lib/plugin-config.js +0 -34
  206. package/lib/plugins.js +0 -10
@@ -0,0 +1,725 @@
1
+ import Ajv from 'ajv';
2
+ import addFormats from 'ajv-formats';
3
+ import _ from 'lodash';
4
+ import path from 'path';
5
+ import {DRIVER_TYPE, PLUGIN_TYPE} from '../constants';
6
+ import {AppiumConfigJsonSchema} from '@appium/schema';
7
+ import {APPIUM_CONFIG_SCHEMA_ID, ArgSpec, SERVER_PROP_NAME} from './arg-spec';
8
+ import {keywords} from './keywords';
9
+
10
+ /**
11
+ * Key/value pairs go in... but they don't come out.
12
+ *
13
+ * @template K,V
14
+ * @extends {Map<K,V>}
15
+ */
16
+ export class RoachHotelMap extends Map {
17
+ /**
18
+ * @param {K} key
19
+ * @param {V} value
20
+ */
21
+ set(key, value) {
22
+ if (this.has(key)) {
23
+ throw new Error(`${key} is already set`);
24
+ }
25
+ return super.set(key, value);
26
+ }
27
+
28
+ /**
29
+ * @param {K} key
30
+ */
31
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
32
+ delete(key) {
33
+ return false;
34
+ }
35
+
36
+ clear() {
37
+ throw new Error(`Cannot clear RoachHotelMap`);
38
+ }
39
+ }
40
+
41
+ /**
42
+ * Extensions that an extension schema file can have.
43
+ */
44
+ export const ALLOWED_SCHEMA_EXTENSIONS = Object.freeze(
45
+ new Set(/** @type {AllowedSchemaExtension[]} */ (['.json', '.js', '.cjs']))
46
+ );
47
+
48
+ const SCHEMA_KEY = '$schema';
49
+
50
+ /**
51
+ * A wrapper around Ajv and schema-related functions.
52
+ *
53
+ * Should have been named Highlander, because _there can only be one_
54
+ */
55
+ class AppiumSchema {
56
+ /**
57
+ * A mapping of unique argument IDs to their corresponding {@link ArgSpec}s.
58
+ *
59
+ * An "argument" is a CLI argument or a config property.
60
+ *
61
+ * Used to provide easy lookups of argument metadata when converting between different representations of those arguments.
62
+ * @type {RoachHotelMap<string,ArgSpec>}
63
+ */
64
+ #argSpecs = new RoachHotelMap();
65
+
66
+ /**
67
+ * A map of extension types to extension names to schema objects.
68
+ *
69
+ * This data structure is used to ensure there are no naming conflicts. The schemas
70
+ * are stored here in memory until the instance is _finalized_.
71
+ * @type {Record<ExtensionType,Map<string,SchemaObject>>}
72
+ */
73
+ #registeredSchemas = {[DRIVER_TYPE]: new Map(), [PLUGIN_TYPE]: new Map()};
74
+
75
+ /**
76
+ * Ajv instance
77
+ *
78
+ * @type {Ajv}
79
+ */
80
+ #ajv;
81
+
82
+ /**
83
+ * Singleton instance.
84
+ * @type {AppiumSchema}
85
+ */
86
+ static #instance;
87
+
88
+ /**
89
+ * Lookup of schema IDs to finalized schemas.
90
+ *
91
+ * This does not include references, but rather the root schemas themselves.
92
+ * @type {Record<string,StrictSchemaObject>?}
93
+ */
94
+ #finalizedSchemas = null;
95
+
96
+ /**
97
+ * Initializes Ajv, adds standard formats and our custom keywords.
98
+ * @see https://npm.im/ajv-formats
99
+ * @private
100
+ */
101
+ constructor() {
102
+ this.#ajv = AppiumSchema._instantiateAjv();
103
+ }
104
+
105
+ /**
106
+ * Factory function for {@link AppiumSchema} instances.
107
+ *
108
+ * Returns a singleton instance if one exists, otherwise creates a new one.
109
+ * Binds public methods to the instance.
110
+ * @returns {AppiumSchema}
111
+ */
112
+ static create() {
113
+ if (!AppiumSchema.#instance) {
114
+ const instance = new AppiumSchema();
115
+ AppiumSchema.#instance = instance;
116
+ _.bindAll(instance, [
117
+ 'finalize',
118
+ 'flatten',
119
+ 'getAllArgSpecs',
120
+ 'getArgSpec',
121
+ 'getDefaults',
122
+ 'getDefaultsForExtension',
123
+ 'getSchema',
124
+ 'hasArgSpec',
125
+ 'isFinalized',
126
+ 'registerSchema',
127
+ 'hasRegisteredSchema',
128
+ 'reset',
129
+ 'validate',
130
+ ]);
131
+ }
132
+
133
+ return AppiumSchema.#instance;
134
+ }
135
+
136
+ /**
137
+ * Returns `true` if a schema has been registered using given extension type and name.
138
+ *
139
+ * This does not depend on whether or not the instance has been _finalized_.
140
+ * @param {ExtensionType} extType - Extension type
141
+ * @param {string} extName - Name
142
+ * @returns {boolean} If registered
143
+ */
144
+ hasRegisteredSchema(extType, extName) {
145
+ return this.#registeredSchemas[extType].has(extName);
146
+ }
147
+
148
+ /**
149
+ * Return `true` if {@link AppiumSchema.finalize finalize} has been called
150
+ * successfully and {@link AppiumSchema.reset reset} has not been called since.
151
+ * @returns {boolean} If finalized
152
+ */
153
+ isFinalized() {
154
+ return Boolean(this.#finalizedSchemas);
155
+ }
156
+
157
+ getAllArgSpecs() {
158
+ return this.#argSpecs;
159
+ }
160
+
161
+ /**
162
+ * Call this when no more schemas will be registered.
163
+ *
164
+ * This does three things:
165
+ * 1. It combines all schemas from extensions into the Appium config schema,
166
+ * then adds the result to the `Ajv` instance.
167
+ * 2. It adds schemas for _each_ argument/property for validation purposes.
168
+ * The CLI uses these schemas to validate specific arguments.
169
+ * 3. The schemas are validated against JSON schema draft-07 (which is the
170
+ * only one supported at this time)
171
+ *
172
+ * Any method in this instance that needs to interact with the `Ajv` instance
173
+ * will throw if this method has not been called.
174
+ *
175
+ * If the instance has already been finalized, this is a no-op.
176
+ * @public
177
+ * @throws {Error} If the schema is not valid
178
+ * @returns {Readonly<Record<string,StrictSchemaObject>>} Record of schema IDs to full schema objects
179
+ */
180
+ finalize() {
181
+ if (this.isFinalized()) {
182
+ return /** @type {Record<string,StrictSchemaObject>} */ (this.#finalizedSchemas);
183
+ }
184
+
185
+ const ajv = this.#ajv;
186
+
187
+ // Ajv will _mutate_ the schema, so we need to clone it.
188
+ const baseSchema = _.cloneDeep(AppiumConfigJsonSchema);
189
+
190
+ /**
191
+ *
192
+ * @param {SchemaObject} schema
193
+ * @param {ExtensionType} [extType]
194
+ * @param {string} [extName]
195
+ */
196
+ const addArgSpecs = (schema, extType, extName) => {
197
+ for (let [propName, propSchema] of Object.entries(schema)) {
198
+ const argSpec = ArgSpec.create(propName, {
199
+ dest: propSchema.appiumCliDest,
200
+ defaultValue: propSchema.default,
201
+ extType,
202
+ extName,
203
+ });
204
+ const {arg} = argSpec;
205
+ this.#argSpecs.set(arg, argSpec);
206
+ }
207
+ };
208
+
209
+ addArgSpecs(_.omit(baseSchema.properties.server.properties, [DRIVER_TYPE, PLUGIN_TYPE]));
210
+
211
+ /**
212
+ * @type {Record<string,StrictSchemaObject>}
213
+ */
214
+ const finalizedSchemas = {};
215
+
216
+ const finalSchema = _.reduce(
217
+ this.#registeredSchemas,
218
+ /**
219
+ * @param {typeof baseSchema} baseSchema
220
+ * @param {Map<string,SchemaObject>} extensionSchemas
221
+ * @param {ExtensionType} extType
222
+ */
223
+ (baseSchema, extensionSchemas, extType) => {
224
+ extensionSchemas.forEach((schema, extName) => {
225
+ const $ref = ArgSpec.toSchemaBaseRef(extType, extName);
226
+ schema.$id = $ref;
227
+ schema.additionalProperties = false; // this makes `schema` become a `StrictSchemaObject`
228
+ baseSchema.properties.server.properties[extType].properties[extName] = {
229
+ $ref,
230
+ $comment: extName,
231
+ };
232
+ ajv.validateSchema(schema, true);
233
+ addArgSpecs(schema.properties, extType, extName);
234
+ ajv.addSchema(schema, $ref);
235
+ finalizedSchemas[$ref] = /** @type {StrictSchemaObject} */ (schema);
236
+ });
237
+ return baseSchema;
238
+ },
239
+ baseSchema
240
+ );
241
+
242
+ ajv.addSchema(finalSchema, APPIUM_CONFIG_SCHEMA_ID);
243
+ finalizedSchemas[APPIUM_CONFIG_SCHEMA_ID] = finalSchema;
244
+ ajv.validateSchema(finalSchema, true);
245
+
246
+ this.#finalizedSchemas = finalizedSchemas;
247
+ return Object.freeze(finalizedSchemas);
248
+ }
249
+
250
+ /**
251
+ * Configures and creates an Ajv instance.
252
+ * @private
253
+ * @returns {Ajv}
254
+ */
255
+ static _instantiateAjv() {
256
+ const ajv = addFormats(
257
+ new Ajv({
258
+ // without this not much validation actually happens
259
+ allErrors: true,
260
+ })
261
+ );
262
+
263
+ // add custom keywords to ajv. see schema-keywords.js
264
+ _.forEach(keywords, (keyword) => {
265
+ ajv.addKeyword(keyword);
266
+ });
267
+
268
+ return ajv;
269
+ }
270
+
271
+ /**
272
+ * Resets this instance to its original state.
273
+ *
274
+ * - Removes all added schemas from the `Ajv` instance
275
+ * - Resets the map of {@link ArgSpec ArgSpecs}
276
+ * - Resets the map of registered schemas
277
+ * - Sets the {@link AppiumSchema._finalized _finalized} flag to `false`
278
+ *
279
+ * If you need to call {@link AppiumSchema.finalize} again, you'll want to call this first.
280
+ * @returns {void}
281
+ */
282
+ reset() {
283
+ for (const schemaId of Object.keys(this.#finalizedSchemas ?? {})) {
284
+ this.#ajv.removeSchema(schemaId);
285
+ }
286
+ this.#argSpecs = new RoachHotelMap();
287
+ this.#registeredSchemas = {
288
+ [DRIVER_TYPE]: new Map(),
289
+ [PLUGIN_TYPE]: new Map(),
290
+ };
291
+ this.#finalizedSchemas = null;
292
+
293
+ // Ajv seems to have an over-eager cache, so we have to dump the object entirely.
294
+ this.#ajv = AppiumSchema._instantiateAjv();
295
+ }
296
+
297
+ /**
298
+ * Registers a schema from an extension.
299
+ *
300
+ * This is "fail-fast" in that the schema will immediately be validated against JSON schema draft-07 _or_ whatever the value of the schema's `$schema` prop is.
301
+ *
302
+ * Does _not_ add the schema to the `ajv` instance (this is done by {@link AppiumSchema.finalize}).
303
+ * @param {ExtensionType} extType - Extension type
304
+ * @param {string} extName - Unique extension name for `type`
305
+ * @param {SchemaObject} schema - Schema object
306
+ * @throws {SchemaNameConflictError} If the schema is an invalid
307
+ * @returns {void}
308
+ */
309
+ registerSchema(extType, extName, schema) {
310
+ if (!(extType && extName) || _.isUndefined(schema)) {
311
+ throw new TypeError('Expected extension type, extension name, and a defined schema');
312
+ }
313
+ if (!AppiumSchema.isSupportedSchemaType(schema)) {
314
+ throw new SchemaUnsupportedSchemaError(schema, extType, extName);
315
+ }
316
+ const normalizedExtName = _.kebabCase(extName);
317
+ if (this.hasRegisteredSchema(extType, normalizedExtName)) {
318
+ if (_.isEqual(this.#registeredSchemas[extType].get(normalizedExtName), schema)) {
319
+ return;
320
+ }
321
+ throw new SchemaNameConflictError(extType, extName);
322
+ }
323
+ this.#ajv.validateSchema(schema, true);
324
+
325
+ this.#registeredSchemas[extType].set(normalizedExtName, schema);
326
+ }
327
+
328
+ /**
329
+ * Returns a {@link ArgSpec} for the given argument name.
330
+ * @param {string} name - CLI argument name
331
+ * @param {ExtensionType} [extType] - Extension type
332
+ * @param {string} [extName] - Extension name
333
+ * @returns {ArgSpec|undefined} ArgSpec or `undefined` if not found
334
+ */
335
+ getArgSpec(name, extType, extName) {
336
+ return this.#argSpecs.get(ArgSpec.toArg(name, extType, extName));
337
+ }
338
+
339
+ /**
340
+ * Returns `true` if the instance knows about an argument by the given `name`.
341
+ * @param {string} name - CLI argument name
342
+ * @param {ExtensionType} [extType] - Extension type
343
+ * @param {string} [extName] - Extension name
344
+ * @returns {boolean} `true` if such an {@link ArgSpec} exists
345
+ */
346
+ hasArgSpec(name, extType, extName) {
347
+ return this.#argSpecs.has(ArgSpec.toArg(name, extType, extName));
348
+ }
349
+
350
+ /**
351
+ * Returns a `Record` of argument "dest" strings to default values.
352
+ *
353
+ * The "dest" string is the property name in object returned by
354
+ * `argparse.ArgumentParser['parse_args']`.
355
+ * @template {boolean|undefined} Flattened
356
+ * @param {Flattened} [flatten=true] - If `true`, flattens the returned object
357
+ * using "keypath"-style keys of the format `<extType>.<extName>.<argName>`.
358
+ * Otherwise, returns a nested object using `extType` and `extName` as
359
+ * properties. Base arguments (server arguments) are always at the top level.
360
+ * @returns {DefaultValues<Flattened>}
361
+ */
362
+ getDefaults(flatten = /** @type {Flattened} */ (true)) {
363
+ if (!this.isFinalized()) {
364
+ throw new SchemaFinalizationError();
365
+ }
366
+
367
+ /**
368
+ * @private
369
+ * @callback DefaultReducer
370
+ * @param {DefaultValues<Flattened>} defaults
371
+ * @param {ArgSpec} argSpec
372
+ * @returns {DefaultValues<Flattened>}
373
+ */
374
+ /** @type {DefaultReducer} */
375
+ const reducer = flatten
376
+ ? (defaults, {defaultValue, dest}) => {
377
+ if (!_.isUndefined(defaultValue)) {
378
+ defaults[dest] = defaultValue;
379
+ }
380
+ return defaults;
381
+ }
382
+ : (defaults, {defaultValue, dest}) => {
383
+ if (!_.isUndefined(defaultValue)) {
384
+ _.set(defaults, dest, defaultValue);
385
+ }
386
+ return defaults;
387
+ };
388
+
389
+ /** @type {DefaultValues<Flattened>} */
390
+ const retval = {};
391
+ return [...this.#argSpecs.values()].reduce(reducer, retval);
392
+ }
393
+
394
+ /**
395
+ * Returns a flattened Record of defaults for a specific extension. Keys will
396
+ * be of format `<argName>`.
397
+ * @param {ExtensionType} extType - Extension type
398
+ * @param {string} extName - Extension name
399
+ * @returns {Record<string,ArgSpecDefaultValue>}
400
+ */
401
+ getDefaultsForExtension(extType, extName) {
402
+ if (!this.isFinalized()) {
403
+ throw new SchemaFinalizationError();
404
+ }
405
+ const specs = [...this.#argSpecs.values()].filter(
406
+ (spec) => spec.extType === extType && spec.extName === extName
407
+ );
408
+ return specs.reduce((defaults, {defaultValue, rawDest}) => {
409
+ if (!_.isUndefined(defaultValue)) {
410
+ defaults[rawDest] = defaultValue;
411
+ }
412
+ return defaults;
413
+ }, {});
414
+ }
415
+
416
+ /**
417
+ * Flatten schema into an array of `SchemaObject`s and associated
418
+ * {@link ArgSpec ArgSpecs}.
419
+ *
420
+ * Converts nested extension schemas to keys based on the extension type and
421
+ * name. Used when translating to `argparse` options or getting the list of
422
+ * default values (see {@link AppiumSchema.getDefaults}) for CLI or otherwise.
423
+ *
424
+ * The return value is an intermediate reprsentation used by `cli-args`
425
+ * module's `toParserArgs`, which converts the finalized schema to parameters
426
+ * used by `argparse`.
427
+ * @throws If {@link AppiumSchema.finalize} has not been called yet.
428
+ * @returns {FlattenedSchema}
429
+ */
430
+ flatten() {
431
+ const schema = this.getSchema();
432
+
433
+ /** @type { {properties: SchemaObject, prefix: string[]}[] } */
434
+ const stack = [{properties: schema.properties, prefix: []}];
435
+ /** @type {FlattenedSchema} */
436
+ const flattened = [];
437
+
438
+ // this bit is a recursive algorithm rewritten as a for loop.
439
+ // when we find something we want to traverse, we add it to `stack`
440
+ for (const {properties, prefix} of stack) {
441
+ const pairs = _.toPairs(properties);
442
+ for (const [key, value] of pairs) {
443
+ if (key === SCHEMA_KEY) {
444
+ continue;
445
+ }
446
+ const {properties, $ref} = value;
447
+ if (properties) {
448
+ stack.push({
449
+ properties,
450
+ prefix: key === SERVER_PROP_NAME ? [] : [...prefix, key],
451
+ });
452
+ } else if ($ref) {
453
+ let refSchema;
454
+ try {
455
+ refSchema = this.getSchema($ref);
456
+ } catch (err) {
457
+ // this can happen if an extension schema supplies a $ref to a non-existent schema
458
+ throw new SchemaUnknownSchemaError($ref);
459
+ }
460
+ const {normalizedExtName} = ArgSpec.extensionInfoFromRootSchemaId($ref);
461
+ if (!normalizedExtName) {
462
+ /* istanbul ignore next */
463
+ throw new ReferenceError(
464
+ `Could not determine extension name from schema ID ${$ref}. This is a bug.`
465
+ );
466
+ }
467
+ stack.push({
468
+ properties: refSchema.properties,
469
+ prefix: [...prefix, key, normalizedExtName],
470
+ });
471
+ } else if (key !== DRIVER_TYPE && key !== PLUGIN_TYPE) {
472
+ const [extType, extName] = prefix;
473
+ const argSpec = this.getArgSpec(key, /** @type {ExtensionType} */ (extType), extName);
474
+ if (!argSpec) {
475
+ /* istanbul ignore next */
476
+ throw new ReferenceError(
477
+ `Unknown argument with key ${key}, extType ${extType} and extName ${extName}. This is a bug.`
478
+ );
479
+ }
480
+ flattened.push({schema: _.cloneDeep(value), argSpec});
481
+ }
482
+ }
483
+ }
484
+
485
+ return flattened;
486
+ }
487
+
488
+ /**
489
+ * Retrieves the schema itself
490
+ * @public
491
+ * @param {string} [ref] - Schema ID
492
+ * @throws If the schema has not yet been finalized
493
+ * @returns {SchemaObject}
494
+ */
495
+ getSchema(ref = APPIUM_CONFIG_SCHEMA_ID) {
496
+ return /** @type {SchemaObject} */ (this._getValidator(ref).schema);
497
+ }
498
+
499
+ /**
500
+ * Retrieves schema validator function from Ajv
501
+ * @param {string} [id] - Schema ID
502
+ * @private
503
+ * @returns {import('ajv').ValidateFunction}
504
+ */
505
+ _getValidator(id = APPIUM_CONFIG_SCHEMA_ID) {
506
+ const validator = this.#ajv.getSchema(id);
507
+ if (!validator) {
508
+ if (id === APPIUM_CONFIG_SCHEMA_ID) {
509
+ throw new SchemaFinalizationError();
510
+ } else {
511
+ throw new SchemaUnknownSchemaError(id);
512
+ }
513
+ }
514
+ return validator;
515
+ }
516
+
517
+ /**
518
+ * Given an object, validates it against the Appium config schema.
519
+ * If errors occur, the returned array will be non-empty.
520
+ * @param {any} value - The value (hopefully an object) to validate against the schema
521
+ * @param {string} [ref] - Schema ID or ref.
522
+ * @public
523
+ * @returns {import('ajv').ErrorObject[]} Array of errors, if any.
524
+ */
525
+ validate(value, ref = APPIUM_CONFIG_SCHEMA_ID) {
526
+ const validator = this._getValidator(ref);
527
+ return !validator(value) && _.isArray(validator.errors) ? [...validator.errors] : [];
528
+ }
529
+
530
+ /**
531
+ * Returns `true` if `filename`'s file extension is allowed (in {@link ALLOWED_SCHEMA_EXTENSIONS}).
532
+ * @param {import('type-fest').LiteralUnion<AllowedSchemaExtension, string>} filename
533
+ * @returns {boolean}
534
+ */
535
+ static isAllowedSchemaFileExtension(filename) {
536
+ return ALLOWED_SCHEMA_EXTENSIONS.has(
537
+ /** @type {AllowedSchemaExtension} */ (path.extname(filename))
538
+ );
539
+ }
540
+
541
+ /**
542
+ * Returns `true` if `schema` is a plain object with a non-true `$async` property.
543
+ * @param {any} schema - Schema to check
544
+ * @returns {schema is SchemaObject}
545
+ */
546
+ static isSupportedSchemaType(schema) {
547
+ return _.isPlainObject(schema) && schema.$async !== true;
548
+ }
549
+ }
550
+
551
+ /**
552
+ * Thrown when the {@link AppiumSchema} instance has not yet been finalized, but
553
+ * the method called requires it.
554
+ */
555
+ export class SchemaFinalizationError extends Error {
556
+ /**
557
+ * @type {Readonly<string>}
558
+ */
559
+ code = 'APPIUMERR_SCHEMA_FINALIZATION';
560
+
561
+ constructor() {
562
+ super('Schema not yet finalized; `finalize()` must be called first.');
563
+ }
564
+ }
565
+
566
+ /**
567
+ * Thrown when a "unique" schema ID conflicts with an existing schema ID.
568
+ *
569
+ * This is likely going to be caused by attempting to register the same schema twice.
570
+ */
571
+ export class SchemaNameConflictError extends Error {
572
+ /**
573
+ * @type {Readonly<string>}
574
+ */
575
+ code = 'APPIUMERR_SCHEMA_NAME_CONFLICT';
576
+
577
+ /**
578
+ * @type {Readonly<{extType: ExtensionType, extName: string}>}
579
+ */
580
+ data;
581
+
582
+ /**
583
+ * @param {ExtensionType} extType
584
+ * @param {string} extName
585
+ */
586
+ constructor(extType, extName) {
587
+ super(`Name for ${extType} schema "${extName}" conflicts with an existing schema`);
588
+ this.data = {extType, extName};
589
+ }
590
+ }
591
+
592
+ /**
593
+ * Thrown when a schema ID was expected, but it doesn't exist on the {@link Ajv} instance.
594
+ */
595
+ export class SchemaUnknownSchemaError extends ReferenceError {
596
+ /**
597
+ * @type {Readonly<string>}
598
+ */
599
+ code = 'APPIUMERR_SCHEMA_UNKNOWN_SCHEMA';
600
+
601
+ /**
602
+ * @type {Readonly<{schemaId: string}>}
603
+ */
604
+ data;
605
+
606
+ /**
607
+ * @param {string} schemaId
608
+ */
609
+ constructor(schemaId) {
610
+ super(`Unknown schema: "${schemaId}"`);
611
+ this.data = {schemaId};
612
+ }
613
+ }
614
+
615
+ /**
616
+ * Thrown when a schema is provided, but it's of an unsupported type.
617
+ *
618
+ * "Valid" schemas which are unsupported include boolean schemas and async schemas
619
+ * (having a `true` `$async` property).
620
+ */
621
+ export class SchemaUnsupportedSchemaError extends TypeError {
622
+ /**
623
+ * @type {Readonly<string>}
624
+ */
625
+ code = 'APPIUMERR_SCHEMA_UNSUPPORTED_SCHEMA';
626
+
627
+ /**
628
+ * @type {Readonly<{schema: any, extType: ExtensionType, extName: string}>}
629
+ */
630
+ data;
631
+
632
+ /**
633
+ * @param {any} schema
634
+ * @param {ExtensionType} extType
635
+ * @param {string} extName
636
+ */
637
+ constructor(schema, extType, extName) {
638
+ // https://github.com/Microsoft/TypeScript/issues/8277
639
+ super(
640
+ (() => {
641
+ let msg = `Unsupported schema from ${extType} "${extName}":`;
642
+ if (_.isBoolean(schema)) {
643
+ return `${msg} schema cannot be a boolean`;
644
+ }
645
+ if (_.isPlainObject(schema)) {
646
+ if (schema.$async) {
647
+ return `${msg} schema cannot be an async schema`;
648
+ }
649
+ /* istanbul ignore next */
650
+ throw new TypeError(
651
+ `schema IS supported; this error should not be thrown (this is a bug). value of schema: ${JSON.stringify(
652
+ schema
653
+ )}`
654
+ );
655
+ }
656
+ return `${msg} schema must be a plain object without a true "$async" property`;
657
+ })()
658
+ );
659
+ this.data = {schema, extType, extName};
660
+ }
661
+ }
662
+
663
+ const appiumSchema = AppiumSchema.create();
664
+
665
+ export const {
666
+ registerSchema,
667
+ getAllArgSpecs,
668
+ getArgSpec,
669
+ hasArgSpec,
670
+ isFinalized,
671
+ finalize: finalizeSchema,
672
+ reset: resetSchema,
673
+ validate,
674
+ getSchema,
675
+ flatten: flattenSchema,
676
+ getDefaults: getDefaultsForSchema,
677
+ getDefaultsForExtension,
678
+ } = appiumSchema;
679
+ export const {isAllowedSchemaFileExtension} = AppiumSchema;
680
+
681
+ /**
682
+ * Appium only supports schemas that are plain objects; not arrays.
683
+ * @typedef {import('ajv').SchemaObject & {[key: number]: never}} SchemaObject
684
+ */
685
+
686
+ /**
687
+ * @typedef {import('@appium/types').ExtensionType} ExtensionType
688
+ */
689
+
690
+ /**
691
+ * An object having property `additionalProperties: false`
692
+ * @typedef StrictProp
693
+ * @property {false} additionalProperties
694
+ */
695
+
696
+ /**
697
+ * A {@link SchemaObject} with `additionalProperties: false`
698
+ * @typedef {SchemaObject & StrictProp} StrictSchemaObject
699
+ */
700
+
701
+ /**
702
+ * A list of schemas associated with properties and their corresponding {@link ArgSpec} objects.
703
+ *
704
+ * Intermediate data structure used when converting the entire schema down to CLI arguments.
705
+ * @typedef { {schema: SchemaObject, argSpec: ArgSpec}[] } FlattenedSchema
706
+ */
707
+
708
+ /**
709
+ * @typedef {ArgSpec['defaultValue']} ArgSpecDefaultValue
710
+ */
711
+
712
+ /**
713
+ * e.g. `{driver: {foo: 'bar'}}` where `foo` is the arg name and `bar` is the default value.
714
+ * @typedef {Record<string,Record<string,ArgSpecDefaultValue>>} NestedArgSpecDefaultValue
715
+ */
716
+
717
+ /**
718
+ * Helper type for the return value of {@link AppiumSchema.getDefaults}
719
+ * @template {boolean|undefined} Flattened
720
+ * @typedef {Record<string,Flattened extends true ? ArgSpecDefaultValue : ArgSpecDefaultValue | NestedArgSpecDefaultValue>} DefaultValues
721
+ */
722
+
723
+ /**
724
+ * @typedef {'.json'|'.js'|'.cjs'} AllowedSchemaExtension
725
+ */