@sortlist/cli 0.28.0 → 0.29.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.
- package/package.json +1 -1
- package/src/commands/dynamic.ts +114 -31
- package/src/graphql/generated/graphql.ts +1 -0
package/package.json
CHANGED
package/src/commands/dynamic.ts
CHANGED
|
@@ -7,13 +7,16 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { Command } from 'commander';
|
|
9
9
|
import {
|
|
10
|
+
isEnumType,
|
|
10
11
|
isInputObjectType,
|
|
11
12
|
isInterfaceType,
|
|
12
13
|
isListType,
|
|
13
14
|
isNonNullType,
|
|
14
15
|
isUnionType,
|
|
15
16
|
type GraphQLArgument,
|
|
17
|
+
type GraphQLEnumType,
|
|
16
18
|
type GraphQLField,
|
|
19
|
+
type GraphQLInputObjectType,
|
|
17
20
|
type GraphQLInputType,
|
|
18
21
|
type GraphQLNamedType,
|
|
19
22
|
type GraphQLObjectType,
|
|
@@ -60,9 +63,14 @@ function ucfirst(s: string): string {
|
|
|
60
63
|
* For mutations with a single `input` argument of an Input type,
|
|
61
64
|
* return the fields of that input type for flattening into CLI options.
|
|
62
65
|
*/
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
+
interface CliArg {
|
|
67
|
+
name: string;
|
|
68
|
+
type: GraphQLArgument['type'];
|
|
69
|
+
required: boolean;
|
|
70
|
+
description?: string | null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function getInputFields(field: GraphQLField<unknown, unknown>): CliArg[] | null {
|
|
66
74
|
if (field.args.length !== 1) return null;
|
|
67
75
|
const arg = field.args[0];
|
|
68
76
|
if (arg.name !== 'input') return null;
|
|
@@ -73,16 +81,97 @@ function getInputFields(
|
|
|
73
81
|
name,
|
|
74
82
|
type: f.type,
|
|
75
83
|
required: isNonNullType(f.type),
|
|
84
|
+
description: f.description,
|
|
76
85
|
}));
|
|
77
86
|
}
|
|
78
87
|
|
|
79
|
-
|
|
88
|
+
/**
|
|
89
|
+
* Render a GraphQL input type as its SDL signature, e.g. `String!`,
|
|
90
|
+
* `[OrganizationSortInput!]`, `OrganizationFilterInput`.
|
|
91
|
+
*/
|
|
92
|
+
function renderType(type: GraphQLInputType): string {
|
|
93
|
+
if (isNonNullType(type)) return `${renderType(type.ofType as GraphQLInputType)}!`;
|
|
94
|
+
if (isListType(type)) return `[${renderType(type.ofType as GraphQLInputType)}]`;
|
|
95
|
+
return (type as GraphQLNamedType).name;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* A "structured" arg is one that must be supplied as JSON on the CLI and parsed
|
|
100
|
+
* (rather than passed through as a raw string): lists and input objects. Plain
|
|
101
|
+
* leaf scalars and enums are passed as raw strings — including when wrapped in
|
|
102
|
+
* non-null (`String!` is still a bare string on the CLI). The non-null wrapper
|
|
103
|
+
* is transparent for this decision; a list at any level means JSON.
|
|
104
|
+
*/
|
|
105
|
+
function isStructuredInput(type: GraphQLInputType): boolean {
|
|
106
|
+
if (isNonNullType(type)) return isStructuredInput(type.ofType as GraphQLInputType);
|
|
107
|
+
if (isListType(type)) return true;
|
|
108
|
+
return isInputObjectType(type);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Coerce a raw CLI string into the value GraphQL expects for the arg's type.
|
|
113
|
+
* Leaf scalars are parsed by name; structured types (input objects, lists,
|
|
114
|
+
* enums) are parsed as JSON. Throws a readable error on malformed JSON so the
|
|
115
|
+
* action can surface it instead of silently sending a bad string.
|
|
116
|
+
*/
|
|
117
|
+
function coerceValue(raw: string, type: GraphQLInputType, flagName: string): unknown {
|
|
118
|
+
if (isStructuredInput(type)) {
|
|
119
|
+
try {
|
|
120
|
+
return JSON.parse(raw);
|
|
121
|
+
} catch (parseError) {
|
|
122
|
+
throw new Error(
|
|
123
|
+
`--${flagName} expects JSON for type ${renderType(type)}, but the value could not be parsed: ` +
|
|
124
|
+
`${(parseError as Error).message}`,
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const typeName = unwrap(type).name;
|
|
80
130
|
if (typeName === 'Int') return parseInt(raw, 10);
|
|
81
131
|
if (typeName === 'Float') return parseFloat(raw);
|
|
82
132
|
if (typeName === 'Boolean') return raw === 'true';
|
|
83
133
|
return raw;
|
|
84
134
|
}
|
|
85
135
|
|
|
136
|
+
/**
|
|
137
|
+
* One-line summary of an input-object or enum type's shape, used in option
|
|
138
|
+
* help so callers know what JSON to pass without a separate introspection
|
|
139
|
+
* round-trip. Expands a single level: `{ industry: StringFilterInput, country:
|
|
140
|
+
* String, ... }` for input objects, `one of: A | B | C` for enums.
|
|
141
|
+
*/
|
|
142
|
+
function describeStructuredType(named: GraphQLNamedType): string | null {
|
|
143
|
+
if (isInputObjectType(named)) {
|
|
144
|
+
const fields = Object.values((named as GraphQLInputObjectType).getFields());
|
|
145
|
+
const parts = fields.map((f) => `${f.name}: ${renderType(f.type)}`);
|
|
146
|
+
return `{ ${parts.join(', ')} }`;
|
|
147
|
+
}
|
|
148
|
+
if (isEnumType(named)) {
|
|
149
|
+
const values = (named as GraphQLEnumType).getValues().map((v) => v.name);
|
|
150
|
+
return `one of: ${values.join(' | ')}`;
|
|
151
|
+
}
|
|
152
|
+
return null;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Build the help description for a single CLI option from its GraphQL arg:
|
|
157
|
+
* the field description (if any), the rendered type with a `(JSON)` marker for
|
|
158
|
+
* structured inputs, and a one-level shape hint (input-object fields or enum
|
|
159
|
+
* values) so callers know what to pass without a separate introspection step.
|
|
160
|
+
*/
|
|
161
|
+
function describeArg(arg: { name: string; type: GraphQLInputType; description?: string | null }): string {
|
|
162
|
+
const parts: string[] = [];
|
|
163
|
+
if (arg.description) parts.push(arg.description.replace(/\s+/g, ' ').trim());
|
|
164
|
+
|
|
165
|
+
const typeSig = renderType(arg.type);
|
|
166
|
+
parts.push(isStructuredInput(arg.type) ? `${typeSig} (JSON)` : typeSig);
|
|
167
|
+
|
|
168
|
+
// Shape hint applies to enums (raw string) and input objects (JSON) alike.
|
|
169
|
+
const shape = describeStructuredType(unwrap(arg.type));
|
|
170
|
+
if (shape) parts.push(shape);
|
|
171
|
+
|
|
172
|
+
return parts.join(' — ');
|
|
173
|
+
}
|
|
174
|
+
|
|
86
175
|
function buildCommand(
|
|
87
176
|
schema: GraphQLSchema,
|
|
88
177
|
fieldName: string,
|
|
@@ -97,12 +186,13 @@ function buildCommand(
|
|
|
97
186
|
|
|
98
187
|
const inputFields = getInputFields(field);
|
|
99
188
|
const useInputWrapper = inputFields != null;
|
|
100
|
-
const cliArgs = useInputWrapper
|
|
189
|
+
const cliArgs: CliArg[] = useInputWrapper
|
|
101
190
|
? inputFields
|
|
102
191
|
: field.args.map((a) => ({
|
|
103
192
|
name: a.name,
|
|
104
193
|
type: a.type,
|
|
105
194
|
required: isNonNullType(a.type),
|
|
195
|
+
description: a.description,
|
|
106
196
|
}));
|
|
107
197
|
|
|
108
198
|
const cmd = new Command(kebab(fieldName)).description(description);
|
|
@@ -111,14 +201,15 @@ function buildCommand(
|
|
|
111
201
|
const acceptsCompanySlug = cliArgs.some((a) => a.name === 'companySlug');
|
|
112
202
|
|
|
113
203
|
for (const a of cliArgs) {
|
|
114
|
-
const
|
|
204
|
+
const placeholder = isStructuredInput(a.type) ? 'json' : kebab(a.name);
|
|
205
|
+
const flag = `--${kebab(a.name)} <${placeholder}>`;
|
|
115
206
|
const isContext = a.name in CONTEXT_FIELDS;
|
|
116
207
|
if (isContext) {
|
|
117
|
-
cmd.option(flag, `${a
|
|
208
|
+
cmd.option(flag, `${describeArg(a)} (from context if omitted)`);
|
|
118
209
|
} else if (a.required) {
|
|
119
|
-
cmd.requiredOption(flag, a
|
|
210
|
+
cmd.requiredOption(flag, describeArg(a));
|
|
120
211
|
} else {
|
|
121
|
-
cmd.option(flag, a
|
|
212
|
+
cmd.option(flag, describeArg(a));
|
|
122
213
|
}
|
|
123
214
|
}
|
|
124
215
|
|
|
@@ -154,35 +245,27 @@ function buildCommand(
|
|
|
154
245
|
opts.companyId = resolved;
|
|
155
246
|
}
|
|
156
247
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
const input: Record<string, unknown> = {};
|
|
248
|
+
const collectArgs = (): Record<string, unknown> => {
|
|
249
|
+
const out: Record<string, unknown> = {};
|
|
160
250
|
for (const a of cliArgs) {
|
|
161
251
|
const raw = opts[a.name];
|
|
162
252
|
const ctxProp = CONTEXT_FIELDS[a.name];
|
|
163
|
-
const namedType = unwrap(a.type);
|
|
164
253
|
if (raw != null) {
|
|
165
|
-
|
|
254
|
+
try {
|
|
255
|
+
out[a.name] = coerceValue(raw, a.type, kebab(a.name));
|
|
256
|
+
} catch (coerceError) {
|
|
257
|
+
error((coerceError as Error).message);
|
|
258
|
+
hint('Run the command with --help to see the expected shape of each argument.');
|
|
259
|
+
process.exit(2);
|
|
260
|
+
}
|
|
166
261
|
} else if (ctxProp && ctx) {
|
|
167
|
-
|
|
262
|
+
out[a.name] = (ctx as Record<string, unknown>)[ctxProp];
|
|
168
263
|
}
|
|
169
264
|
}
|
|
170
|
-
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
}
|
|
174
|
-
variables = {};
|
|
175
|
-
for (const a of cliArgs) {
|
|
176
|
-
const raw = opts[a.name];
|
|
177
|
-
const ctxProp = CONTEXT_FIELDS[a.name];
|
|
178
|
-
const namedType = unwrap(a.type);
|
|
179
|
-
if (raw != null) {
|
|
180
|
-
variables[a.name] = coerceValue(raw, namedType.name);
|
|
181
|
-
} else if (ctxProp && ctx) {
|
|
182
|
-
variables[a.name] = (ctx as Record<string, unknown>)[ctxProp];
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
}
|
|
265
|
+
return out;
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
const variables: Record<string, unknown> = useInputWrapper ? { input: collectArgs() } : collectArgs();
|
|
186
269
|
|
|
187
270
|
const { query, fieldName: fName } = buildQuery(schema, kind, fieldName, variables, {
|
|
188
271
|
depth: opts.depth,
|