appium 2.0.0-beta.5 → 2.0.0-beta.53

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