appium 2.0.0-beta.4 → 2.0.0-beta.42

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 (153) 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 +256 -131
  5. package/build/lib/cli/args.d.ts +17 -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 +37 -0
  9. package/build/lib/cli/driver-command.d.ts.map +1 -0
  10. package/build/lib/cli/driver-command.js +27 -18
  11. package/build/lib/cli/extension-command.d.ts +376 -0
  12. package/build/lib/cli/extension-command.d.ts.map +1 -0
  13. package/build/lib/cli/extension-command.js +287 -156
  14. package/build/lib/cli/extension.d.ts +22 -0
  15. package/build/lib/cli/extension.d.ts.map +1 -0
  16. package/build/lib/cli/extension.js +31 -17
  17. package/build/lib/cli/parser.d.ts +84 -0
  18. package/build/lib/cli/parser.d.ts.map +1 -0
  19. package/build/lib/cli/parser.js +164 -94
  20. package/build/lib/cli/plugin-command.d.ts +34 -0
  21. package/build/lib/cli/plugin-command.d.ts.map +1 -0
  22. package/build/lib/cli/plugin-command.js +26 -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 +49 -0
  30. package/build/lib/config.d.ts.map +1 -0
  31. package/build/lib/config.js +119 -96
  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 +75 -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 +66 -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 +183 -91
  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 +178 -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 +450 -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 +123 -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 +520 -186
  99. package/lib/cli/args.js +267 -422
  100. package/lib/cli/driver-command.js +58 -23
  101. package/lib/cli/extension-command.js +613 -260
  102. package/lib/cli/extension.js +47 -17
  103. package/lib/cli/parser.js +263 -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 +243 -110
  108. package/lib/constants.js +69 -0
  109. package/lib/extension/driver-config.js +249 -0
  110. package/lib/extension/extension-config.js +677 -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 +308 -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 +85 -85
  127. package/plugin.d.ts +1 -0
  128. package/plugin.js +13 -0
  129. package/scripts/autoinstall-extensions.js +185 -0
  130. package/support.d.ts +1 -0
  131. package/support.js +13 -0
  132. package/test.d.ts +7 -0
  133. package/test.js +13 -0
  134. package/types/appium-manifest.ts +73 -0
  135. package/types/cli.ts +150 -0
  136. package/types/extension-manifest.ts +64 -0
  137. package/types/index.ts +21 -0
  138. package/CHANGELOG.md +0 -3515
  139. package/bin/ios-webkit-debug-proxy-launcher.js +0 -71
  140. package/build/lib/cli/npm.js +0 -206
  141. package/build/lib/cli/parser-helpers.js +0 -82
  142. package/build/lib/driver-config.js +0 -77
  143. package/build/lib/drivers.js +0 -96
  144. package/build/lib/extension-config.js +0 -253
  145. package/build/lib/plugin-config.js +0 -59
  146. package/build/lib/plugins.js +0 -14
  147. package/lib/cli/npm.js +0 -183
  148. package/lib/cli/parser-helpers.js +0 -79
  149. package/lib/driver-config.js +0 -46
  150. package/lib/drivers.js +0 -81
  151. package/lib/extension-config.js +0 -209
  152. package/lib/plugin-config.js +0 -34
  153. package/lib/plugins.js +0 -10
@@ -1,43 +1,73 @@
1
1
  /* eslint-disable no-console */
2
-
3
2
  import DriverCommand from './driver-command';
4
3
  import PluginCommand from './plugin-command';
5
- import DriverConfig from '../driver-config';
6
- import PluginConfig from '../plugin-config';
7
- import { DRIVER_TYPE } from '../extension-config';
8
- import { errAndQuit, log, JSON_SPACES } from './utils';
4
+ import {DRIVER_TYPE, PLUGIN_TYPE} from '../constants';
5
+ import {errAndQuit, JSON_SPACES} from './utils';
6
+
7
+ export const commandClasses = Object.freeze(
8
+ /** @type {const} */ ({
9
+ [DRIVER_TYPE]: DriverCommand,
10
+ [PLUGIN_TYPE]: PluginCommand,
11
+ })
12
+ );
9
13
 
10
14
  /**
11
15
  * Run a subcommand of the 'appium driver' type. Each subcommand has its own set of arguments which
12
16
  * can be represented as a JS object.
13
17
  *
14
- * @param {Object} args - JS object where the key is the parameter name (as defined in
18
+ * @param {import('appium/types').Args<import('appium/types').WithExtSubcommand>} args - JS object where the key is the parameter name (as defined in
15
19
  * driver-parser.js)
20
+ * @template {ExtensionType} ExtType
21
+ * @param {import('../extension/extension-config').ExtensionConfig<ExtType>} config - Extension config object
16
22
  */
17
- async function runExtensionCommand (args, type) {
23
+ async function runExtensionCommand(args, config) {
18
24
  // TODO driver config file should be locked while any of these commands are
19
25
  // running to prevent weird situations
20
26
  let jsonResult = null;
21
- const {json, appiumHome} = args;
22
- const logFn = (msg) => log(json, msg);
23
- const ConfigClass = type === DRIVER_TYPE ? DriverConfig : PluginConfig;
24
- const CommandClass = type === DRIVER_TYPE ? DriverCommand : PluginCommand;
25
- const config = new ConfigClass(appiumHome, logFn);
27
+ const {extensionType: type} = config;
28
+ const extCmd = args[`${type}Command`];
29
+ if (!extCmd) {
30
+ throw new TypeError(`Cannot call ${type} command without a subcommand like 'install'`);
31
+ }
32
+ let {json, suppressOutput} = args;
33
+ json = Boolean(json);
34
+ if (suppressOutput) {
35
+ json = true;
36
+ }
37
+ const CommandClass = /** @type {ExtCommand<ExtType>} */ (commandClasses[type]);
26
38
  const cmd = new CommandClass({config, json});
27
39
  try {
28
- await config.read();
29
40
  jsonResult = await cmd.execute(args);
30
41
  } catch (err) {
42
+ // in the suppress output case, we are calling this function internally and should
43
+ // just throw instead of printing an error and ending the process
44
+ if (suppressOutput) {
45
+ throw err;
46
+ }
31
47
  errAndQuit(json, err);
32
48
  }
33
49
 
34
- if (json) {
50
+ if (json && !suppressOutput) {
35
51
  console.log(JSON.stringify(jsonResult, null, JSON_SPACES));
36
52
  }
37
53
 
38
54
  return jsonResult;
39
55
  }
40
56
 
41
- export {
42
- runExtensionCommand,
43
- };
57
+ export {runExtensionCommand};
58
+
59
+ /**
60
+ * @template {ExtensionType} ExtType
61
+ * @typedef {ExtType extends DriverType ? Class<DriverCommand> : ExtType extends PluginType ? Class<PluginCommand> : never} ExtCommand
62
+ */
63
+
64
+ /**
65
+ * @typedef {import('@appium/types').ExtensionType} ExtensionType
66
+ * @typedef {import('@appium/types').DriverType} DriverType
67
+ * @typedef {import('@appium/types').PluginType} PluginType
68
+ */
69
+
70
+ /**
71
+ * @template T
72
+ * @typedef {import('@appium/types').Class<T>} Class
73
+ */
package/lib/cli/parser.js CHANGED
@@ -1,98 +1,278 @@
1
- import path from 'path';
1
+ import {fs} from '@appium/support';
2
+ import {ArgumentParser} from 'argparse';
2
3
  import _ from 'lodash';
3
- import { ArgumentParser } from 'argparse';
4
- import { sharedArgs, serverArgs, extensionArgs } from './args';
5
- import { DRIVER_TYPE, PLUGIN_TYPE } from '../extension-config';
6
- import { rootDir } from '../utils';
7
-
8
- function getParser (debug = false) {
9
- const parser = new ArgumentParser({
10
- version: require(path.resolve(rootDir, 'package.json')).version,
11
- addHelp: true,
12
- description: 'A webdriver-compatible server for use with native and hybrid iOS and Android applications.',
13
- prog: process.argv[1] ? path.basename(process.argv[1]) : 'appium',
14
- debug
15
- });
16
- const subParsers = parser.addSubparsers({dest: 'subcommand', debug});
17
-
18
- // add the 'server' subcommand, and store the raw arguments on the parser
19
- // object as a way for other parts of the code to work with the arguments
20
- // conceptually rather than just through argparse
21
- const serverArgs = addServerToParser(sharedArgs, subParsers, debug);
22
- parser.rawArgs = serverArgs;
23
-
24
- // add the 'driver' and 'plugin' subcommands
25
- addExtensionsToParser(sharedArgs, subParsers, debug);
26
-
27
- // modify the parseArgs function to insert the 'server' subcommand if the
28
- // user hasn't specified a subcommand or the global help command
29
- parser._parseArgs = parser.parseArgs;
30
- parser.parseArgs = function (args, namespace) {
31
- if (_.isUndefined(args)) {
32
- args = [...process.argv.slice(2)];
33
- }
34
- if (!_.includes([DRIVER_TYPE, PLUGIN_TYPE, 'server', '-h'], args[0])) {
35
- args.splice(0, 0, 'server');
4
+ import path from 'path';
5
+ import {DRIVER_TYPE, PLUGIN_TYPE, SERVER_SUBCOMMAND} from '../constants';
6
+ import {finalizeSchema, getArgSpec, hasArgSpec} from '../schema';
7
+ import {rootDir} from '../config';
8
+ import {getExtensionArgs, getServerArgs} from './args';
9
+
10
+ export const EXTRA_ARGS = 'extraArgs';
11
+
12
+ /**
13
+ * If the parsed args do not contain any of these values, then we
14
+ * will automatially inject the `server` subcommand.
15
+ */
16
+ const NON_SERVER_ARGS = Object.freeze(
17
+ new Set([DRIVER_TYPE, PLUGIN_TYPE, SERVER_SUBCOMMAND, '-h', '--help', '-v', '--version'])
18
+ );
19
+
20
+ const version = fs.readPackageJsonFrom(rootDir).version;
21
+
22
+ /**
23
+ * A wrapper around `argparse`
24
+ *
25
+ * - Handles instantiation, configuration, and monkeypatching of an
26
+ * `ArgumentParser` instance for Appium server and its extensions
27
+ * - Handles error conditions, messages, and exit behavior
28
+ */
29
+ class ArgParser {
30
+ /**
31
+ * @param {boolean} [debug] - If true, throw instead of exit on error.
32
+ */
33
+ constructor(debug = false) {
34
+ const prog = process.argv[1] ? path.basename(process.argv[1]) : 'appium';
35
+ const parser = new ArgumentParser({
36
+ add_help: true,
37
+ description:
38
+ 'A webdriver-compatible server that facilitates automation of web, mobile, and other types of apps across various platforms.',
39
+ prog,
40
+ });
41
+
42
+ ArgParser._patchExit(parser);
43
+
44
+ /**
45
+ * Program name (typically `appium`)
46
+ * @type {string}
47
+ */
48
+ this.prog = prog;
49
+
50
+ /**
51
+ * If `true`, throw an error on parse failure instead of printing help
52
+ * @type {boolean}
53
+ */
54
+ this.debug = debug;
55
+
56
+ /**
57
+ * Wrapped `ArgumentParser` instance
58
+ * @type {ArgumentParser}
59
+ */
60
+ this.parser = parser;
61
+
62
+ parser.add_argument('-v', '--version', {
63
+ action: 'version',
64
+ version,
65
+ });
66
+
67
+ const subParsers = parser.add_subparsers({dest: 'subcommand'});
68
+
69
+ // add the 'server' subcommand, and store the raw arguments on the parser
70
+ // object as a way for other parts of the code to work with the arguments
71
+ // conceptually rather than just through argparse
72
+ const serverArgs = ArgParser._addServerToParser(subParsers);
73
+
74
+ this.rawArgs = serverArgs;
75
+
76
+ // add the 'driver' and 'plugin' subcommands
77
+ ArgParser._addExtensionCommandsToParser(subParsers);
78
+
79
+ // backwards compatibility / drop-in wrapper
80
+ /**
81
+ * @type {ArgParser['parseArgs']}
82
+ */
83
+ this.parse_args = this.parseArgs;
84
+ }
85
+
86
+ /**
87
+ * Parse arguments from the command line.
88
+ *
89
+ * If no subcommand is passed in, this method will inject the `server` subcommand.
90
+ *
91
+ * `ArgParser.prototype.parse_args` is an alias of this method.
92
+ * @template [T=import('appium/types').WithServerSubcommand]
93
+ * @param {string[]} [args] - Array of arguments, ostensibly from `process.argv`. Gathers args from `process.argv` if not provided.
94
+ * @returns {import('appium/types').Args<T>} - The parsed arguments
95
+ */
96
+ parseArgs(args = process.argv.slice(2)) {
97
+ if (!NON_SERVER_ARGS.has(args[0])) {
98
+ args.unshift(SERVER_SUBCOMMAND);
36
99
  }
37
- return this._parseArgs(args, namespace);
38
- }.bind(parser);
39
- return parser;
40
- }
41
100
 
42
- function addServerToParser (sharedArgs, subParsers, debug) {
43
- const serverParser = subParsers.addParser('server', {
44
- addHelp: true,
45
- help: 'Run an Appium server',
46
- debug
47
- });
101
+ try {
102
+ const parsed = this.parser.parse_known_args(args);
103
+ const [knownArgs, unknownArgs] = parsed;
104
+ if (
105
+ unknownArgs?.length &&
106
+ (knownArgs.driverCommand === 'run' || knownArgs.pluginCommand === 'run')
107
+ ) {
108
+ return ArgParser._transformParsedArgs(knownArgs, unknownArgs);
109
+ } else if (unknownArgs?.length) {
110
+ throw new Error(`[ERROR] Unrecognized arguments: ${unknownArgs.join(' ')}`);
111
+ }
112
+ return ArgParser._transformParsedArgs(knownArgs);
113
+ } catch (err) {
114
+ if (this.debug) {
115
+ throw err;
116
+ }
117
+ // this isn't tested via unit tests (we use `debug: true`) so may escape coverage.
48
118
 
49
- for (const [flags, opts] of [...sharedArgs, ...serverArgs]) {
50
- // addArgument mutates arguments so make copies
51
- serverParser.addArgument([...flags], {...opts});
119
+ /* istanbul ignore next */
120
+ {
121
+ // eslint-disable-next-line no-console
122
+ console.error(); // need an extra space since argparse prints usage.
123
+ // eslint-disable-next-line no-console
124
+ console.error(err.message);
125
+ process.exit(1);
126
+ }
127
+ }
52
128
  }
53
129
 
54
- return serverArgs;
55
- }
130
+ /**
131
+ * Given an object full of arguments as returned by `argparser.parse_args`,
132
+ * expand the ones for extensions into a nested object structure and rename
133
+ * keys to match the intended destination.
134
+ *
135
+ * E.g., `{'driver-foo-bar': baz}` becomes `{driver: {foo: {bar: 'baz'}}}`
136
+ * @param {object} args
137
+ * @param {string[]} [unknownArgs]
138
+ * @returns {object}
139
+ */
140
+ static _transformParsedArgs(args, unknownArgs = []) {
141
+ const result = _.reduce(
142
+ args,
143
+ (unpacked, value, key) => {
144
+ if (!_.isUndefined(value) && hasArgSpec(key)) {
145
+ const {dest} = /** @type {import('../schema/arg-spec').ArgSpec} */ (getArgSpec(key));
146
+ _.set(unpacked, dest, value);
147
+ } else {
148
+ // this could be anything that _isn't_ a server arg
149
+ unpacked[key] = value;
150
+ }
151
+ return unpacked;
152
+ },
153
+ {}
154
+ );
155
+ result[EXTRA_ARGS] = unknownArgs;
156
+ return result;
157
+ }
56
158
 
57
- function getDefaultServerArgs () {
58
- let defaults = {};
59
- for (let [, arg] of serverArgs) {
60
- defaults[arg.dest] = arg.defaultValue;
159
+ /**
160
+ * Patches the `exit()` method of the parser to throw an error, so we can handle it manually.
161
+ * @param {ArgumentParser} parser
162
+ */
163
+ static _patchExit(parser) {
164
+ parser.exit = (code, msg) => {
165
+ if (code) {
166
+ throw new Error(msg);
167
+ }
168
+ process.exit();
169
+ };
61
170
  }
62
- return defaults;
63
- }
64
171
 
65
- function addExtensionsToParser (sharedArgs, subParsers, debug) {
66
- for (const type of [DRIVER_TYPE, PLUGIN_TYPE]) {
67
- const extParser = subParsers.addParser(type, {
68
- addHelp: true,
69
- help: `Access the ${type} management CLI commands`,
70
- debug
71
- });
72
- const extSubParsers = extParser.addSubparsers({
73
- dest: `${type}Command`,
74
- debug
172
+ /**
173
+ *
174
+ * @param {import('argparse').SubParser} subParser
175
+ * @returns {import('./args').ArgumentDefinitions}
176
+ */
177
+ static _addServerToParser(subParser) {
178
+ const serverParser = subParser.add_parser('server', {
179
+ add_help: true,
180
+ help: 'Run an Appium server',
75
181
  });
76
- const parserSpecs = [
77
- {command: 'list', args: extensionArgs[type].list,
78
- help: `List available and installed ${type}s`},
79
- {command: 'install', args: extensionArgs[type].install,
80
- help: `Install a ${type}`},
81
- {command: 'uninstall', args: extensionArgs[type].uninstall,
82
- help: `Uninstall a ${type}`},
83
- {command: 'update', args: extensionArgs[type].update,
84
- help: `Update installed ${type}s to the latest version`},
85
- ];
86
-
87
- for (const {command, args, help} of parserSpecs) {
88
- const parser = extSubParsers.addParser(command, {help, debug});
89
- for (const [flags, opts] of [...sharedArgs, ...args]) {
90
- // addArgument mutates params so make sure to send in copies instead
91
- parser.addArgument([...flags], {...opts});
182
+
183
+ ArgParser._patchExit(serverParser);
184
+
185
+ const serverArgs = getServerArgs();
186
+ for (const [flagsOrNames, opts] of serverArgs) {
187
+ // TS doesn't like the spread operator here.
188
+ // @ts-ignore
189
+ serverParser.add_argument(...flagsOrNames, {...opts});
190
+ }
191
+
192
+ return serverArgs;
193
+ }
194
+
195
+ /**
196
+ * Adds extension sub-sub-commands to `driver`/`plugin` subcommands
197
+ * @param {import('argparse').SubParser} subParsers
198
+ */
199
+ static _addExtensionCommandsToParser(subParsers) {
200
+ for (const type of /** @type {[DriverType, PluginType]} */ ([DRIVER_TYPE, PLUGIN_TYPE])) {
201
+ const extParser = subParsers.add_parser(type, {
202
+ add_help: true,
203
+ help: `Access the ${type} management CLI commands`,
204
+ });
205
+
206
+ ArgParser._patchExit(extParser);
207
+
208
+ const extSubParsers = extParser.add_subparsers({
209
+ dest: `${type}Command`,
210
+ });
211
+ const extensionArgs = getExtensionArgs();
212
+ /**
213
+ * @type { {command: import('appium/types').CliExtensionSubcommand, args: import('./args').ArgumentDefinitions, help: string}[] }
214
+ */
215
+ const parserSpecs = [
216
+ {
217
+ command: 'list',
218
+ args: extensionArgs[type].list,
219
+ help: `List available and installed ${type}s`,
220
+ },
221
+ {
222
+ command: 'install',
223
+ args: extensionArgs[type].install,
224
+ help: `Install a ${type}`,
225
+ },
226
+ {
227
+ command: 'uninstall',
228
+ args: extensionArgs[type].uninstall,
229
+ help: `Uninstall a ${type}`,
230
+ },
231
+ {
232
+ command: 'update',
233
+ args: extensionArgs[type].update,
234
+ help: `Update installed ${type}s to the latest version`,
235
+ },
236
+ {
237
+ command: 'run',
238
+ args: extensionArgs[type].run,
239
+ help:
240
+ `Run a script (defined inside the ${type}'s package.json under the ` +
241
+ `“scripts” field inside the “appium” field) from an installed ${type}`,
242
+ },
243
+ ];
244
+
245
+ for (const {command, args, help} of parserSpecs) {
246
+ const parser = extSubParsers.add_parser(command, {help});
247
+
248
+ ArgParser._patchExit(parser);
249
+
250
+ for (const [flagsOrNames, opts] of args) {
251
+ // add_argument mutates params so make sure to send in copies instead
252
+ // @ts-ignore
253
+ parser.add_argument(...flagsOrNames, {...opts});
254
+ }
92
255
  }
93
256
  }
94
257
  }
95
258
  }
96
259
 
97
- export default getParser;
98
- export { getParser, getDefaultServerArgs };
260
+ /**
261
+ * Creates a {@link ArgParser} instance; finalizes the config schema.
262
+ *
263
+ * @constructs ArgParser
264
+ * @param {boolean} [debug] - If `true`, throw instead of exit upon parsing error
265
+ * @returns {ArgParser}
266
+ */
267
+ function getParser(debug) {
268
+ finalizeSchema();
269
+
270
+ return new ArgParser(debug);
271
+ }
272
+
273
+ export {getParser, ArgParser};
274
+
275
+ /**
276
+ * @typedef {import('@appium/types').DriverType} DriverType
277
+ * @typedef {import('@appium/types').PluginType} PluginType
278
+ */
@@ -1,43 +1,71 @@
1
1
  import _ from 'lodash';
2
2
  import ExtensionCommand from './extension-command';
3
- import { PLUGIN_TYPE } from '../extension-config';
4
- import { KNOWN_PLUGINS } from '../plugins';
3
+ import {KNOWN_PLUGINS} from '../constants';
5
4
 
6
5
  const REQ_PLUGIN_FIELDS = ['pluginName', 'mainClass'];
7
6
 
7
+ /**
8
+ * @extends {ExtensionCommand<PluginType>}
9
+ */
8
10
  export default class PluginCommand extends ExtensionCommand {
9
-
10
- constructor ({config, json}) {
11
- super({config, json, type: PLUGIN_TYPE});
11
+ /**
12
+ *
13
+ * @param {import('./extension-command').ExtensionCommandOptions<PluginType>} opts
14
+ */
15
+ constructor({config, json}) {
16
+ super({config, json});
12
17
  this.knownExtensions = KNOWN_PLUGINS;
13
18
  }
14
19
 
15
- async install ({plugin, installType, packageName}) {
16
- return await super.install({ext: plugin, installType, packageName});
20
+ async install({plugin, installType, packageName}) {
21
+ return await super._install({
22
+ installSpec: plugin,
23
+ installType,
24
+ packageName,
25
+ });
26
+ }
27
+
28
+ async uninstall({plugin}) {
29
+ return await super._uninstall({installSpec: plugin});
17
30
  }
18
31
 
19
- async uninstall ({plugin}) {
20
- return await super.uninstall({ext: plugin});
32
+ async update({plugin, unsafe}) {
33
+ return await super._update({installSpec: plugin, unsafe});
21
34
  }
22
35
 
23
- async update ({plugin, unsafe}) {
24
- return await super.update({ext: plugin, unsafe});
36
+ async run({plugin, scriptName, extraArgs}) {
37
+ return await super._run({installSpec: plugin, scriptName, extraArgs});
25
38
  }
26
39
 
27
- getPostInstallText ({extName, extData}) {
40
+ getPostInstallText({extName, extData}) {
28
41
  return `Plugin ${extName}@${extData.version} successfully installed`.green;
29
42
  }
30
43
 
31
- validateExtensionFields (appiumPkgData) {
32
- const missingFields = REQ_PLUGIN_FIELDS.reduce((acc, field) => (
33
- appiumPkgData[field] ? acc : [...acc, field]
34
- ), []);
44
+ /**
45
+ * Validates fields in `appium` field of `driverMetadata`
46
+ *
47
+ * For any `package.json` fields which a driver requires, validate the type of
48
+ * those fields on the `package.json` data, throwing an error if anything is
49
+ * amiss.
50
+ * @param {import('appium/types').ExtMetadata<PluginType>} pluginMetadata
51
+ * @param {string} installSpec
52
+ * @returns {void}
53
+ */
54
+ validateExtensionFields(pluginMetadata, installSpec) {
55
+ const missingFields = REQ_PLUGIN_FIELDS.reduce(
56
+ (acc, field) => (pluginMetadata[field] ? acc : [...acc, field]),
57
+ []
58
+ );
35
59
 
36
60
  if (!_.isEmpty(missingFields)) {
37
- throw new Error(`Installed plugin did not expose correct fields for compability ` +
38
- `with Appium. Missing fields: ${JSON.stringify(missingFields)}`);
61
+ throw new Error(
62
+ `Installed plugin "${installSpec}" did not expose correct fields for compability ` +
63
+ `with Appium. Missing fields: ${JSON.stringify(missingFields)}`
64
+ );
39
65
  }
40
-
41
66
  }
42
-
43
67
  }
68
+
69
+ /**
70
+ * @typedef {import('@appium/types').PluginType} PluginType
71
+ */
package/lib/cli/utils.js CHANGED
@@ -7,9 +7,9 @@ const JSON_SPACES = 4;
7
7
  /***
8
8
  * Log an error to the console and exit the process.
9
9
  * @param {boolean} json - whether we should log json or text
10
- * @param {string} msg - error message
10
+ * @param {any} msg - error message, object, Error instance, etc.
11
11
  */
12
- function errAndQuit (json, msg) {
12
+ function errAndQuit(json, msg) {
13
13
  if (json) {
14
14
  console.log(JSON.stringify({error: `${msg}`}, null, JSON_SPACES));
15
15
  } else {
@@ -26,7 +26,7 @@ function errAndQuit (json, msg) {
26
26
  * @param {boolean} json - whether we are in json mode (and should therefore not log)
27
27
  * @param {string} msg - string to log
28
28
  */
29
- function log (json, msg) {
29
+ function log(json, msg) {
30
30
  !json && console.log(msg);
31
31
  }
32
32
 
@@ -36,7 +36,7 @@ function log (json, msg) {
36
36
  * @param {string} msg - string to log
37
37
  * @param {function} fn - function to wrap with spinning
38
38
  */
39
- async function spinWith (json, msg, fn) {
39
+ async function spinWith(json, msg, fn) {
40
40
  if (json) {
41
41
  return await fn();
42
42
  }
@@ -52,9 +52,23 @@ async function spinWith (json, msg, fn) {
52
52
  }
53
53
  }
54
54
 
55
- export {
56
- errAndQuit,
57
- log,
58
- spinWith,
59
- JSON_SPACES
60
- };
55
+ class RingBuffer {
56
+ constructor(size = 50) {
57
+ this.size = size;
58
+ this.buffer = [];
59
+ }
60
+ getBuff() {
61
+ return this.buffer;
62
+ }
63
+ dequeue() {
64
+ this.buffer.shift();
65
+ }
66
+ enqueue(item) {
67
+ if (this.buffer.length >= this.size) {
68
+ this.dequeue();
69
+ }
70
+ this.buffer.push(item);
71
+ }
72
+ }
73
+
74
+ export {errAndQuit, log, spinWith, JSON_SPACES, RingBuffer};