appium 2.0.0-beta.4 → 2.0.0-beta.40

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 (151) hide show
  1. package/README.md +10 -11
  2. package/build/lib/appium.d.ts +204 -0
  3. package/build/lib/appium.d.ts.map +1 -0
  4. package/build/lib/appium.js +257 -131
  5. package/build/lib/cli/args.d.ts +20 -0
  6. package/build/lib/cli/args.d.ts.map +1 -0
  7. package/build/lib/cli/args.js +96 -282
  8. package/build/lib/cli/driver-command.d.ts +36 -0
  9. package/build/lib/cli/driver-command.d.ts.map +1 -0
  10. package/build/lib/cli/driver-command.js +25 -18
  11. package/build/lib/cli/extension-command.d.ts +372 -0
  12. package/build/lib/cli/extension-command.d.ts.map +1 -0
  13. package/build/lib/cli/extension-command.js +286 -156
  14. package/build/lib/cli/extension.d.ts +18 -0
  15. package/build/lib/cli/extension.d.ts.map +1 -0
  16. package/build/lib/cli/extension.js +30 -17
  17. package/build/lib/cli/parser.d.ts +80 -0
  18. package/build/lib/cli/parser.d.ts.map +1 -0
  19. package/build/lib/cli/parser.js +152 -95
  20. package/build/lib/cli/plugin-command.d.ts +33 -0
  21. package/build/lib/cli/plugin-command.d.ts.map +1 -0
  22. package/build/lib/cli/plugin-command.js +24 -19
  23. package/build/lib/cli/utils.d.ts +29 -0
  24. package/build/lib/cli/utils.d.ts.map +1 -0
  25. package/build/lib/cli/utils.js +27 -3
  26. package/build/lib/config-file.d.ts +100 -0
  27. package/build/lib/config-file.d.ts.map +1 -0
  28. package/build/lib/config-file.js +136 -0
  29. package/build/lib/config.d.ts +41 -0
  30. package/build/lib/config.d.ts.map +1 -0
  31. package/build/lib/config.js +92 -67
  32. package/build/lib/constants.d.ts +48 -0
  33. package/build/lib/constants.d.ts.map +1 -0
  34. package/build/lib/constants.js +60 -0
  35. package/build/lib/extension/driver-config.d.ts +81 -0
  36. package/build/lib/extension/driver-config.d.ts.map +1 -0
  37. package/build/lib/extension/driver-config.js +177 -0
  38. package/build/lib/extension/extension-config.d.ts +242 -0
  39. package/build/lib/extension/extension-config.d.ts.map +1 -0
  40. package/build/lib/extension/extension-config.js +436 -0
  41. package/build/lib/extension/index.d.ts +48 -0
  42. package/build/lib/extension/index.d.ts.map +1 -0
  43. package/build/lib/extension/index.js +74 -0
  44. package/build/lib/extension/manifest.d.ts +174 -0
  45. package/build/lib/extension/manifest.d.ts.map +1 -0
  46. package/build/lib/extension/manifest.js +256 -0
  47. package/build/lib/extension/package-changed.d.ts +11 -0
  48. package/build/lib/extension/package-changed.d.ts.map +1 -0
  49. package/build/lib/extension/package-changed.js +68 -0
  50. package/build/lib/extension/plugin-config.d.ts +57 -0
  51. package/build/lib/extension/plugin-config.d.ts.map +1 -0
  52. package/build/lib/extension/plugin-config.js +78 -0
  53. package/build/lib/grid-register.d.ts +10 -0
  54. package/build/lib/grid-register.d.ts.map +1 -0
  55. package/build/lib/grid-register.js +21 -25
  56. package/build/lib/logger.d.ts +3 -0
  57. package/build/lib/logger.d.ts.map +1 -0
  58. package/build/lib/logger.js +4 -6
  59. package/build/lib/logsink.d.ts +4 -0
  60. package/build/lib/logsink.d.ts.map +1 -0
  61. package/build/lib/logsink.js +14 -17
  62. package/build/lib/main.d.ts +55 -0
  63. package/build/lib/main.d.ts.map +1 -0
  64. package/build/lib/main.js +189 -90
  65. package/build/lib/schema/arg-spec.d.ts +143 -0
  66. package/build/lib/schema/arg-spec.d.ts.map +1 -0
  67. package/build/lib/schema/arg-spec.js +119 -0
  68. package/build/lib/schema/cli-args.d.ts +19 -0
  69. package/build/lib/schema/cli-args.d.ts.map +1 -0
  70. package/build/lib/schema/cli-args.js +180 -0
  71. package/build/lib/schema/cli-transformers.d.ts +5 -0
  72. package/build/lib/schema/cli-transformers.d.ts.map +1 -0
  73. package/build/lib/schema/cli-transformers.js +74 -0
  74. package/build/lib/schema/index.d.ts +3 -0
  75. package/build/lib/schema/index.d.ts.map +1 -0
  76. package/build/lib/schema/index.js +34 -0
  77. package/build/lib/schema/keywords.d.ts +24 -0
  78. package/build/lib/schema/keywords.d.ts.map +1 -0
  79. package/build/lib/schema/keywords.js +70 -0
  80. package/build/lib/schema/schema.d.ts +259 -0
  81. package/build/lib/schema/schema.d.ts.map +1 -0
  82. package/build/lib/schema/schema.js +452 -0
  83. package/build/lib/utils.d.ts +66 -0
  84. package/build/lib/utils.d.ts.map +1 -0
  85. package/build/lib/utils.js +35 -139
  86. package/build/tsconfig.tsbuildinfo +1 -0
  87. package/build/types/appium-manifest.d.ts +59 -0
  88. package/build/types/appium-manifest.d.ts.map +1 -0
  89. package/build/types/cli.d.ts +112 -0
  90. package/build/types/cli.d.ts.map +1 -0
  91. package/build/types/extension-manifest.d.ts +55 -0
  92. package/build/types/extension-manifest.d.ts.map +1 -0
  93. package/build/types/index.d.ts +16 -0
  94. package/build/types/index.d.ts.map +1 -0
  95. package/driver.d.ts +1 -0
  96. package/driver.js +14 -0
  97. package/index.js +11 -0
  98. package/lib/appium.js +517 -186
  99. package/lib/cli/args.js +269 -422
  100. package/lib/cli/driver-command.js +58 -23
  101. package/lib/cli/extension-command.js +612 -260
  102. package/lib/cli/extension.js +34 -16
  103. package/lib/cli/parser.js +241 -83
  104. package/lib/cli/plugin-command.js +48 -20
  105. package/lib/cli/utils.js +24 -10
  106. package/lib/config-file.js +219 -0
  107. package/lib/config.js +210 -91
  108. package/lib/constants.js +69 -0
  109. package/lib/extension/driver-config.js +249 -0
  110. package/lib/extension/extension-config.js +679 -0
  111. package/lib/extension/index.js +116 -0
  112. package/lib/extension/manifest.js +475 -0
  113. package/lib/extension/package-changed.js +64 -0
  114. package/lib/extension/plugin-config.js +113 -0
  115. package/lib/grid-register.js +49 -35
  116. package/lib/logger.js +1 -2
  117. package/lib/logsink.js +38 -33
  118. package/lib/main.js +303 -100
  119. package/lib/schema/arg-spec.js +229 -0
  120. package/lib/schema/cli-args.js +238 -0
  121. package/lib/schema/cli-transformers.js +115 -0
  122. package/lib/schema/index.js +2 -0
  123. package/lib/schema/keywords.js +136 -0
  124. package/lib/schema/schema.js +717 -0
  125. package/lib/utils.js +121 -140
  126. package/package.json +75 -85
  127. package/plugin.d.ts +1 -0
  128. package/plugin.js +13 -0
  129. package/scripts/autoinstall-extensions.js +177 -0
  130. package/support.d.ts +1 -0
  131. package/support.js +13 -0
  132. package/types/appium-manifest.ts +73 -0
  133. package/types/cli.ts +146 -0
  134. package/types/extension-manifest.ts +64 -0
  135. package/types/index.ts +21 -0
  136. package/CHANGELOG.md +0 -3515
  137. package/bin/ios-webkit-debug-proxy-launcher.js +0 -71
  138. package/build/lib/cli/npm.js +0 -206
  139. package/build/lib/cli/parser-helpers.js +0 -82
  140. package/build/lib/driver-config.js +0 -77
  141. package/build/lib/drivers.js +0 -96
  142. package/build/lib/extension-config.js +0 -253
  143. package/build/lib/plugin-config.js +0 -59
  144. package/build/lib/plugins.js +0 -14
  145. package/lib/cli/npm.js +0 -183
  146. package/lib/cli/parser-helpers.js +0 -79
  147. package/lib/driver-config.js +0 -46
  148. package/lib/drivers.js +0 -81
  149. package/lib/extension-config.js +0 -209
  150. package/lib/plugin-config.js +0 -34
  151. package/lib/plugins.js +0 -10
@@ -0,0 +1,679 @@
1
+ import _ from 'lodash';
2
+ import B from 'bluebird';
3
+ import path from 'path';
4
+ import resolveFrom from 'resolve-from';
5
+ import {satisfies} from 'semver';
6
+ import {util} from '@appium/support';
7
+ import {commandClasses} from '../cli/extension';
8
+ import {APPIUM_VER} from '../config';
9
+ import log from '../logger';
10
+ import {
11
+ ALLOWED_SCHEMA_EXTENSIONS,
12
+ isAllowedSchemaFileExtension,
13
+ registerSchema,
14
+ } from '../schema/schema';
15
+
16
+ const INSTALL_TYPE_NPM = 'npm';
17
+ const INSTALL_TYPE_LOCAL = 'local';
18
+ const INSTALL_TYPE_GITHUB = 'github';
19
+ const INSTALL_TYPE_GIT = 'git';
20
+
21
+ /** @type {Set<InstallType>} */
22
+ const INSTALL_TYPES = new Set([
23
+ INSTALL_TYPE_GIT,
24
+ INSTALL_TYPE_GITHUB,
25
+ INSTALL_TYPE_LOCAL,
26
+ INSTALL_TYPE_NPM,
27
+ ]);
28
+
29
+ /**
30
+ * This class is abstract. It should not be instantiated directly.
31
+ *
32
+ * Subclasses should provide the generic parameter to implement.
33
+ * @template {ExtensionType} ExtType
34
+ */
35
+ export class ExtensionConfig {
36
+ /** @type {ExtType} */
37
+ extensionType;
38
+
39
+ /** @type {`${ExtType}s`} */
40
+ configKey;
41
+
42
+ /** @type {ExtRecord<ExtType>} */
43
+ installedExtensions;
44
+
45
+ /** @type {import('@appium/types').AppiumLogger} */
46
+ log;
47
+
48
+ /** @type {Manifest} */
49
+ manifest;
50
+
51
+ /**
52
+ * @type {ExtensionListData}
53
+ */
54
+ _listDataCache;
55
+
56
+ /**
57
+ * @protected
58
+ * @param {ExtType} extensionType - Type of extension
59
+ * @param {Manifest} manifest - `Manifest` instance
60
+ */
61
+ constructor(extensionType, manifest) {
62
+ this.extensionType = extensionType;
63
+ this.configKey = `${extensionType}s`;
64
+ this.installedExtensions = manifest.getExtensionData(extensionType);
65
+ this.manifest = manifest;
66
+ }
67
+
68
+ get manifestPath() {
69
+ return this.manifest.manifestPath;
70
+ }
71
+
72
+ get appiumHome() {
73
+ return this.manifest.appiumHome;
74
+ }
75
+
76
+ /**
77
+ * Returns a list of errors for a given extension.
78
+ *
79
+ * @param {ExtName<ExtType>} extName
80
+ * @param {ExtManifest<ExtType>} extManifest
81
+ * @returns {ExtManifestProblem[]}
82
+ */
83
+ getProblems(extName, extManifest) {
84
+ return [
85
+ ...this.getGenericConfigProblems(extManifest, extName),
86
+ ...this.getConfigProblems(extManifest, extName),
87
+ ...this.getSchemaProblems(extManifest, extName),
88
+ ];
89
+ }
90
+
91
+ /**
92
+ * Returns a list of warnings for a given extension.
93
+ *
94
+ * @param {ExtName<ExtType>} extName
95
+ * @param {ExtManifest<ExtType>} extManifest
96
+ * @returns {Promise<string[]>}
97
+ */
98
+ async getWarnings(extName, extManifest) {
99
+ const [genericConfigWarnings, configWarnings] = await B.all([
100
+ this.getGenericConfigWarnings(extManifest, extName),
101
+ this.getConfigWarnings(extManifest, extName),
102
+ ]);
103
+
104
+ return [...genericConfigWarnings, ...configWarnings];
105
+ }
106
+
107
+ /**
108
+ * Returns a list of extension-type-specific issues. To be implemented by subclasses.
109
+ * @abstract
110
+ * @param {ExtManifest<ExtType>} extManifest
111
+ * @param {ExtName<ExtType>} extName
112
+ * @returns {Promise<string[]>}
113
+ */
114
+ // eslint-disable-next-line no-unused-vars,require-await
115
+ async getConfigWarnings(extManifest, extName) {
116
+ return [];
117
+ }
118
+
119
+ /**
120
+ *
121
+ * @param {Map<ExtName<ExtType>,ExtManifestProblem[]>} [errorMap]
122
+ * @param {Map<ExtName<ExtType>,string[]>} [warningMap]
123
+ */
124
+ getValidationResultSummaries(errorMap = new Map(), warningMap = new Map()) {
125
+ /**
126
+ * Array of computed strings
127
+ * @type {string[]}
128
+ */
129
+ const errorSummaries = [];
130
+ for (const [extName, problems] of errorMap.entries()) {
131
+ if (_.isEmpty(problems)) {
132
+ continue;
133
+ }
134
+ // remove this extension from the list since it's not valid
135
+ errorSummaries.push(
136
+ `${this.extensionType} "${extName}" had ${util.pluralize(
137
+ 'error',
138
+ problems.length
139
+ )} and will not be available:`
140
+ );
141
+ for (const problem of problems) {
142
+ errorSummaries.push(
143
+ ` - ${problem.err} (Actual value: ` + `${JSON.stringify(problem.val)})`
144
+ );
145
+ }
146
+ }
147
+ /** @type {string[]} */
148
+ const warningSummaries = [];
149
+ for (const [extName, warnings] of warningMap.entries()) {
150
+ if (_.isEmpty(warnings)) {
151
+ continue;
152
+ }
153
+ const extTypeText = _.capitalize(this.extensionType);
154
+ const problemEnumerationText = util.pluralize('potential problem', warnings.length, true);
155
+ warningSummaries.push(`${extTypeText} "${extName}" has ${problemEnumerationText}: `);
156
+ for (const warning of warnings) {
157
+ warningSummaries.push(` - ${warning}`);
158
+ }
159
+ }
160
+
161
+ return {errorSummaries, warningSummaries};
162
+ }
163
+
164
+ /**
165
+ * Checks extensions for problems. To be called by subclasses' `validate` method.
166
+ *
167
+ * Errors and warnings will be displayed to the user.
168
+ *
169
+ * This method mutates `exts`.
170
+ *
171
+ * @protected
172
+ * @param {ExtRecord<ExtType>} exts - Lookup of extension names to {@linkcode ExtManifest} objects
173
+ * @returns {Promise<ExtRecord<ExtType>>} The same lookup, but picking only error-free extensions
174
+ */
175
+ async _validate(exts) {
176
+ /**
177
+ * Lookup of extension names to {@linkcode ExtManifestProblem ExtManifestProblems}
178
+ * @type {Map<ExtName<ExtType>,ExtManifestProblem[]>}
179
+ */
180
+ const errorMap = new Map();
181
+ /**
182
+ * Lookup of extension names to warnings.
183
+ * @type {Map<ExtName<ExtType>,string[]>}
184
+ */
185
+ const warningMap = new Map();
186
+
187
+ for (const [extName, extManifest] of _.toPairs(exts)) {
188
+ const [errors, warnings] = await B.all([
189
+ this.getProblems(extName, extManifest),
190
+ this.getWarnings(extName, extManifest),
191
+ ]);
192
+ if (errors.length) {
193
+ delete exts[extName];
194
+ }
195
+ errorMap.set(extName, errors);
196
+ warningMap.set(extName, warnings);
197
+ }
198
+
199
+ const {errorSummaries, warningSummaries} = this.getValidationResultSummaries(
200
+ errorMap,
201
+ warningMap
202
+ );
203
+
204
+ if (!_.isEmpty(errorSummaries)) {
205
+ log.error(
206
+ `Appium encountered ${util.pluralize(
207
+ 'error',
208
+ errorSummaries.length,
209
+ true
210
+ )} while validating ${this.configKey} found in manifest ${this.manifestPath}`
211
+ );
212
+ for (const summary of errorSummaries) {
213
+ log.error(summary);
214
+ }
215
+ } else {
216
+ // only display warnings if there are no errors!
217
+
218
+ if (!_.isEmpty(warningSummaries)) {
219
+ log.warn(
220
+ `Appium encountered ${util.pluralize(
221
+ 'warning',
222
+ warningSummaries.length,
223
+ true
224
+ )} while validating ${this.configKey} found in manifest ${this.manifestPath}`
225
+ );
226
+ for (const summary of warningSummaries) {
227
+ log.warn(summary);
228
+ }
229
+ }
230
+ }
231
+ return exts;
232
+ }
233
+
234
+ /**
235
+ * Retrieves listing data for extensions via command class.
236
+ * Caches the result in {@linkcode ExtensionConfig._listDataCache}
237
+ * @protected
238
+ * @returns {Promise<ExtensionListData>}
239
+ */
240
+ async getListData() {
241
+ if (this._listDataCache) {
242
+ return this._listDataCache;
243
+ }
244
+ const CommandClass = /** @type {ExtCommand<ExtType>} */ (commandClasses[this.extensionType]);
245
+ const cmd = new CommandClass({config: this, json: true});
246
+ const listData = await cmd.list({showInstalled: true, showUpdates: true});
247
+ this._listDataCache = listData;
248
+ return listData;
249
+ }
250
+
251
+ /**
252
+ * Returns a list of warnings for a particular extension.
253
+ *
254
+ * By definition, a non-empty list of warnings does _not_ imply the extension cannot be loaded,
255
+ * but it may not work as expected or otherwise throw an exception at runtime.
256
+ *
257
+ * @param {ExtManifest<ExtType>} extManifest
258
+ * @param {ExtName<ExtType>} extName
259
+ * @returns {Promise<string[]>}
260
+ */
261
+ async getGenericConfigWarnings(extManifest, extName) {
262
+ const {appiumVersion, installSpec, installType, pkgName} = extManifest;
263
+ const warnings = [];
264
+
265
+ const invalidFields = [];
266
+ if (!_.isString(installSpec)) {
267
+ invalidFields.push('installSpec');
268
+ }
269
+
270
+ if (!INSTALL_TYPES.has(installType)) {
271
+ invalidFields.push('installType');
272
+ }
273
+
274
+ const extTypeText = _.capitalize(this.extensionType);
275
+
276
+ if (invalidFields.length) {
277
+ const invalidFieldsEnumerationText = util.pluralize(
278
+ 'invalid or missing field',
279
+ invalidFields.length,
280
+ true
281
+ );
282
+ const invalidFieldsText = invalidFields.map((field) => `"${field}"`).join(', ');
283
+
284
+ warnings.push(
285
+ `${extTypeText} "${extName}" (package \`${pkgName}\`) has ${invalidFieldsEnumerationText} (${invalidFieldsText}) in \`extensions.yaml\`; this may cause upgrades done via the \`appium\` CLI tool to fail. Please reinstall with \`appium ${this.extensionType} uninstall ${extName}\` and \`appium ${this.extensionType} install ${extName}\` to attempt a fix.`
286
+ );
287
+ }
288
+
289
+ /**
290
+ * Helps concatenate warning messages related to peer dependencies
291
+ * @param {string} reason
292
+ * @returns string
293
+ */
294
+ const createPeerWarning = (reason) =>
295
+ `${extTypeText} "${extName}" (package \`${pkgName}\`) may be incompatible with the current version of Appium (v${APPIUM_VER}) due to ${reason}`;
296
+
297
+ if (_.isString(appiumVersion) && !satisfies(APPIUM_VER, appiumVersion)) {
298
+ const listData = await this.getListData();
299
+ const extListData = /** @type {InstalledExtensionListData} */ (listData[extName]);
300
+ if (extListData?.installed) {
301
+ const {updateVersion, upToDate} = extListData;
302
+ if (!upToDate) {
303
+ warnings.push(
304
+ createPeerWarning(
305
+ `its peer dependency on older Appium v${appiumVersion}. Please upgrade \`${pkgName}\` to v${updateVersion} or newer.`
306
+ )
307
+ );
308
+ } else {
309
+ warnings.push(
310
+ createPeerWarning(
311
+ `its peer dependency on older Appium v${appiumVersion}. Please ask the developer of \`${pkgName}\` to update the peer dependency on Appium to v${APPIUM_VER}.`
312
+ )
313
+ );
314
+ }
315
+ }
316
+ } else if (!_.isString(appiumVersion)) {
317
+ const listData = await this.getListData();
318
+ const extListData = /** @type {InstalledExtensionListData} */ (listData[extName]);
319
+ if (!extListData?.upToDate && extListData?.updateVersion) {
320
+ warnings.push(
321
+ createPeerWarning(
322
+ `an invalid or missing peer dependency on Appium. A newer version of \`${pkgName}\` is available; please attempt to upgrade "${extName}" to v${extListData.updateVersion} or newer.`
323
+ )
324
+ );
325
+ } else {
326
+ warnings.push(
327
+ createPeerWarning(
328
+ `an invalid or missing peer dependency on Appium. Please ask the developer of \`${pkgName}\` to add a peer dependency on \`^appium@${APPIUM_VER}\`.`
329
+ )
330
+ );
331
+ }
332
+ }
333
+ return warnings;
334
+ }
335
+ /**
336
+ * Returns list of unrecoverable errors (if any) for the given extension _if_ it has a `schema` property.
337
+ *
338
+ * @param {ExtManifest<ExtType>} extManifest - Extension data (from manifest)
339
+ * @param {ExtName<ExtType>} extName - Extension name (from manifest)
340
+ * @returns {ExtManifestProblem[]}
341
+ */
342
+ getSchemaProblems(extManifest, extName) {
343
+ /** @type {ExtManifestProblem[]} */
344
+ const problems = [];
345
+ const {schema: argSchemaPath} = extManifest;
346
+ if (ExtensionConfig.extDataHasSchema(extManifest)) {
347
+ if (_.isString(argSchemaPath)) {
348
+ if (isAllowedSchemaFileExtension(argSchemaPath)) {
349
+ try {
350
+ this.readExtensionSchema(extName, extManifest);
351
+ } catch (err) {
352
+ problems.push({
353
+ err: `Unable to register schema at path ${argSchemaPath}; ${err.message}`,
354
+ val: argSchemaPath,
355
+ });
356
+ }
357
+ } else {
358
+ problems.push({
359
+ err: `Schema file has unsupported extension. Allowed: ${[
360
+ ...ALLOWED_SCHEMA_EXTENSIONS,
361
+ ].join(', ')}`,
362
+ val: argSchemaPath,
363
+ });
364
+ }
365
+ } else if (_.isPlainObject(argSchemaPath)) {
366
+ try {
367
+ this.readExtensionSchema(extName, extManifest);
368
+ } catch (err) {
369
+ problems.push({
370
+ err: `Unable to register embedded schema; ${err.message}`,
371
+ val: argSchemaPath,
372
+ });
373
+ }
374
+ } else {
375
+ problems.push({
376
+ err: 'Incorrectly formatted schema field; must be a path to a schema file or a schema object.',
377
+ val: argSchemaPath,
378
+ });
379
+ }
380
+ }
381
+ return problems;
382
+ }
383
+
384
+ /**
385
+ * Return a list of generic unrecoverable errors for the given extension
386
+ * @param {ExtManifest<ExtType>} extManifest - Extension data (from manifest)
387
+ * @param {ExtName<ExtType>} extName - Extension name (from manifest)
388
+ * @returns {ExtManifestProblem[]}
389
+ */
390
+ // eslint-disable-next-line no-unused-vars
391
+ getGenericConfigProblems(extManifest, extName) {
392
+ const {version, pkgName, mainClass} = extManifest;
393
+ const problems = [];
394
+
395
+ if (!_.isString(version)) {
396
+ problems.push({
397
+ err: `Invalid or missing \`version\` field in my \`package.json\` and/or \`extensions.yaml\` (must be a string)`,
398
+ val: version,
399
+ });
400
+ }
401
+
402
+ if (!_.isString(pkgName)) {
403
+ problems.push({
404
+ err: `Invalid or missing \`name\` field in my \`package.json\` and/or \`extensions.yaml\` (must be a string)`,
405
+ val: pkgName,
406
+ });
407
+ }
408
+
409
+ if (!_.isString(mainClass)) {
410
+ problems.push({
411
+ err: `Invalid or missing \`appium.mainClass\` field in my \`package.json\` and/or \`mainClass\` field in \`extensions.yaml\` (must be a string)`,
412
+ val: mainClass,
413
+ });
414
+ }
415
+
416
+ return problems;
417
+ }
418
+
419
+ /**
420
+ * @abstract
421
+ * @param {ExtManifest<ExtType>} extManifest
422
+ * @param {ExtName<ExtType>} extName
423
+ * @returns {ExtManifestProblem[]}
424
+ */
425
+ // eslint-disable-next-line no-unused-vars
426
+ getConfigProblems(extManifest, extName) {
427
+ // shoud override this method if special validation is necessary for this extension type
428
+ return [];
429
+ }
430
+
431
+ /**
432
+ * @param {string} extName
433
+ * @param {ExtManifest<ExtType>} extManifest
434
+ * @param {ExtensionConfigMutationOpts} [opts]
435
+ * @returns {Promise<void>}
436
+ */
437
+ async addExtension(extName, extManifest, {write = true} = {}) {
438
+ this.manifest.addExtension(this.extensionType, extName, extManifest);
439
+ if (write) {
440
+ await this.manifest.write();
441
+ }
442
+ }
443
+
444
+ /**
445
+ * @param {ExtName<ExtType>} extName
446
+ * @param {ExtManifest<ExtType>|import('../cli/extension-command').ExtensionFields<ExtType>} extManifest
447
+ * @param {ExtensionConfigMutationOpts} [opts]
448
+ * @returns {Promise<void>}
449
+ */
450
+ async updateExtension(extName, extManifest, {write = true} = {}) {
451
+ this.installedExtensions[extName] = {
452
+ ...this.installedExtensions[extName],
453
+ ...extManifest,
454
+ };
455
+ if (write) {
456
+ await this.manifest.write();
457
+ }
458
+ }
459
+
460
+ /**
461
+ * Remove an extension from the list of installed extensions, and optionally avoid a write to the manifest file.
462
+ *
463
+ * @param {ExtName<ExtType>} extName
464
+ * @param {ExtensionConfigMutationOpts} [opts]
465
+ * @returns {Promise<void>}
466
+ */
467
+ async removeExtension(extName, {write = true} = {}) {
468
+ delete this.installedExtensions[extName];
469
+ if (write) {
470
+ await this.manifest.write();
471
+ }
472
+ }
473
+
474
+ /**
475
+ * @param {ExtName<ExtType>[]} [activeNames]
476
+ * @returns {void}
477
+ */
478
+ // eslint-disable-next-line no-unused-vars
479
+ print(activeNames) {
480
+ if (_.isEmpty(this.installedExtensions)) {
481
+ log.info(
482
+ `No ${this.configKey} have been installed in ${this.appiumHome}. Use the "appium ${this.extensionType}" ` +
483
+ 'command to install the one(s) you want to use.'
484
+ );
485
+ return;
486
+ }
487
+
488
+ log.info(`Available ${this.configKey}:`);
489
+ for (const [extName, extManifest] of /** @type {[string, ExtManifest<ExtType>][]} */ (
490
+ _.toPairs(this.installedExtensions)
491
+ )) {
492
+ log.info(` - ${this.extensionDesc(extName, extManifest)}`);
493
+ }
494
+ }
495
+
496
+ /**
497
+ * Returns a string describing the extension. Subclasses must implement.
498
+ * @param {ExtName<ExtType>} extName - Extension name
499
+ * @param {ExtManifest<ExtType>} extManifest - Extension data
500
+ * @returns {string}
501
+ * @abstract
502
+ */
503
+ // eslint-disable-next-line no-unused-vars
504
+ extensionDesc(extName, extManifest) {
505
+ throw new Error('This must be implemented in a subclass');
506
+ }
507
+
508
+ /**
509
+ * @param {string} extName
510
+ * @returns {string}
511
+ */
512
+ getInstallPath(extName) {
513
+ return path.join(this.appiumHome, 'node_modules', this.installedExtensions[extName].pkgName);
514
+ }
515
+
516
+ /**
517
+ * Loads extension and returns its main class (constructor)
518
+ * @param {ExtName<ExtType>} extName
519
+ * @returns {ExtClass<ExtType>}
520
+ */
521
+ require(extName) {
522
+ const {mainClass} = this.installedExtensions[extName];
523
+ const reqPath = this.getInstallPath(extName);
524
+ /** @type {string} */
525
+ let reqResolved;
526
+ try {
527
+ reqResolved = require.resolve(reqPath);
528
+ } catch (err) {
529
+ throw new ReferenceError(`Could not find a ${this.extensionType} installed at ${reqPath}`);
530
+ }
531
+ // note: this will only reload the entry point
532
+ if (process.env.APPIUM_RELOAD_EXTENSIONS && require.cache[reqResolved]) {
533
+ log.debug(`Removing ${reqResolved} from require cache`);
534
+ delete require.cache[reqResolved];
535
+ }
536
+ log.debug(`Requiring ${this.extensionType} at ${reqPath}`);
537
+ const MainClass = require(reqPath)[mainClass];
538
+ if (!MainClass) {
539
+ throw new ReferenceError(
540
+ `Could not find a class named "${mainClass}" exported by ${this.extensionType} "${extName}"`
541
+ );
542
+ }
543
+ return MainClass;
544
+ }
545
+
546
+ /**
547
+ * @param {string} extName
548
+ * @returns {boolean}
549
+ */
550
+ isInstalled(extName) {
551
+ return _.includes(Object.keys(this.installedExtensions), extName);
552
+ }
553
+
554
+ /**
555
+ * Intended to be called by corresponding instance methods of subclass.
556
+ * @private
557
+ * @template {ExtensionType} ExtType
558
+ * @param {string} appiumHome
559
+ * @param {ExtType} extType
560
+ * @param {ExtName<ExtType>} extName - Extension name (unique to its type)
561
+ * @param {ExtManifestWithSchema<ExtType>} extManifest - Extension config
562
+ * @returns {import('ajv').SchemaObject|undefined}
563
+ */
564
+ static _readExtensionSchema(appiumHome, extType, extName, extManifest) {
565
+ const {pkgName, schema: argSchemaPath} = extManifest;
566
+ if (!argSchemaPath) {
567
+ throw new TypeError(
568
+ `No \`schema\` property found in config for ${extType} ${pkgName} -- why is this function being called?`
569
+ );
570
+ }
571
+ let moduleObject;
572
+ if (_.isString(argSchemaPath)) {
573
+ const schemaPath = resolveFrom(appiumHome, path.join(pkgName, argSchemaPath));
574
+ moduleObject = require(schemaPath);
575
+ } else {
576
+ moduleObject = argSchemaPath;
577
+ }
578
+ // this sucks. default exports should be destroyed
579
+ const schema = moduleObject.__esModule ? moduleObject.default : moduleObject;
580
+ registerSchema(extType, extName, schema);
581
+ return schema;
582
+ }
583
+
584
+ /**
585
+ * Returns `true` if a specific {@link ExtManifest} object has a `schema` prop.
586
+ * The {@link ExtManifest} object becomes a {@link ExtManifestWithSchema} object.
587
+ * @template {ExtensionType} ExtType
588
+ * @param {ExtManifest<ExtType>} extManifest
589
+ * @returns {extManifest is ExtManifestWithSchema<ExtType>}
590
+ */
591
+ static extDataHasSchema(extManifest) {
592
+ return _.isString(extManifest?.schema) || _.isObject(extManifest?.schema);
593
+ }
594
+
595
+ /**
596
+ * If an extension provides a schema, this will load the schema and attempt to
597
+ * register it with the schema registrar.
598
+ * @param {ExtName<ExtType>} extName - Name of extension
599
+ * @param {ExtManifestWithSchema<ExtType>} extManifest - Extension data
600
+ * @returns {import('ajv').SchemaObject|undefined}
601
+ */
602
+ readExtensionSchema(extName, extManifest) {
603
+ return ExtensionConfig._readExtensionSchema(
604
+ this.appiumHome,
605
+ this.extensionType,
606
+ extName,
607
+ extManifest
608
+ );
609
+ }
610
+ }
611
+
612
+ export {INSTALL_TYPE_NPM, INSTALL_TYPE_GIT, INSTALL_TYPE_LOCAL, INSTALL_TYPE_GITHUB, INSTALL_TYPES};
613
+
614
+ /**
615
+ * An issue with the {@linkcode ExtManifest} for a particular extension.
616
+ *
617
+ * The existance of such an object implies that the extension cannot be loaded.
618
+ * @typedef ExtManifestProblem
619
+ * @property {string} err - Error message
620
+ * @property {any} val - Associated value
621
+ */
622
+
623
+ /**
624
+ * An optional logging function provided to an {@link ExtensionConfig} subclass.
625
+ * @callback ExtensionLogFn
626
+ * @param {...any} args
627
+ * @returns {void}
628
+ */
629
+
630
+ /**
631
+ * @typedef {import('@appium/types').ExtensionType} ExtensionType
632
+ * @typedef {import('./manifest').Manifest} Manifest
633
+ */
634
+
635
+ /**
636
+ * @template T
637
+ * @typedef {import('appium/types').ExtManifest<T>} ExtManifest
638
+ */
639
+
640
+ /**
641
+ * @template T
642
+ * @typedef {import('appium/types').ExtManifestWithSchema<T>} ExtManifestWithSchema
643
+ */
644
+
645
+ /**
646
+ * @template T
647
+ * @typedef {import('appium/types').ExtName<T>} ExtName
648
+ */
649
+
650
+ /**
651
+ * @template T
652
+ * @typedef {import('appium/types').ExtClass<T>} ExtClass
653
+ */
654
+
655
+ /**
656
+ * @template T
657
+ * @typedef {import('appium/types').ExtRecord<T>} ExtRecord
658
+ */
659
+
660
+ /**
661
+ * @template T
662
+ * @typedef {import('../cli/extension').ExtCommand<T>} ExtCommand
663
+ */
664
+
665
+ /**
666
+ * Options for various methods in {@link ExtensionConfig}
667
+ * @typedef ExtensionConfigMutationOpts
668
+ * @property {boolean} [write=true] Whether or not to write the manifest to disk after a mutation operation
669
+ */
670
+
671
+ /**
672
+ * A valid install type
673
+ * @typedef {typeof INSTALL_TYPE_NPM | typeof INSTALL_TYPE_GIT | typeof INSTALL_TYPE_LOCAL | typeof INSTALL_TYPE_GITHUB} InstallType
674
+ */
675
+
676
+ /**
677
+ * @typedef {import('../cli/extension-command').ExtensionListData} ExtensionListData
678
+ * @typedef {import('../cli/extension-command').InstalledExtensionListData} InstalledExtensionListData
679
+ */