appium 2.0.0-beta.3 → 2.0.0-beta.30

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 (139) hide show
  1. package/README.md +10 -11
  2. package/build/lib/appium.d.ts +215 -0
  3. package/build/lib/appium.d.ts.map +1 -0
  4. package/build/lib/appium.js +238 -132
  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 +19 -12
  11. package/build/lib/cli/extension-command.d.ts +345 -0
  12. package/build/lib/cli/extension-command.d.ts.map +1 -0
  13. package/build/lib/cli/extension-command.js +171 -96
  14. package/build/lib/cli/extension.d.ts +14 -0
  15. package/build/lib/cli/extension.d.ts.map +1 -0
  16. package/build/lib/cli/extension.js +31 -16
  17. package/build/lib/cli/parser.d.ts +79 -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 +39 -0
  21. package/build/lib/cli/plugin-command.d.ts.map +1 -0
  22. package/build/lib/cli/plugin-command.js +18 -13
  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 +40 -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 +84 -0
  36. package/build/lib/extension/driver-config.d.ts.map +1 -0
  37. package/build/lib/extension/driver-config.js +190 -0
  38. package/build/lib/extension/extension-config.d.ts +170 -0
  39. package/build/lib/extension/extension-config.d.ts.map +1 -0
  40. package/build/lib/extension/extension-config.js +297 -0
  41. package/build/lib/extension/index.d.ts +39 -0
  42. package/build/lib/extension/index.d.ts.map +1 -0
  43. package/build/lib/extension/index.js +77 -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 +246 -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 +62 -0
  51. package/build/lib/extension/plugin-config.d.ts.map +1 -0
  52. package/build/lib/extension/plugin-config.js +87 -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 +12 -16
  62. package/build/lib/main.d.ts +51 -0
  63. package/build/lib/main.d.ts.map +1 -0
  64. package/build/lib/main.js +174 -82
  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/index.js +11 -0
  88. package/lib/appium-config.schema.json +278 -0
  89. package/lib/appium.js +398 -155
  90. package/lib/cli/args.js +174 -377
  91. package/lib/cli/driver-command.js +22 -7
  92. package/lib/cli/extension-command.js +372 -177
  93. package/lib/cli/extension.js +32 -10
  94. package/lib/cli/parser.js +252 -83
  95. package/lib/cli/plugin-command.js +19 -6
  96. package/lib/cli/utils.js +22 -2
  97. package/lib/config-file.js +223 -0
  98. package/lib/config.js +169 -69
  99. package/lib/constants.js +78 -0
  100. package/lib/extension/driver-config.js +249 -0
  101. package/lib/extension/extension-config.js +458 -0
  102. package/lib/extension/index.js +102 -0
  103. package/lib/extension/manifest.js +486 -0
  104. package/lib/extension/package-changed.js +63 -0
  105. package/lib/extension/plugin-config.js +113 -0
  106. package/lib/grid-register.js +25 -22
  107. package/lib/logger.js +1 -1
  108. package/lib/logsink.js +14 -7
  109. package/lib/main.js +233 -83
  110. package/lib/schema/arg-spec.js +232 -0
  111. package/lib/schema/cli-args.js +261 -0
  112. package/lib/schema/cli-transformers.js +122 -0
  113. package/lib/schema/index.js +2 -0
  114. package/lib/schema/keywords.js +134 -0
  115. package/lib/schema/schema.js +734 -0
  116. package/lib/utils.js +85 -129
  117. package/package.json +62 -85
  118. package/scripts/postinstall.js +71 -0
  119. package/types/appium-manifest.d.ts +61 -0
  120. package/types/cli.d.ts +134 -0
  121. package/types/extension.d.ts +56 -0
  122. package/types/external-manifest.d.ts +58 -0
  123. package/types/index.d.ts +7 -0
  124. package/CHANGELOG.md +0 -3515
  125. package/bin/ios-webkit-debug-proxy-launcher.js +0 -71
  126. package/build/lib/cli/npm.js +0 -206
  127. package/build/lib/cli/parser-helpers.js +0 -82
  128. package/build/lib/driver-config.js +0 -77
  129. package/build/lib/drivers.js +0 -96
  130. package/build/lib/extension-config.js +0 -251
  131. package/build/lib/plugin-config.js +0 -59
  132. package/build/lib/plugins.js +0 -14
  133. package/lib/cli/npm.js +0 -183
  134. package/lib/cli/parser-helpers.js +0 -79
  135. package/lib/driver-config.js +0 -46
  136. package/lib/drivers.js +0 -81
  137. package/lib/extension-config.js +0 -208
  138. package/lib/plugin-config.js +0 -34
  139. package/lib/plugins.js +0 -10
@@ -0,0 +1,249 @@
1
+
2
+ import _ from 'lodash';
3
+ import { DRIVER_TYPE } from '../constants';
4
+ import log from '../logger';
5
+ import { ExtensionConfig } from './extension-config';
6
+
7
+ /**
8
+ * @extends {ExtensionConfig<DriverType>}
9
+ */
10
+ export class DriverConfig extends ExtensionConfig {
11
+
12
+ /**
13
+ * A set of unique automation names used by drivers.
14
+ * @type {Set<string>}
15
+ */
16
+ knownAutomationNames;
17
+
18
+ /**
19
+ * A mapping of {@link Manifest} instances to {@link DriverConfig} instances.
20
+ *
21
+ * `Manifest` and `ExtensionConfig` have a one-to-many relationship; each `Manifest` should be associated with a `DriverConfig` and a `PluginConfig`; no more, no less.
22
+ *
23
+ * This variable tracks the `Manifest`-to-`DriverConfig` portion.
24
+ *
25
+ * @type {WeakMap<Manifest,DriverConfig>}
26
+ * @private
27
+ */
28
+ static _instances = new WeakMap();
29
+
30
+ /**
31
+ * Call {@link DriverConfig.create} instead.
32
+ * @private
33
+ * @param {import('./manifest').Manifest} manifest - Manifest instance
34
+ * @param {DriverConfigOptions} [opts]
35
+ */
36
+ constructor (manifest, {logFn, extData} = {}) {
37
+ super(DRIVER_TYPE, manifest, logFn);
38
+
39
+ this.knownAutomationNames = new Set();
40
+
41
+ if (extData) {
42
+ this.validate(extData);
43
+ }
44
+ }
45
+
46
+ /**
47
+ * Creates a new {@link DriverConfig} instance for a {@link Manifest} instance.
48
+ *
49
+ * @param {Manifest} manifest
50
+ * @param {DriverConfigOptions} [opts]
51
+ * @throws If `manifest` already associated with a `DriverConfig`
52
+ * @returns {DriverConfig}
53
+ */
54
+ static create (manifest, {extData, logFn} = {}) {
55
+ const instance = new DriverConfig(manifest, {logFn, extData});
56
+ if (DriverConfig.getInstance(manifest)) {
57
+ throw new Error(`Manifest with APPIUM_HOME ${manifest.appiumHome} already has a DriverConfig; use DriverConfig.getInstance() to retrieve it.`);
58
+ }
59
+ DriverConfig._instances.set(manifest, instance);
60
+ return instance;
61
+ }
62
+
63
+ /**
64
+ * Returns a DriverConfig associated with a Manifest
65
+ * @param {Manifest} manifest
66
+ * @returns {DriverConfig|undefined}
67
+ */
68
+ static getInstance (manifest) {
69
+ return DriverConfig._instances.get(manifest);
70
+ }
71
+
72
+ /**
73
+ * Checks extensions for problems
74
+ * @param {ExtRecord<DriverType>} exts
75
+ */
76
+ validate (exts) {
77
+ this.knownAutomationNames.clear();
78
+ return super.validate(exts);
79
+ }
80
+
81
+ /**
82
+ * @param {ExtManifest<DriverType>} extData
83
+ * @returns {import('./extension-config').Problem[]}
84
+ */
85
+ getConfigProblems (extData) {
86
+ const problems = [];
87
+ const {platformNames, automationName} = extData;
88
+
89
+ if (!_.isArray(platformNames)) {
90
+ problems.push({
91
+ err: 'Missing or incorrect supported platformNames list.',
92
+ val: platformNames
93
+ });
94
+ } else {
95
+ if (_.isEmpty(platformNames)) {
96
+ problems.push({
97
+ err: 'Empty platformNames list.',
98
+ val: platformNames
99
+ });
100
+ } else {
101
+ for (const pName of platformNames) {
102
+ if (!_.isString(pName)) {
103
+ problems.push({err: 'Incorrectly formatted platformName.', val: pName});
104
+ }
105
+ }
106
+ }
107
+ }
108
+
109
+ if (!_.isString(automationName)) {
110
+ problems.push({err: 'Missing or incorrect automationName', val: automationName});
111
+ }
112
+
113
+ if (this.knownAutomationNames.has(automationName)) {
114
+ problems.push({
115
+ err: 'Multiple drivers claim support for the same automationName',
116
+ val: automationName
117
+ });
118
+ }
119
+
120
+ // should we retain the name at the end of this function, once we've checked there are no problems?
121
+ this.knownAutomationNames.add(automationName);
122
+
123
+ return problems;
124
+ }
125
+
126
+ /**
127
+ * @param {ExtName<DriverType>} driverName
128
+ * @param {ExtManifest<DriverType>} extData
129
+ * @returns {string}
130
+ */
131
+ extensionDesc (driverName, {version, automationName}) {
132
+ return `${driverName}@${version} (automationName '${automationName}')`;
133
+ }
134
+
135
+ /**
136
+ * Given capabilities, find a matching driver within the config. Load its class and return it along with version and driver name.
137
+ * @param {Capabilities} caps
138
+ * @returns {MatchedDriver}
139
+ */
140
+ findMatchingDriver ({automationName, platformName}) {
141
+ if (!_.isString(platformName)) {
142
+ throw new Error('You must include a platformName capability');
143
+ }
144
+
145
+ if (!_.isString(automationName)) {
146
+ throw new Error('You must include an automationName capability');
147
+ }
148
+
149
+ log.info(`Attempting to find matching driver for automationName ` +
150
+ `'${automationName}' and platformName '${platformName}'`);
151
+
152
+ try {
153
+ const {
154
+ driverName,
155
+ mainClass,
156
+ version,
157
+ } = this._getDriverBySupport(automationName, platformName);
158
+ log.info(`The '${driverName}' driver was installed and matched caps.`);
159
+ log.info(`Will require it at ${this.getInstallPath(driverName)}`);
160
+ const driver = this.require(driverName);
161
+ if (!driver) {
162
+ throw new Error(`Driver '${driverName}' did not export a class with name '${mainClass}'. Contact the author of the driver!`);
163
+ }
164
+ return {driver, version, driverName};
165
+ } catch (err) {
166
+ const msg = `Could not find a driver for automationName ` +
167
+ `'${automationName}' and platformName ${platformName}'. ` +
168
+ `Have you installed a driver that supports those ` +
169
+ `capabilities? Run 'appium driver list --installed' to see. ` +
170
+ `(Lower-level error: ${err.message})`;
171
+ throw new Error(msg);
172
+ }
173
+ }
174
+
175
+ /**
176
+ * Given an automation name and platform name, find a suitable driver and return its extension data.
177
+ * @param {string} matchAutomationName
178
+ * @param {string} matchPlatformName
179
+ * @returns {ExtMetadata<DriverType> & import('../../types/appium-manifest').InternalMetadata & import('../../types/external-manifest').CommonMetadata}
180
+ */
181
+ _getDriverBySupport (matchAutomationName, matchPlatformName) {
182
+ const drivers = this.installedExtensions;
183
+ for (const [driverName, driverData] of _.toPairs(drivers)) {
184
+ const {automationName, platformNames} = driverData;
185
+ const aNameMatches = automationName.toLowerCase() === matchAutomationName.toLowerCase();
186
+ const pNameMatches = _.includes(platformNames.map(_.toLower),
187
+ matchPlatformName.toLowerCase());
188
+
189
+ if (aNameMatches && pNameMatches) {
190
+ return {driverName, ...driverData};
191
+ }
192
+
193
+ if (aNameMatches) {
194
+ throw new Error(`Driver '${driverName}' supports automationName ` +
195
+ `'${automationName}', but Appium could not find ` +
196
+ `support for platformName '${matchPlatformName}'. Supported ` +
197
+ `platformNames are: ` +
198
+ JSON.stringify(platformNames));
199
+ }
200
+ }
201
+
202
+ throw new Error(`Could not find installed driver to support given caps`);
203
+ }
204
+ }
205
+
206
+ /**
207
+ * @typedef DriverConfigOptions
208
+ * @property {import('./extension-config').ExtensionLogFn} [logFn] - Optional logging function
209
+ * @property {ManifestData['drivers']} [extData] - Extension data
210
+ */
211
+
212
+ /**
213
+ * @template T
214
+ * @typedef {import('../../types').ExtMetadata<T>} ExtMetadata
215
+ */
216
+
217
+ /**
218
+ * @template T
219
+ * @typedef {import('../../types').ExtManifest<T>} ExtManifest
220
+ */
221
+
222
+ /**
223
+ * @typedef {import('../../types').ManifestData} ManifestData
224
+ * @typedef {import('../../types').DriverType} DriverType
225
+ * @typedef {import('./manifest').Manifest} Manifest
226
+ */
227
+
228
+ /**
229
+ * @template T
230
+ * @typedef {import('../../types').ExtRecord<T>} ExtRecord
231
+ */
232
+
233
+ /**
234
+ * @template T
235
+ * @typedef {import('../../types').ExtName<T>} ExtName
236
+ */
237
+
238
+
239
+ /**
240
+ * Return value of {@linkcode DriverConfig.findMatchingDriver}
241
+ * @typedef MatchedDriver
242
+ * @property {import('../../types/extension').DriverClass} driver
243
+ * @property {string} version
244
+ * @property {string} driverName
245
+ */
246
+
247
+ /**
248
+ * @typedef {import('@appium/types').Capabilities} Capabilities
249
+ */
@@ -0,0 +1,458 @@
1
+
2
+ import _ from 'lodash';
3
+ import path from 'path';
4
+ import resolveFrom from 'resolve-from';
5
+ import log from '../logger';
6
+ import {
7
+ ALLOWED_SCHEMA_EXTENSIONS,
8
+ isAllowedSchemaFileExtension,
9
+ registerSchema,
10
+ } from '../schema/schema';
11
+
12
+ const INSTALL_TYPE_NPM = 'npm';
13
+ const INSTALL_TYPE_LOCAL = 'local';
14
+ const INSTALL_TYPE_GITHUB = 'github';
15
+ const INSTALL_TYPE_GIT = 'git';
16
+
17
+ /** @type {Set<InstallType>} */
18
+ const INSTALL_TYPES = new Set([
19
+ INSTALL_TYPE_GIT,
20
+ INSTALL_TYPE_GITHUB,
21
+ INSTALL_TYPE_LOCAL,
22
+ INSTALL_TYPE_NPM,
23
+ ]);
24
+
25
+ /**
26
+ * This class is abstract. It should not be instantiated directly.
27
+ *
28
+ * Subclasses should provide the generic parameter to implement.
29
+ * @template {ExtensionType} ExtType
30
+ */
31
+ export class ExtensionConfig {
32
+ /** @type {ExtType} */
33
+ extensionType;
34
+
35
+ /** @type {`${ExtType}s`} */
36
+ configKey;
37
+
38
+ /** @type {ExtRecord<ExtType>} */
39
+ installedExtensions;
40
+
41
+ /** @type {ExtensionLogFn} */
42
+ log;
43
+
44
+ /** @type {Manifest} */
45
+ manifest;
46
+
47
+ /**
48
+ * @protected
49
+ * @param {ExtType} extensionType - Type of extension
50
+ * @param {Manifest} manifest - `Manifest` instance
51
+ * @param {ExtensionLogFn} [logFn]
52
+ */
53
+ constructor (extensionType, manifest, logFn) {
54
+ const logger = _.isFunction(logFn) ? logFn : log.error.bind(log);
55
+ this.extensionType = extensionType;
56
+ this.configKey = `${extensionType}s`;
57
+ this.installedExtensions = manifest.getExtensionData(extensionType);
58
+ this.log = logger;
59
+ this.manifest = manifest;
60
+ }
61
+
62
+ get manifestPath () {
63
+ return this.manifest.manifestPath;
64
+ }
65
+
66
+ get appiumHome () {
67
+ return this.manifest.appiumHome;
68
+ }
69
+
70
+ /**
71
+ * Checks extensions for problems
72
+ * @param {ExtRecord<ExtType>} exts - Extension data
73
+ */
74
+ validate (exts) {
75
+ const foundProblems =
76
+ /** @type {Record<ExtName<ExtType>,Problem[]>} */ ({});
77
+ for (const [
78
+ extName,
79
+ extData,
80
+ ] of /** @type {[ExtName<ExtType>, ExtManifest<ExtType>][]} */ (
81
+ _.toPairs(exts)
82
+ )) {
83
+ foundProblems[extName] = [
84
+ ...this.getGenericConfigProblems(extData, extName),
85
+ ...this.getConfigProblems(extData),
86
+ ...this.getSchemaProblems(extData, extName),
87
+ ];
88
+ }
89
+
90
+ const problemSummaries = [];
91
+ for (const [extName, problems] of _.toPairs(foundProblems)) {
92
+ if (_.isEmpty(problems)) {
93
+ continue;
94
+ }
95
+ // remove this extension from the list since it's not valid
96
+ delete exts[extName];
97
+ problemSummaries.push(
98
+ `${this.extensionType} ${extName} had errors and will not ` +
99
+ `be available. Errors:`,
100
+ );
101
+ for (const problem of problems) {
102
+ problemSummaries.push(
103
+ ` - ${problem.err} (Actual value: ` +
104
+ `${JSON.stringify(problem.val)})`,
105
+ );
106
+ }
107
+ }
108
+
109
+ if (!_.isEmpty(problemSummaries)) {
110
+ this.log(
111
+ `Appium encountered one or more errors while validating ` +
112
+ `the ${this.configKey} extension file (${this.manifestPath}):`,
113
+ );
114
+ for (const summary of problemSummaries) {
115
+ this.log(summary);
116
+ }
117
+ }
118
+
119
+ return exts;
120
+ }
121
+
122
+ /**
123
+ * @param {ExtManifest<ExtType>} extData
124
+ * @param {ExtName<ExtType>} extName
125
+ * @returns {Problem[]}
126
+ */
127
+ getSchemaProblems (extData, extName) {
128
+ const problems = [];
129
+ const {schema: argSchemaPath} = extData;
130
+ if (ExtensionConfig.extDataHasSchema(extData)) {
131
+ if (_.isString(argSchemaPath)) {
132
+ if (isAllowedSchemaFileExtension(argSchemaPath)) {
133
+ try {
134
+ this.readExtensionSchema(extName, extData);
135
+ } catch (err) {
136
+ problems.push({
137
+ err: `Unable to register schema at path ${argSchemaPath}; ${err.message}`,
138
+ val: argSchemaPath,
139
+ });
140
+ }
141
+ } else {
142
+ problems.push({
143
+ err: `Schema file has unsupported extension. Allowed: ${[
144
+ ...ALLOWED_SCHEMA_EXTENSIONS,
145
+ ].join(', ')}`,
146
+ val: argSchemaPath,
147
+ });
148
+ }
149
+ } else if (_.isPlainObject(argSchemaPath)) {
150
+ try {
151
+ this.readExtensionSchema(extName, extData);
152
+ } catch (err) {
153
+ problems.push({
154
+ err: `Unable to register embedded schema; ${err.message}`,
155
+ val: argSchemaPath,
156
+ });
157
+ }
158
+ } else {
159
+ problems.push({
160
+ err: 'Incorrectly formatted schema field; must be a path to a schema file or a schema object.',
161
+ val: argSchemaPath,
162
+ });
163
+ }
164
+ }
165
+ return problems;
166
+ }
167
+
168
+ /**
169
+ * @param {ExtManifest<ExtType>} extData
170
+ * @param {ExtName<ExtType>} extName
171
+ * @returns {Problem[]}
172
+ */
173
+ // eslint-disable-next-line no-unused-vars
174
+ getGenericConfigProblems (extData, extName) {
175
+ const {version, pkgName, installSpec, installType, mainClass} =
176
+ extData;
177
+ const problems = [];
178
+
179
+ if (!_.isString(version)) {
180
+ problems.push({err: 'Missing or incorrect version', val: version});
181
+ }
182
+
183
+ if (!_.isString(pkgName)) {
184
+ problems.push({
185
+ err: 'Missing or incorrect NPM package name',
186
+ val: pkgName,
187
+ });
188
+ }
189
+
190
+ if (!_.isString(installSpec)) {
191
+ problems.push({
192
+ err: 'Missing or incorrect installation spec',
193
+ val: installSpec,
194
+ });
195
+ }
196
+
197
+ if (!INSTALL_TYPES.has(installType)) {
198
+ problems.push({
199
+ err: 'Missing or incorrect install type',
200
+ val: installType,
201
+ });
202
+ }
203
+
204
+ if (!_.isString(mainClass)) {
205
+ problems.push({
206
+ err: 'Missing or incorrect driver class name',
207
+ val: mainClass,
208
+ });
209
+ }
210
+
211
+ return problems;
212
+ }
213
+
214
+ /**
215
+ * @abstract
216
+ * @param {ExtManifest<ExtType>} extData
217
+ * @returns {Problem[]}
218
+ */
219
+ // eslint-disable-next-line no-unused-vars
220
+ getConfigProblems (extData) {
221
+ // shoud override this method if special validation is necessary for this extension type
222
+ return [];
223
+ }
224
+
225
+ /**
226
+ * @param {string} extName
227
+ * @param {ExtManifest<ExtType>} extData
228
+ * @param {ExtensionConfigMutationOpts} [opts]
229
+ * @returns {Promise<void>}
230
+ */
231
+ async addExtension (extName, extData, {write = true} = {}) {
232
+ this.manifest.addExtension(this.extensionType, extName, extData);
233
+ if (write) {
234
+ await this.manifest.write();
235
+ }
236
+ }
237
+
238
+ /**
239
+ * @param {ExtName<ExtType>} extName
240
+ * @param {ExtManifest<ExtType>|import('../cli/extension-command').ExtensionFields<ExtType>} extData
241
+ * @param {ExtensionConfigMutationOpts} [opts]
242
+ * @returns {Promise<void>}
243
+ */
244
+ async updateExtension (extName, extData, {write = true} = {}) {
245
+ this.installedExtensions[extName] = {
246
+ ...this.installedExtensions[extName],
247
+ ...extData,
248
+ };
249
+ if (write) {
250
+ await this.manifest.write();
251
+ }
252
+ }
253
+
254
+ /**
255
+ * @param {ExtName<ExtType>} extName
256
+ * @param {ExtensionConfigMutationOpts} [opts]
257
+ * @returns {Promise<void>}
258
+ */
259
+ async removeExtension (extName, {write = true} = {}) {
260
+ delete this.installedExtensions[extName];
261
+ if (write) {
262
+ await this.manifest.write();
263
+ }
264
+ }
265
+
266
+ /**
267
+ * @param {ExtName<ExtType>[]} [activeNames]
268
+ * @returns {void}
269
+ */
270
+ // eslint-disable-next-line no-unused-vars
271
+ print (activeNames) {
272
+ if (_.isEmpty(this.installedExtensions)) {
273
+ log.info(
274
+ `No ${this.configKey} have been installed in ${this.appiumHome}. Use the "appium ${this.extensionType}" ` +
275
+ 'command to install the one(s) you want to use.',
276
+ );
277
+ return;
278
+ }
279
+
280
+ log.info(`Available ${this.configKey}:`);
281
+ for (const [
282
+ extName,
283
+ extData,
284
+ ] of /** @type {[string, ExtManifest<ExtType>][]} */ (
285
+ _.toPairs(this.installedExtensions)
286
+ )) {
287
+ log.info(` - ${this.extensionDesc(extName, extData)}`);
288
+ }
289
+ }
290
+
291
+ /**
292
+ * Returns a string describing the extension. Subclasses must implement.
293
+ * @param {ExtName<ExtType>} extName - Extension name
294
+ * @param {ExtManifest<ExtType>} extData - Extension data
295
+ * @returns {string}
296
+ * @abstract
297
+ */
298
+ // eslint-disable-next-line no-unused-vars
299
+ extensionDesc (extName, extData) {
300
+ throw new Error('This must be implemented in a subclass');
301
+ }
302
+
303
+ /**
304
+ * @param {string} extName
305
+ * @returns {string}
306
+ */
307
+ getInstallPath (extName) {
308
+ return path.join(this.appiumHome, 'node_modules', this.installedExtensions[extName].pkgName);
309
+ }
310
+
311
+ /**
312
+ * Loads extension and returns its main class (constructor)
313
+ * @param {ExtName<ExtType>} extName
314
+ * @returns {ExtClass<ExtType>}
315
+ */
316
+ require (extName) {
317
+ const {mainClass} = this.installedExtensions[extName];
318
+ const reqPath = this.getInstallPath(extName);
319
+ const reqResolved = require.resolve(reqPath);
320
+ // note: this will only reload the entry point
321
+ if (process.env.APPIUM_RELOAD_EXTENSIONS && require.cache[reqResolved]) {
322
+ log.debug(`Removing ${reqResolved} from require cache`);
323
+ delete require.cache[reqResolved];
324
+ }
325
+ log.debug(`Requiring ${this.extensionType} at ${reqPath}`);
326
+ return require(reqPath)[mainClass];
327
+ }
328
+
329
+ /**
330
+ * @param {string} extName
331
+ * @returns {boolean}
332
+ */
333
+ isInstalled (extName) {
334
+ return _.includes(Object.keys(this.installedExtensions), extName);
335
+ }
336
+
337
+ /**
338
+ * Intended to be called by corresponding instance methods of subclass.
339
+ * @private
340
+ * @template {ExtensionType} ExtType
341
+ * @param {string} appiumHome
342
+ * @param {ExtType} extType
343
+ * @param {ExtName<ExtType>} extName - Extension name (unique to its type)
344
+ * @param {ExtManifestWithSchema<ExtType>} extData - Extension config
345
+ * @returns {import('ajv').SchemaObject|undefined}
346
+ */
347
+ static _readExtensionSchema (appiumHome, extType, extName, extData) {
348
+ const {pkgName, schema: argSchemaPath} = extData;
349
+ if (!argSchemaPath) {
350
+ throw new TypeError(
351
+ `No \`schema\` property found in config for ${extType} ${pkgName} -- why is this function being called?`,
352
+ );
353
+ }
354
+ let moduleObject;
355
+ if (_.isString(argSchemaPath)) {
356
+ const schemaPath = resolveFrom(appiumHome, path.join(pkgName, argSchemaPath));
357
+ moduleObject = require(schemaPath);
358
+ } else {
359
+ moduleObject = argSchemaPath;
360
+ }
361
+ // this sucks. default exports should be destroyed
362
+ const schema = moduleObject.__esModule
363
+ ? moduleObject.default
364
+ : moduleObject;
365
+ registerSchema(extType, extName, schema);
366
+ return schema;
367
+ }
368
+
369
+ /**
370
+ * Returns `true` if a specific {@link ExtManifest} object has a `schema` prop.
371
+ * The {@link ExtManifest} object becomes a {@link ExtManifestWithSchema} object.
372
+ * @template {ExtensionType} ExtType
373
+ * @param {ExtManifest<ExtType>} extData
374
+ * @returns {extData is ExtManifestWithSchema<ExtType>}
375
+ */
376
+ static extDataHasSchema (extData) {
377
+ return _.isString(extData?.schema) || _.isObject(extData?.schema);
378
+ }
379
+
380
+ /**
381
+ * If an extension provides a schema, this will load the schema and attempt to
382
+ * register it with the schema registrar.
383
+ * @param {ExtName<ExtType>} extName - Name of extension
384
+ * @param {ExtManifestWithSchema<ExtType>} extData - Extension data
385
+ * @returns {import('ajv').SchemaObject|undefined}
386
+ */
387
+ readExtensionSchema (extName, extData) {
388
+ return ExtensionConfig._readExtensionSchema(
389
+ this.appiumHome,
390
+ this.extensionType,
391
+ extName,
392
+ extData,
393
+ );
394
+ }
395
+ }
396
+
397
+ export {
398
+ INSTALL_TYPE_NPM,
399
+ INSTALL_TYPE_GIT,
400
+ INSTALL_TYPE_LOCAL,
401
+ INSTALL_TYPE_GITHUB,
402
+ INSTALL_TYPES,
403
+ };
404
+
405
+ /**
406
+ * Config problem
407
+ * @typedef Problem
408
+ * @property {string} err - Error message
409
+ * @property {any} val - Associated value
410
+ */
411
+
412
+ /**
413
+ * An optional logging function provided to an {@link ExtensionConfig} subclass.
414
+ * @callback ExtensionLogFn
415
+ * @param {...any} args
416
+ * @returns {void}
417
+ */
418
+
419
+ /**
420
+ * @typedef {import('../../types').ExtensionType} ExtensionType
421
+ * @typedef {import('./manifest').Manifest} Manifest
422
+ */
423
+
424
+ /**
425
+ * @template T
426
+ * @typedef {import('../../types/appium-manifest').ExtManifest<T>} ExtManifest
427
+ */
428
+
429
+ /**
430
+ * @template T
431
+ * @typedef {import('../../types/appium-manifest').ExtManifestWithSchema<T>} ExtManifestWithSchema
432
+ */
433
+
434
+ /**
435
+ * @template T
436
+ * @typedef {import('../../types/appium-manifest').ExtName<T>} ExtName
437
+ */
438
+
439
+ /**
440
+ * @template T
441
+ * @typedef {import('../../types/extension').ExtClass<T>} ExtClass
442
+ */
443
+
444
+ /**
445
+ * @template T
446
+ * @typedef {import('../../types/appium-manifest').ExtRecord<T>} ExtRecord
447
+ */
448
+
449
+ /**
450
+ * Options for various methods in {@link ExtensionConfig}
451
+ * @typedef ExtensionConfigMutationOpts
452
+ * @property {boolean} [write=true] Whether or not to write the manifest to disk after a mutation operation
453
+ */
454
+
455
+ /**
456
+ * A valid install type
457
+ * @typedef {typeof INSTALL_TYPE_NPM | typeof INSTALL_TYPE_GIT | typeof INSTALL_TYPE_LOCAL | typeof INSTALL_TYPE_GITHUB} InstallType
458
+ */