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