appium 3.2.2 → 3.3.0

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 (120) hide show
  1. package/build/lib/cli/args.d.ts +16 -12
  2. package/build/lib/cli/args.d.ts.map +1 -1
  3. package/build/lib/cli/args.js +15 -35
  4. package/build/lib/cli/args.js.map +1 -1
  5. package/build/lib/cli/driver-command.d.ts +51 -93
  6. package/build/lib/cli/driver-command.d.ts.map +1 -1
  7. package/build/lib/cli/driver-command.js +11 -66
  8. package/build/lib/cli/driver-command.js.map +1 -1
  9. package/build/lib/cli/extension-command.d.ts +211 -415
  10. package/build/lib/cli/extension-command.d.ts.map +1 -1
  11. package/build/lib/cli/extension-command.js +384 -653
  12. package/build/lib/cli/extension-command.js.map +1 -1
  13. package/build/lib/cli/extension.d.ts +11 -16
  14. package/build/lib/cli/extension.d.ts.map +1 -1
  15. package/build/lib/cli/extension.js +10 -28
  16. package/build/lib/cli/extension.js.map +1 -1
  17. package/build/lib/cli/parser.d.ts +40 -69
  18. package/build/lib/cli/parser.d.ts.map +1 -1
  19. package/build/lib/cli/parser.js +24 -59
  20. package/build/lib/cli/parser.js.map +1 -1
  21. package/build/lib/cli/plugin-command.d.ts +50 -90
  22. package/build/lib/cli/plugin-command.d.ts.map +1 -1
  23. package/build/lib/cli/plugin-command.js +11 -63
  24. package/build/lib/cli/plugin-command.js.map +1 -1
  25. package/build/lib/cli/setup-command.d.ts +21 -26
  26. package/build/lib/cli/setup-command.d.ts.map +1 -1
  27. package/build/lib/cli/setup-command.js +13 -55
  28. package/build/lib/cli/setup-command.js.map +1 -1
  29. package/build/lib/cli/utils.d.ts +27 -29
  30. package/build/lib/cli/utils.d.ts.map +1 -1
  31. package/build/lib/cli/utils.js +29 -31
  32. package/build/lib/cli/utils.js.map +1 -1
  33. package/build/lib/config-file.d.ts +24 -67
  34. package/build/lib/config-file.d.ts.map +1 -1
  35. package/build/lib/config-file.js +56 -115
  36. package/build/lib/config-file.js.map +1 -1
  37. package/build/lib/config.d.ts +42 -44
  38. package/build/lib/config.d.ts.map +1 -1
  39. package/build/lib/config.js +75 -107
  40. package/build/lib/config.js.map +1 -1
  41. package/build/lib/constants.d.ts +23 -23
  42. package/build/lib/constants.d.ts.map +1 -1
  43. package/build/lib/constants.js +10 -15
  44. package/build/lib/constants.js.map +1 -1
  45. package/build/lib/doctor/doctor.d.ts +40 -57
  46. package/build/lib/doctor/doctor.d.ts.map +1 -1
  47. package/build/lib/doctor/doctor.js +29 -60
  48. package/build/lib/doctor/doctor.js.map +1 -1
  49. package/build/lib/grid-register.d.ts +32 -7
  50. package/build/lib/grid-register.d.ts.map +1 -1
  51. package/build/lib/grid-register.js +84 -48
  52. package/build/lib/grid-register.js.map +1 -1
  53. package/build/lib/logsink.d.ts +13 -22
  54. package/build/lib/logsink.d.ts.map +1 -1
  55. package/build/lib/logsink.js +48 -103
  56. package/build/lib/logsink.js.map +1 -1
  57. package/build/lib/main.js +1 -1
  58. package/build/lib/main.js.map +1 -1
  59. package/build/lib/schema/arg-spec.d.ts +32 -107
  60. package/build/lib/schema/arg-spec.d.ts.map +1 -1
  61. package/build/lib/schema/arg-spec.js +11 -107
  62. package/build/lib/schema/arg-spec.js.map +1 -1
  63. package/build/lib/schema/cli-args.d.ts +3 -15
  64. package/build/lib/schema/cli-args.d.ts.map +1 -1
  65. package/build/lib/schema/cli-args.js +15 -105
  66. package/build/lib/schema/cli-args.js.map +1 -1
  67. package/build/lib/schema/cli-transformers.d.ts +15 -12
  68. package/build/lib/schema/cli-transformers.d.ts.map +1 -1
  69. package/build/lib/schema/cli-transformers.js +15 -45
  70. package/build/lib/schema/cli-transformers.js.map +1 -1
  71. package/build/lib/schema/index.d.ts +2 -2
  72. package/build/lib/schema/index.d.ts.map +1 -1
  73. package/build/lib/schema/index.js.map +1 -1
  74. package/build/lib/schema/keywords.d.ts +12 -20
  75. package/build/lib/schema/keywords.d.ts.map +1 -1
  76. package/build/lib/schema/keywords.js +6 -51
  77. package/build/lib/schema/keywords.js.map +1 -1
  78. package/build/lib/schema/schema.d.ts +106 -231
  79. package/build/lib/schema/schema.d.ts.map +1 -1
  80. package/build/lib/schema/schema.js +75 -345
  81. package/build/lib/schema/schema.js.map +1 -1
  82. package/build/lib/utils.d.ts +59 -238
  83. package/build/lib/utils.d.ts.map +1 -1
  84. package/build/lib/utils.js +55 -207
  85. package/build/lib/utils.js.map +1 -1
  86. package/lib/cli/{args.js → args.ts} +40 -51
  87. package/lib/cli/driver-command.ts +122 -0
  88. package/lib/cli/{extension-command.js → extension-command.ts} +610 -689
  89. package/lib/cli/extension.ts +65 -0
  90. package/lib/cli/{parser.js → parser.ts} +48 -71
  91. package/lib/cli/plugin-command.ts +117 -0
  92. package/lib/cli/{setup-command.js → setup-command.ts} +57 -72
  93. package/lib/cli/utils.ts +97 -0
  94. package/lib/config-file.ts +212 -0
  95. package/lib/{config.js → config.ts} +129 -141
  96. package/lib/{constants.js → constants.ts} +30 -41
  97. package/lib/doctor/{doctor.js → doctor.ts} +81 -91
  98. package/lib/grid-register.ts +250 -0
  99. package/lib/{logsink.js → logsink.ts} +91 -137
  100. package/lib/main.js +1 -1
  101. package/lib/schema/arg-spec.ts +131 -0
  102. package/lib/schema/cli-args.ts +171 -0
  103. package/lib/schema/cli-transformers.ts +83 -0
  104. package/lib/schema/keywords.ts +96 -0
  105. package/lib/schema/schema.ts +449 -0
  106. package/lib/utils.ts +404 -0
  107. package/package.json +16 -16
  108. package/lib/cli/driver-command.js +0 -174
  109. package/lib/cli/extension.js +0 -74
  110. package/lib/cli/plugin-command.js +0 -164
  111. package/lib/cli/utils.js +0 -91
  112. package/lib/config-file.js +0 -228
  113. package/lib/grid-register.js +0 -146
  114. package/lib/schema/arg-spec.js +0 -229
  115. package/lib/schema/cli-args.js +0 -254
  116. package/lib/schema/cli-transformers.js +0 -113
  117. package/lib/schema/keywords.js +0 -136
  118. package/lib/schema/schema.js +0 -725
  119. package/lib/utils.js +0 -512
  120. /package/lib/schema/{index.js → index.ts} +0 -0
@@ -1,229 +0,0 @@
1
- import _ from 'lodash';
2
-
3
- /**
4
- * The original ID of the Appium config schema.
5
- * We use this in the CLI to convert it to `argparse` options.
6
- */
7
- export const APPIUM_CONFIG_SCHEMA_ID = 'appium.json';
8
-
9
- /**
10
- * The schema prop containing server-related options. Everything in here
11
- * is "native" to Appium.
12
- * Used by {@link flattenSchema} for transforming the schema into CLI args.
13
- */
14
- export const SERVER_PROP_NAME = 'server';
15
-
16
- /**
17
- * Used to parse extension info from a schema ID.
18
- */
19
- const SCHEMA_ID_REGEXP = /^(?<extType>.+?)-(?<normalizedExtName>.+)\.json$/;
20
-
21
- /**
22
- * Avoid typos by using constants!
23
- */
24
- const PROPERTIES = 'properties';
25
-
26
- /**
27
- * An `ArgSpec` is a class representing metadata about an argument (or config
28
- * option) used for cross-referencing.
29
- *
30
- * This class has no instance methods, and is basically just a read-only "struct".
31
- * @template D
32
- */
33
- export class ArgSpec {
34
- /**
35
- * The canonical name of the argument. Corresponds to key in schema's `properties` prop.
36
- * @type {string}
37
- */
38
- name;
39
-
40
- /**
41
- * The `ExtensionType` of the argument. This will be set if the arg came from an extension;
42
- * otherwise it will be `undefined`.
43
- * @type {ExtensionType|undefined}
44
- */
45
- extType;
46
-
47
- /**
48
- * The name of the extension, if this argument came from an extension.
49
- *
50
- * Otherwise `undefined`.
51
- * @type {string|undefined}
52
- */
53
- extName;
54
-
55
- /**
56
- * The schema ID (`$id`) for the argument. This is automatically determined, and any user-provided `$id`s will be overwritten.
57
- *
58
- * @type {string}
59
- */
60
- ref;
61
-
62
- /**
63
- * The CLI argument, sans leading dashes.
64
- * @type {string}
65
- */
66
- arg;
67
-
68
- /**
69
- * The desired keypath for the argument after arguments have been parsed.
70
- *
71
- * Typically this is camelCased. If the arg came from an extension, it will be prefixed with
72
- * `<extType>.<extName>.`
73
- * @type {string}
74
- */
75
- dest;
76
-
77
- /**
78
- * The same as {@link ArgSpec.dest} but without the leading `<extType>.<extName>.` prefix.
79
- */
80
- rawDest;
81
-
82
- /**
83
- * Whatever the default value of this argument is, as specified by the
84
- * `default` property of the schema.
85
- * @type {D|undefined}
86
- */
87
- defaultValue;
88
-
89
- /**
90
- * Builds some computed fields and assigns them to the instance.
91
- *
92
- * Undefined properties are not assigned.
93
- *
94
- * The _constructor_ is private. Use {@link ArgSpec.create} instead.
95
- * @private
96
- * @param {string} name
97
- * @param {ArgSpecOptions<D>} opts
98
- */
99
- constructor(name, {extType, extName, dest, defaultValue} = {}) {
100
- // we must normalize the extension name to fit into our convention for CLI
101
- // args.
102
- const arg = ArgSpec.toArg(name, extType, extName);
103
-
104
- const ref = ArgSpec.toSchemaRef(name, extType, extName);
105
-
106
- // if no explicit `dest` provided, just camelCase the name to avoid needing
107
- // to use bracket syntax when accessing props on the parsed args object.
108
- const rawDest = _.camelCase(dest ?? name);
109
-
110
- const destKeypath = extType && extName ? [extType, extName, rawDest].join('.') : rawDest;
111
-
112
- this.defaultValue = defaultValue;
113
- this.name = name;
114
- this.extType = extType;
115
- this.extName = extName;
116
- this.arg = arg;
117
- this.dest = destKeypath;
118
- this.ref = ref;
119
- this.rawDest = rawDest;
120
- }
121
-
122
- /**
123
- * Return the schema ID (`$id`) for the **argument** given the parameters.
124
- *
125
- * If you need the "root" or "base" schema ID, use {@link ArgSpec.toSchemaBaseRef} instead.
126
- * @param {string} name - Argument name
127
- * @param {ExtensionType} [extType] - Extension type
128
- * @param {string} [extName] - Extension name
129
- * @returns {string} Schema ID
130
- */
131
- static toSchemaRef(name, extType, extName) {
132
- const baseRef = ArgSpec.toSchemaBaseRef(extType, extName);
133
- if (extType && extName) {
134
- return [`${baseRef}#`, PROPERTIES, name].join('/');
135
- }
136
- return [`${baseRef}#`, PROPERTIES, SERVER_PROP_NAME, PROPERTIES, name].join('/');
137
- }
138
-
139
- /**
140
- * Return the schema ID for an extension or the base schema ID.
141
- * @param {ExtensionType} [extType] - Extension type
142
- * @param {string} [extName] - Extension name
143
- */
144
- static toSchemaBaseRef(extType, extName) {
145
- if (extType && extName) {
146
- return `${extType}-${ArgSpec.toNormalizedExtName(extName)}.json`;
147
- }
148
- return APPIUM_CONFIG_SCHEMA_ID;
149
- }
150
-
151
- /**
152
- * Return the unique ID for the argument given the parameters.
153
- * @param {string} name - Argument name
154
- * @param {ExtensionType} [extType] - Extension type
155
- * @param {string} [extName] - Extension name
156
- * @returns {string} Unique ID
157
- */
158
- static toArg(name, extType, extName) {
159
- const properName = _.kebabCase(name.replace(/^--?/, ''));
160
- if (extType && extName) {
161
- return [extType, _.kebabCase(extName), properName].join('-');
162
- }
163
- return properName;
164
- }
165
-
166
- /**
167
- * Normalizes a raw extension name (not including the type).
168
- * @param {string} extName - Extension name
169
- * @returns {string} Normalized extension name
170
- */
171
- static toNormalizedExtName(extName) {
172
- return _.kebabCase(extName);
173
- }
174
-
175
- /**
176
- * When given the root ID of a schema for an extension (`<extType>-<normalizedExtName>.json`) Returns an object containing the extension type and the _normalized_ extension name.
177
- * @param {string} schemaId - Root schema ID
178
- * @returns { {extType?: ExtensionType, normalizedExtName?: string} }
179
- */
180
- static extensionInfoFromRootSchemaId(schemaId) {
181
- const matches = schemaId.match(SCHEMA_ID_REGEXP);
182
- if (matches?.groups) {
183
- const {extType, normalizedExtName} =
184
- /** @type { {extType: ExtensionType, normalizedExtName: string} } */ (matches.groups);
185
- return {extType, normalizedExtName};
186
- }
187
- return {};
188
- }
189
-
190
- /**
191
- * Creates an `ArgSpec`
192
- *
193
- * @param {string} name - The canonical name of the argument. Corresponds to a key in a schema's
194
- * `properties` property.
195
- * @template D
196
- * @param {ArgSpecOptions<D>} [opts] - Options
197
- * @returns {Readonly<ArgSpec>}
198
- */
199
- static create(name, opts) {
200
- return Object.freeze(new ArgSpec(name, opts));
201
- }
202
-
203
- /**
204
- * String representation, useful for debugging
205
- * @returns {string}
206
- */
207
- /* istanbul ignore next */
208
- toString() {
209
- let str = `[ArgSpec] ${this.name} (${this.ref})`;
210
- if (this.extType && this.extName) {
211
- str += ` (ext: ${this.extType}/${this.extName})`;
212
- }
213
- return str;
214
- }
215
- }
216
-
217
- /**
218
- * Options for {@link ArgSpec.create}
219
- * @template D
220
- * @typedef ArgSpecOptions
221
- * @property {string} [extName]
222
- * @property {ExtensionType} [extType]
223
- * @property {string} [dest]
224
- * @property {D} [defaultValue]
225
- */
226
-
227
- /**
228
- * @typedef {import('@appium/types').ExtensionType} ExtensionType
229
- */
@@ -1,254 +0,0 @@
1
- import {ArgumentTypeError} from 'argparse';
2
- import _ from 'lodash';
3
- import {formatErrors} from '../config-file';
4
- import {flattenSchema, validate} from './schema';
5
- import {transformers, parseCsvLine} from './cli-transformers';
6
-
7
- /**
8
- * This module concerns functions which convert schema definitions to
9
- * `argparse`-compatible data structures, for deriving CLI arguments from a
10
- * schema.
11
- */
12
-
13
- /**
14
- * Lookup of possible values for the `type` field in a JSON schema.
15
- * @type {Readonly<Record<string, import('json-schema').JSONSchema7TypeName>>}
16
- */
17
- const TYPENAMES = Object.freeze({
18
- ARRAY: 'array',
19
- OBJECT: 'object',
20
- BOOLEAN: 'boolean',
21
- INTEGER: 'integer',
22
- NUMBER: 'number',
23
- NULL: 'null',
24
- STRING: 'string',
25
- });
26
-
27
- /**
28
- * Options with alias lengths less than this will be considered "short" flags.
29
- */
30
- const SHORT_ARG_CUTOFF = 3;
31
-
32
- /**
33
- * Convert an alias (`foo`) to a flag (`--foo`) or a short flag (`-f`).
34
- * @param {ArgSpec} argSpec - the argument specification
35
- * @param {string} [alias] - the alias to convert to a flag
36
- * @returns {string} the flag
37
- */
38
- function aliasToFlag(argSpec, alias) {
39
- const {extType, extName, name} = argSpec;
40
- const arg = alias ?? name;
41
- const isShort = arg.length < SHORT_ARG_CUTOFF;
42
- if (extType && extName) {
43
- return isShort
44
- ? `--${extType}-${_.kebabCase(extName)}-${arg}`
45
- : `--${extType}-${_.kebabCase(extName)}-${_.kebabCase(arg)}`;
46
- }
47
- return isShort ? `-${arg}` : `--${_.kebabCase(arg)}`;
48
- }
49
-
50
- /**
51
- * Converts a string to SCREAMING_SNAKE_CASE
52
- */
53
- const screamingSnakeCase = _.flow(_.snakeCase, _.toUpper);
54
-
55
- /**
56
- * Given unique property name `name`, return a function which validates a value
57
- * against a property within the schema.
58
- * @template Coerced
59
- * @param {ArgSpec} argSpec - Argument name
60
- * @param {(value: string) => Coerced} [coerce] - Function to coerce to a different
61
- * primitive
62
- * @todo See if we can remove `coerce` by allowing Ajv to coerce in its
63
- * constructor options
64
- * @returns
65
- */
66
- function getSchemaValidator({ref: schemaId}, coerce = _.identity) {
67
- /** @param {string} value */
68
- return (value) => {
69
- const coerced = coerce(value);
70
- const errors = validate(coerced, schemaId);
71
- if (_.isEmpty(errors)) {
72
- return coerced;
73
- }
74
- throw new ArgumentTypeError('\n\n' + formatErrors(errors, value, {schemaId}));
75
- };
76
- }
77
-
78
- /**
79
- * Determine the description for display on the CLI, given the schema.
80
- * @param {AppiumJSONSchema} schema
81
- * @returns {string}
82
- */
83
- function makeDescription(schema) {
84
- const {appiumCliDescription, description = '', appiumDeprecated} = schema;
85
- let desc = appiumCliDescription ?? description;
86
- if (appiumDeprecated) {
87
- desc = `[DEPRECATED] ${desc}`;
88
- }
89
- return desc;
90
- }
91
-
92
- /**
93
- * Given arg `name`, a JSON schema `subSchema`, and options, return an argument definition
94
- * as understood by `argparse`.
95
- * @param {AppiumJSONSchema} subSchema - JSON schema for the option
96
- * @param {ArgSpec} argSpec - Argument spec tuple
97
- * @returns {[[string]|[string, string], import('argparse').ArgumentOptions]} Tuple of flag and options
98
- */
99
- function subSchemaToArgDef(subSchema, argSpec) {
100
- let {type, appiumCliAliases, appiumCliTransformer, enum: enumValues} = subSchema;
101
-
102
- const {name, arg} = argSpec;
103
-
104
- const aliases = [
105
- aliasToFlag(argSpec),
106
- .../** @type {string[]} */ (appiumCliAliases ?? []).map((alias) => aliasToFlag(argSpec, alias)),
107
- ];
108
-
109
- /** @type {import('argparse').ArgumentOptions} */
110
- let argOpts = {
111
- required: false,
112
- help: makeDescription(subSchema),
113
- };
114
-
115
- // argparse infers dest from the option string (e.g. --log-level → log_level).
116
- // We need schema dest (e.g. loglevel) so merged server args and logsink use the right key.
117
- if (!argSpec.extType) {
118
- argOpts.dest = argSpec.rawDest;
119
- }
120
-
121
- /**
122
- * Generally we will provide a `type` to `argparse` as a function which
123
- * validates using ajv (which is much more full-featured than what `argparse`
124
- * can offer). The exception is `boolean`-type options, which have no
125
- * `argType`.
126
- *
127
- * Not sure if this type is correct, but it's not doing what I want. I want
128
- * to say "this is a function which returns something of type `T` where `T` is
129
- * never a `Promise`". This function must be sync.
130
- * @type {((value: string) => unknown)|undefined}
131
- */
132
- let argTypeFunction;
133
-
134
- // handle special cases for various types
135
- switch (type) {
136
- // booleans do not have a type per `ArgumentOptions`, just an "action"
137
- // NOTE: due to limitations of `argparse`, we cannot provide fancy help text, and must rely on its internal error messaging.
138
- case TYPENAMES.BOOLEAN: {
139
- argOpts.action = 'store_const';
140
- argOpts.const = true;
141
- break;
142
- }
143
-
144
- case TYPENAMES.OBJECT: {
145
- argTypeFunction = _.flow(transformers.json, (o) => {
146
- // Arrays and plain strings are also valid JSON
147
- if (!_.isPlainObject(o)) {
148
- throw new ArgumentTypeError(`'${_.truncate(o, {length: 100})}' must be a plain object`);;
149
- }
150
- return o;
151
- });
152
- break;
153
- }
154
-
155
- // arrays are treated as CSVs, because `argparse` doesn't handle array data.
156
- case TYPENAMES.ARRAY: {
157
- argTypeFunction = parseCsvLine;
158
- break;
159
- }
160
-
161
- // "number" type is coerced to float. `argparse` does this for us if we use `float` type, but
162
- // we don't.
163
- case TYPENAMES.NUMBER: {
164
- argTypeFunction = getSchemaValidator(argSpec, parseFloat);
165
- break;
166
- }
167
-
168
- // "integer" is coerced to an .. integer. again, `argparse` would do this for us if we used `int`.
169
- case TYPENAMES.INTEGER: {
170
- argTypeFunction = getSchemaValidator(argSpec, _.parseInt);
171
- break;
172
- }
173
-
174
- // strings (like number and integer) are subject to further validation
175
- // (e.g., must satisfy a mask or regex or even some custom validation
176
- // function)
177
- case TYPENAMES.STRING: {
178
- argTypeFunction = getSchemaValidator(argSpec);
179
- break;
180
- }
181
-
182
- // TODO: there may be some way to restrict this at the Ajv level --
183
- // that may involve patching the metaschema.
184
- case TYPENAMES.NULL:
185
- // falls through
186
- default: {
187
- throw new TypeError(`Schema property "${arg}": \`${type}\` type unknown or disallowed`);
188
- }
189
- }
190
-
191
- // metavar is used in help text. `boolean` cannot have a metavar--it is not
192
- // displayed--and `argparse` throws if you give it one.
193
- if (type !== TYPENAMES.BOOLEAN) {
194
- argOpts.metavar = screamingSnakeCase(name);
195
- }
196
-
197
- if (appiumCliTransformer && transformers[appiumCliTransformer]) {
198
- if (type === TYPENAMES.ARRAY) {
199
- const csvTransformer = /** @type {(x: string) => string[]} */ (argTypeFunction);
200
- argTypeFunction = (val) => _.flatMap(csvTransformer(val).map(transformers[appiumCliTransformer]));
201
- } else {
202
- argTypeFunction = _.flow(argTypeFunction ?? _.identity, transformers[appiumCliTransformer]);
203
- }
204
- }
205
-
206
- if (argTypeFunction) {
207
- argOpts.type = argTypeFunction;
208
- }
209
-
210
- // convert JSON schema `enum` to `choices`. `enum` can contain any JSON type, but `argparse`
211
- // is limited to a single type per arg (I think). so let's make everything a string.
212
- // and might as well _require_ the `type: string` while we're at it.
213
- if (enumValues && !_.isEmpty(enumValues)) {
214
- if (type === TYPENAMES.STRING) {
215
- argOpts.choices = enumValues.map(String);
216
- } else {
217
- throw new TypeError(
218
- `Problem with schema for ${arg}; \`enum\` is only supported for \`type: 'string'\``
219
- );
220
- }
221
- }
222
-
223
- // TODO: argparse only accepts the command name and a single alias; any extra aliases
224
- // will be silently discarded.
225
- const finalAliases = /** @type {[string]|[string, string]} */ (aliases);
226
- return [finalAliases, argOpts];
227
- }
228
-
229
- /**
230
- * Converts the finalized, flattened schema representation into
231
- * ArgumentDefinitions for handoff to `argparse`.
232
- *
233
- * @throws If schema has not been added to ajv (via `finalizeSchema()`)
234
- * @returns {import('../cli/args').ArgumentDefinitions} A map of arrays of
235
- * aliases to `argparse` arguments; empty if no schema found
236
- */
237
- export function toParserArgs() {
238
- const flattened = flattenSchema().filter(({schema}) => !schema.appiumCliIgnored);
239
- return new Map(_.map(flattened, ({schema, argSpec}) => subSchemaToArgDef(schema, argSpec)));
240
- }
241
-
242
- /**
243
- * @template {string|number} T
244
- * @typedef {import('ajv/dist/types').FormatValidator<T>} FormatValidator<T>
245
- */
246
-
247
- /**
248
- * A JSON 7 schema with our custom keywords.
249
- * @typedef {import('./keywords').AppiumJSONSchemaKeywords & import('json-schema').JSONSchema7} AppiumJSONSchema
250
- */
251
-
252
- /**
253
- * @typedef {import('./arg-spec').ArgSpec} ArgSpec
254
- */
@@ -1,113 +0,0 @@
1
- import {ArgumentTypeError} from 'argparse';
2
- import {readFileSync, existsSync} from 'node:fs';
3
-
4
- /**
5
- * This module provides custom keywords for Appium schemas, as well as
6
- * "transformers" (see `argTransformers` below).
7
- *
8
- * Custom keywords are just properties that will appear in a schema (e.g.,
9
- * `appium-config-schema.js`) beyond what the JSON Schema spec offers. These
10
- * are usable by extensions, as well.
11
- */
12
-
13
- /**
14
- * Splits a CSV string into an array
15
- * @param {string} value
16
- * @returns {string[]}
17
- */
18
- export function parseCsvLine(value) {
19
- return value
20
- .split(',')
21
- .map((v) => v.trim())
22
- .filter(Boolean);
23
- }
24
-
25
- /**
26
- * Split a file by newline then calls {@link parseCsvLine} on each line.
27
- * @param {string} value
28
- * @returns {string[]}
29
- */
30
- function parseCsvFile(value) {
31
- return value
32
- .split(/\r?\n/)
33
- .map((v) => v.trim())
34
- .filter(Boolean)
35
- .flatMap(parseCsvLine);
36
- }
37
-
38
- /**
39
- * Namespace containing _transformers_ for CLI arguments. "Validators" and
40
- * "formatters" do not actually modify the value, but these do.
41
- *
42
- * Use case is for when the config file can accept e.g., a `string[]`, but the
43
- * CLI can only take a `string` (as `argparse` seems to be limited in that
44
- * fashion; it also cannot understand an argument having multiple types).
45
- *
46
- * For example, the `csv` transform takes a `string` and returns a `string[]` by
47
- * splitting it by comma--_or_ if that `string` happens to be a
48
- * filepath--reading the file as a `.csv`.
49
- *
50
- * This contains some copy-pasted code from `lib/cli/parser-helpers.js`, which was
51
- * obliterated.
52
- */
53
- export const transformers = {
54
- /**
55
- * Given a CSV-style string or pathname, parse it into an array.
56
- * The file can also be split on newlines.
57
- * @param {string} csvOrPath
58
- * @returns {string[]}
59
- */
60
- csv: (csvOrPath) => {
61
- let csv = csvOrPath;
62
- let loadedFromFile = false;
63
- // since this value could be a single string (no commas) _or_ a pathname, we will need
64
- // to attempt to parse it as a file _first_.
65
- if (existsSync(csvOrPath)) {
66
- try {
67
- csv = readFileSync(csvOrPath, 'utf8');
68
- } catch (err) {
69
- throw new ArgumentTypeError(`Could not read file '${csvOrPath}': ${err.message}`);
70
- }
71
- loadedFromFile = true;
72
- }
73
- try {
74
- return loadedFromFile ? parseCsvFile(csv) : parseCsvLine(csv);
75
- } catch (err) {
76
- const msg = loadedFromFile
77
- ? `The provided value of '${csvOrPath}' must be a valid CSV`
78
- : `Must be a comma-delimited string, e.g., "foo,bar,baz"`;
79
- throw new TypeError(`${msg}. Original error: ${err.message}`);
80
- }
81
- },
82
-
83
- /**
84
- * Parse a string which could be a path to a JSON file or a JSON string.
85
- * @param {string} jsonOrPath
86
- * @returns {object}
87
- */
88
- json: (jsonOrPath) => {
89
- let json = jsonOrPath;
90
- let loadedFromFile = false;
91
- if (existsSync(jsonOrPath)) {
92
- try {
93
- // use synchronous file access, as `argparse` provides no way of either
94
- // awaiting or using callbacks. This step happens in startup, in what is
95
- // effectively command-line code, so nothing is blocked in terms of
96
- // sessions, so holding up the event loop does not incur the usual
97
- // drawbacks.
98
- json = readFileSync(jsonOrPath, 'utf8');
99
- } catch (err) {
100
- throw new ArgumentTypeError(`Could not read file '${jsonOrPath}': ${err.message}`);
101
- }
102
- loadedFromFile = true;
103
- }
104
- try {
105
- return JSON.parse(json);
106
- } catch (e) {
107
- const msg = loadedFromFile
108
- ? `'${jsonOrPath}' must be a valid JSON`
109
- : `The provided value must be a valid JSON`;
110
- throw new TypeError(`${msg}. Original error: ${e.message}`);
111
- }
112
- },
113
- };