stepzen 0.16.0 → 0.17.0-beta.2
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/README.md +4 -3
- package/lib/commands/import.d.ts +15 -5
- package/lib/commands/import.js +117 -41
- package/lib/commands/init.d.ts +0 -3
- package/lib/commands/init.js +5 -108
- package/lib/commands/lint.js +2 -2
- package/lib/commands/start.d.ts +0 -1
- package/lib/commands/start.js +32 -37
- package/lib/commands/transpile.d.ts +1 -1
- package/lib/commands/transpile.js +5 -5
- package/lib/commands/validate.js +3 -3
- package/lib/generate/curl2sdl.d.ts +3 -20
- package/lib/generate/curl2sdl.js +43 -113
- package/lib/generate/graphql2sdl.d.ts +23 -0
- package/lib/generate/graphql2sdl.js +62 -0
- package/lib/generate/helpers.d.ts +18 -1
- package/lib/generate/helpers.js +81 -8
- package/lib/generate/index.d.ts +10 -3
- package/lib/generate/index.js +11 -2
- package/lib/shared/header-params-parser.d.ts +1 -1
- package/lib/shared/header.d.ts +21 -0
- package/lib/shared/header.js +13 -0
- package/lib/shared/utils.d.ts +15 -1
- package/lib/shared/utils.js +28 -7
- package/lib/shared/workspace.d.ts +8 -0
- package/lib/shared/workspace.js +105 -1
- package/lib/shared/zen-command.d.ts +5 -1
- package/lib/shared/zen-command.js +27 -0
- package/lib/start/console.d.ts +11 -2
- package/lib/start/console.js +37 -9
- package/lib/start/deploy.d.ts +5 -1
- package/lib/start/deploy.js +2 -26
- package/lib/start/index.d.ts +2 -3
- package/lib/start/index.js +6 -5
- package/oclif.manifest.json +1 -1
- package/package.json +3 -3
|
@@ -1,24 +1,6 @@
|
|
|
1
1
|
import { CurlArguments } from '../shared/curl-parser';
|
|
2
2
|
import { PathParam } from '../shared/path-params-parser';
|
|
3
|
-
|
|
4
|
-
name: string;
|
|
5
|
-
value: string;
|
|
6
|
-
};
|
|
7
|
-
export declare type NamePartsHeaderInput = {
|
|
8
|
-
name: string;
|
|
9
|
-
parts: HeaderInputValuePart[];
|
|
10
|
-
};
|
|
11
|
-
export declare type HeaderInput = NameValueHeaderInput | NamePartsHeaderInput;
|
|
12
|
-
export declare type HeaderInputConstantValuePart = {
|
|
13
|
-
kind: 'Constant';
|
|
14
|
-
value: string;
|
|
15
|
-
};
|
|
16
|
-
export declare type HeaderInputVariableValuePart = {
|
|
17
|
-
kind: 'Variable';
|
|
18
|
-
value: string;
|
|
19
|
-
name: string;
|
|
20
|
-
};
|
|
21
|
-
export declare type HeaderInputValuePart = HeaderInputConstantValuePart | HeaderInputVariableValuePart;
|
|
3
|
+
import { HeaderInput } from '../shared/header';
|
|
22
4
|
export declare type Curl2SdlOptions = {
|
|
23
5
|
curlArgs: CurlArguments;
|
|
24
6
|
name?: string;
|
|
@@ -28,6 +10,7 @@ export declare type Curl2SdlOptions = {
|
|
|
28
10
|
typePrefix?: string;
|
|
29
11
|
pathParams: readonly PathParam[];
|
|
30
12
|
headers: readonly HeaderInput[];
|
|
13
|
+
onConflict: 'overwrite' | 'append';
|
|
31
14
|
};
|
|
32
15
|
export declare type EditableCurl2SdlOptions = Pick<Curl2SdlOptions, 'curlArgs' | 'queryName' | 'rootType' | 'typePrefix' | 'pathParams'>;
|
|
33
16
|
export declare type CurlAnswers = {
|
|
@@ -65,7 +48,7 @@ export declare const compileParameterList: (headers: HeaderInput[], pathParams:
|
|
|
65
48
|
*/
|
|
66
49
|
export declare const makeDuplicateParamsMessage: (headers: HeaderInput[], pathParams: PathParam[], url: string) => string | null;
|
|
67
50
|
export declare const askCurlQuestions: (defaultAnswers?: Partial<CurlAnswers>) => Promise<EditableCurl2SdlOptions>;
|
|
68
|
-
export declare const curl2sdl: ({ curlArgs, name, source, queryName, rootType, typePrefix, pathParams, headers, }: Curl2SdlOptions) => Promise<{
|
|
51
|
+
export declare const curl2sdl: ({ curlArgs, name, source, queryName, rootType, typePrefix, pathParams, headers, onConflict, }: Curl2SdlOptions) => Promise<{
|
|
69
52
|
error: string;
|
|
70
53
|
} | {
|
|
71
54
|
outPath: string;
|
package/lib/generate/curl2sdl.js
CHANGED
|
@@ -2,22 +2,13 @@
|
|
|
2
2
|
// Copyright (c) 2020,2021,2022, StepZen, Inc.
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
4
|
exports.curl2sdl = exports.askCurlQuestions = exports.makeDuplicateParamsMessage = exports.compileParameterList = void 0;
|
|
5
|
-
const errors_1 = require("@oclif/errors");
|
|
6
5
|
const chalk = require("chalk");
|
|
7
|
-
const fs = require("fs-extra");
|
|
8
6
|
const inquirer = require("inquirer");
|
|
9
7
|
const lodash_1 = require("lodash");
|
|
10
|
-
const os = require("os");
|
|
11
|
-
const path = require("path");
|
|
12
8
|
const string_argv_1 = require("string-argv");
|
|
13
|
-
const node_fetch_1 = require("node-fetch");
|
|
14
|
-
const debug = require("debug");
|
|
15
|
-
const prettier = require("prettier");
|
|
16
|
-
const transpiler_1 = require("@stepzen/transpiler");
|
|
17
9
|
const curl_parser_1 = require("../shared/curl-parser");
|
|
18
10
|
const path_params_parser_1 = require("../shared/path-params-parser");
|
|
19
|
-
const
|
|
20
|
-
const errors_2 = require("../shared/errors");
|
|
11
|
+
const helpers_1 = require("./helpers");
|
|
21
12
|
/**
|
|
22
13
|
* Collect together all query parameters from HTTP headers and from the
|
|
23
14
|
* pathname, and return them as a uniform list.
|
|
@@ -116,7 +107,7 @@ exports.askCurlQuestions = async (defaultAnswers = {}) => {
|
|
|
116
107
|
},
|
|
117
108
|
{
|
|
118
109
|
name: 'typePrefix',
|
|
119
|
-
message: 'Prefix to add to all generated type
|
|
110
|
+
message: 'Prefix to add to all generated type names (leave blank to use defaults)',
|
|
120
111
|
},
|
|
121
112
|
];
|
|
122
113
|
questions = questions.map(question => (Object.assign(Object.assign({}, question), { default: lodash_1.get(defaultAnswers, question.name) })));
|
|
@@ -151,107 +142,46 @@ exports.askCurlQuestions = async (defaultAnswers = {}) => {
|
|
|
151
142
|
pathParams: parsedPathParamsOrError,
|
|
152
143
|
};
|
|
153
144
|
};
|
|
154
|
-
exports.curl2sdl = async ({ curlArgs, name, source, queryName, rootType, typePrefix, pathParams, headers, }) => {
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
};
|
|
192
|
-
debug('stepzen:curl2sdl')(url);
|
|
193
|
-
debug('stepzen:curl2sdl')(query);
|
|
194
|
-
debug('stepzen:curl2sdl')(variables);
|
|
195
|
-
const payload = JSON.stringify({
|
|
196
|
-
query,
|
|
197
|
-
variables,
|
|
198
|
-
});
|
|
199
|
-
debug('stepzen:curl2sdl')(payload);
|
|
200
|
-
const response = await node_fetch_1.default(url, {
|
|
201
|
-
method: 'POST',
|
|
202
|
-
headers: { 'Content-Type': 'application/json' },
|
|
203
|
-
body: JSON.stringify({
|
|
204
|
-
query,
|
|
205
|
-
variables,
|
|
206
|
-
}),
|
|
207
|
-
});
|
|
208
|
-
const text = await response.text();
|
|
209
|
-
debug('stepzen:curl2sdl')(text);
|
|
210
|
-
json = JSON.parse(text);
|
|
211
|
-
}
|
|
212
|
-
catch (error) {
|
|
213
|
-
debug('stepzen:curl2sdl')(error);
|
|
214
|
-
throw new errors_1.CLIError(errors_2.PERMANENT_STEPZEN_ERROR);
|
|
215
|
-
}
|
|
216
|
-
if (!json.data && !json.errors) {
|
|
217
|
-
debug('stepzen:curl2sdl')('expected the response from the JSON introspection service ' +
|
|
218
|
-
'to contain either `data` or `errors`');
|
|
219
|
-
throw new errors_1.CLIError(errors_2.PERMANENT_STEPZEN_ERROR);
|
|
220
|
-
}
|
|
221
|
-
if (json.errors) {
|
|
222
|
-
return { error: json.errors.map((error) => error.message).join('\n') };
|
|
223
|
-
}
|
|
224
|
-
const { getSDLFromCurl } = json.data;
|
|
225
|
-
if (!getSDLFromCurl) {
|
|
226
|
-
debug('stepzen:curl2sdl')('expected the response from the JSON introspection service ' +
|
|
227
|
-
'to contain a `getSDLFromCurl` object');
|
|
228
|
-
throw new errors_1.CLIError(errors_2.PERMANENT_STEPZEN_ERROR);
|
|
229
|
-
}
|
|
230
|
-
const { config, sdl } = getSDLFromCurl;
|
|
231
|
-
if (!config && !sdl) {
|
|
232
|
-
debug('stepzen:curl2sdl')('expected the response from the JSON introspection service ' +
|
|
233
|
-
'to contain at least one of `getSDLFromCurl.config` or ' +
|
|
234
|
-
'`getSDLFromCurl.sdl` properties');
|
|
235
|
-
throw new errors_1.CLIError(errors_2.PERMANENT_STEPZEN_ERROR);
|
|
236
|
-
}
|
|
237
|
-
// write out the generated config and schema files
|
|
238
|
-
const tmp = path.join(os.tmpdir(), `stepzen-curl2sdl-${Date.now()}`);
|
|
239
|
-
fs.ensureDirSync(tmp);
|
|
240
|
-
// fs.ensureDirSync(path.join(tmp, subfolder))
|
|
241
|
-
if (config) {
|
|
242
|
-
fs.writeFileSync(path.join(tmp, 'config.yaml'), prettier.format(config, { parser: 'yaml' }));
|
|
243
|
-
}
|
|
244
|
-
if (sdl) {
|
|
245
|
-
fs.writeFileSync(path.join(tmp, 'index.graphql'), prettier.format(sdl, { parser: 'graphql' }));
|
|
246
|
-
}
|
|
247
|
-
const result = await transpiler_1.merge(source, {
|
|
248
|
-
name: name || 'curl',
|
|
249
|
-
source: tmp,
|
|
250
|
-
}, {
|
|
251
|
-
answers: {},
|
|
252
|
-
output: null,
|
|
253
|
-
silent: true,
|
|
254
|
-
mergeTypes: false,
|
|
145
|
+
exports.curl2sdl = async ({ curlArgs, name, source, queryName, rootType, typePrefix, pathParams, headers, onConflict, }) => {
|
|
146
|
+
const response = await helpers_1.queryIntrospectionService({
|
|
147
|
+
operation: 'getSDLFromCurl',
|
|
148
|
+
variables: {
|
|
149
|
+
command: {
|
|
150
|
+
type: 'String!',
|
|
151
|
+
value: curlArgs.url,
|
|
152
|
+
},
|
|
153
|
+
queryName: {
|
|
154
|
+
type: 'String',
|
|
155
|
+
value: queryName || null,
|
|
156
|
+
},
|
|
157
|
+
rootType: {
|
|
158
|
+
type: 'String',
|
|
159
|
+
value: rootType || null,
|
|
160
|
+
},
|
|
161
|
+
typePrefix: {
|
|
162
|
+
type: 'String',
|
|
163
|
+
value: typePrefix || null,
|
|
164
|
+
},
|
|
165
|
+
headers: {
|
|
166
|
+
type: '[HeaderInput!]',
|
|
167
|
+
value: headers && headers.length > 0 ? headers : null,
|
|
168
|
+
},
|
|
169
|
+
data: {
|
|
170
|
+
type: 'String',
|
|
171
|
+
value: curlArgs.data || null,
|
|
172
|
+
},
|
|
173
|
+
method: {
|
|
174
|
+
type: 'HTTPMethod!',
|
|
175
|
+
value: curlArgs.method,
|
|
176
|
+
},
|
|
177
|
+
pathParams: {
|
|
178
|
+
type: '[PathParamInput!]',
|
|
179
|
+
value: pathParams.length > 0 ? pathParams : null,
|
|
180
|
+
},
|
|
181
|
+
},
|
|
255
182
|
});
|
|
256
|
-
|
|
183
|
+
if (response.error) {
|
|
184
|
+
return { error: response.error };
|
|
185
|
+
}
|
|
186
|
+
return helpers_1.writeSdlAndConfig(name || 'curl', source, onConflict, response);
|
|
257
187
|
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { HeaderInput } from '../shared/header';
|
|
2
|
+
export declare type GraphQL2SdlOptions = {
|
|
3
|
+
name?: string;
|
|
4
|
+
source: string;
|
|
5
|
+
endpoint: string;
|
|
6
|
+
typePrefix?: string;
|
|
7
|
+
includeRootOperations?: boolean;
|
|
8
|
+
header?: string;
|
|
9
|
+
onConflict: 'overwrite' | 'append';
|
|
10
|
+
};
|
|
11
|
+
export declare type EditableGraphQL2SdlOptions = Pick<GraphQL2SdlOptions, 'endpoint' | 'typePrefix' | 'includeRootOperations' | 'header'>;
|
|
12
|
+
export declare type GraphQLAnswers = {
|
|
13
|
+
endpoint: string;
|
|
14
|
+
typePrefix: string;
|
|
15
|
+
includeRootOperations: boolean;
|
|
16
|
+
headers: readonly HeaderInput[];
|
|
17
|
+
};
|
|
18
|
+
export declare const askGraphQLQuestions: (defaultAnswers?: Partial<GraphQLAnswers>) => Promise<EditableGraphQL2SdlOptions>;
|
|
19
|
+
export declare const graphql2sdl: ({ name, source, endpoint, typePrefix, includeRootOperations, header, onConflict, }: GraphQL2SdlOptions) => Promise<{
|
|
20
|
+
error: string;
|
|
21
|
+
} | {
|
|
22
|
+
outPath: string;
|
|
23
|
+
}>;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Copyright (c) 2020,2021,2022, StepZen, Inc.
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.graphql2sdl = exports.askGraphQLQuestions = void 0;
|
|
5
|
+
const inquirer = require("inquirer");
|
|
6
|
+
const lodash_1 = require("lodash");
|
|
7
|
+
const header_1 = require("../shared/header");
|
|
8
|
+
const helpers_1 = require("./helpers");
|
|
9
|
+
exports.askGraphQLQuestions = async (defaultAnswers = {}) => {
|
|
10
|
+
let questions = [
|
|
11
|
+
{
|
|
12
|
+
name: 'endpoint',
|
|
13
|
+
message: 'What is the GraphQL endpoint URL?',
|
|
14
|
+
validate: input => input.trim() !== '',
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
name: 'typePrefix',
|
|
18
|
+
message: 'Prefix to add to all generated type names (leave blank for none)',
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
name: 'includeRootOperations',
|
|
22
|
+
type: 'confirm',
|
|
23
|
+
message: 'Should type prefix be added to query and mutation fields as well?',
|
|
24
|
+
when: answers => Boolean(answers.typePrefix),
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
name: 'header',
|
|
28
|
+
type: 'input',
|
|
29
|
+
message: 'Add an HTTP header, e.g. Header-Name: header value (leave blank for none)',
|
|
30
|
+
validate: input => input === '' || header_1.HEADER_REGEX.test(input.trim()) || 'invalid header',
|
|
31
|
+
},
|
|
32
|
+
];
|
|
33
|
+
questions = questions.map(question => (Object.assign(Object.assign({}, question), { default: lodash_1.get(defaultAnswers, question.name) })));
|
|
34
|
+
return inquirer.prompt(questions);
|
|
35
|
+
};
|
|
36
|
+
exports.graphql2sdl = async ({ name, source, endpoint, typePrefix, includeRootOperations, header, onConflict, }) => {
|
|
37
|
+
const response = await helpers_1.queryIntrospectionService({
|
|
38
|
+
operation: 'getSDLFromGraphQL',
|
|
39
|
+
variables: {
|
|
40
|
+
endpoint: {
|
|
41
|
+
type: 'String!',
|
|
42
|
+
value: endpoint,
|
|
43
|
+
},
|
|
44
|
+
typePrefix: {
|
|
45
|
+
type: 'String',
|
|
46
|
+
value: typePrefix || null,
|
|
47
|
+
},
|
|
48
|
+
includeRootOperations: {
|
|
49
|
+
type: 'Boolean',
|
|
50
|
+
value: includeRootOperations || null,
|
|
51
|
+
},
|
|
52
|
+
headers: {
|
|
53
|
+
type: '[HeaderInput!]',
|
|
54
|
+
value: header ? [header_1.parseHeaderNameValue(header)] : null,
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
if (response.error) {
|
|
59
|
+
return { error: response.error };
|
|
60
|
+
}
|
|
61
|
+
return helpers_1.writeSdlAndConfig(name || 'graphql', source, onConflict, response);
|
|
62
|
+
};
|
|
@@ -11,4 +11,21 @@ export declare const getSchema: (arg: string) => string;
|
|
|
11
11
|
export declare const getTemplates: () => Promise<string>;
|
|
12
12
|
export declare const askGeneratorQuestions: (id: string, settings: any, state: any) => Promise<any>;
|
|
13
13
|
export declare const askTemplateQuestions: (id: string, settings: any, state: any) => Promise<any>;
|
|
14
|
-
export declare
|
|
14
|
+
export declare type IntrospectionServiceQuery = {
|
|
15
|
+
operation: string;
|
|
16
|
+
variables: {
|
|
17
|
+
[id: string]: {
|
|
18
|
+
type: string;
|
|
19
|
+
value: any | null;
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
export declare type IntrospectionServiceResponse = {
|
|
24
|
+
sdl?: string;
|
|
25
|
+
config?: string;
|
|
26
|
+
error?: string;
|
|
27
|
+
};
|
|
28
|
+
export declare const queryIntrospectionService: (query: IntrospectionServiceQuery) => Promise<IntrospectionServiceResponse>;
|
|
29
|
+
export declare const writeSdlAndConfig: (name: string, source: string, onConflict: 'overwrite' | 'append', { config, sdl }: IntrospectionServiceResponse) => Promise<{
|
|
30
|
+
outPath: string;
|
|
31
|
+
}>;
|
package/lib/generate/helpers.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
// Copyright (c) 2020,2021,2022, StepZen, Inc.
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.
|
|
4
|
+
exports.writeSdlAndConfig = exports.queryIntrospectionService = exports.askTemplateQuestions = exports.askGeneratorQuestions = exports.getTemplates = exports.getSchema = exports.getConfiguration = exports.createGeneratorFiles = void 0;
|
|
5
5
|
const errors_1 = require("@oclif/errors");
|
|
6
6
|
const chalk = require("chalk");
|
|
7
7
|
const debug = require("debug");
|
|
@@ -13,6 +13,7 @@ const os = require("os");
|
|
|
13
13
|
const path = require("path");
|
|
14
14
|
const shell = require("shelljs");
|
|
15
15
|
const transpiler = require("@stepzen/transpiler");
|
|
16
|
+
const prettier = require("prettier");
|
|
16
17
|
const constants_1 = require("../shared/constants");
|
|
17
18
|
const errors_2 = require("../shared/errors");
|
|
18
19
|
const { version } = require('../../package.json');
|
|
@@ -93,7 +94,7 @@ exports.getSchema = (arg) => {
|
|
|
93
94
|
const schema = arg.trim();
|
|
94
95
|
// Now supports importing only one schema at a time:
|
|
95
96
|
// https://github.com/steprz/stepzen-cli/issues/628
|
|
96
|
-
if (schema.includes(',')) {
|
|
97
|
+
if (schema.includes(',') || schema.includes(' ')) {
|
|
97
98
|
throw new errors_1.CLIError("Importing multiple schemas is no longer supported; please specify only one (e.g. 'stepzen import mysql')");
|
|
98
99
|
}
|
|
99
100
|
// support `postgres` as an alias to `postgresql`
|
|
@@ -160,14 +161,86 @@ exports.askTemplateQuestions = async (id, settings, state) => {
|
|
|
160
161
|
const answers = await inquirer.prompt(questions);
|
|
161
162
|
return Object.assign(Object.assign({}, state), answers);
|
|
162
163
|
};
|
|
163
|
-
exports.
|
|
164
|
-
|
|
164
|
+
exports.queryIntrospectionService = async (query) => {
|
|
165
|
+
let json;
|
|
166
|
+
try {
|
|
167
|
+
const url = `${constants_1.STEPZEN_JSON2SDL_SERVER_URL}/api/graphql`;
|
|
168
|
+
debug('stepzen:introspection')(url);
|
|
169
|
+
const queryString = `query (
|
|
170
|
+
${Object.entries(query.variables).map(([id, { type }]) => `$${id}: ${type}`)}
|
|
171
|
+
) {
|
|
172
|
+
${query.operation}(${Object.keys(query.variables).map(id => `${id}: $${id}`)}) {
|
|
173
|
+
sdl
|
|
174
|
+
config
|
|
175
|
+
}
|
|
176
|
+
}`;
|
|
177
|
+
debug('stepzen:introspection')(queryString);
|
|
178
|
+
debug('stepzen:introspection')(query.variables);
|
|
179
|
+
const payload = JSON.stringify({
|
|
180
|
+
query: queryString,
|
|
181
|
+
variables: lodash_1.mapValues(query.variables, ({ value }) => value),
|
|
182
|
+
});
|
|
183
|
+
debug('stepzen:introspection')(payload);
|
|
184
|
+
const response = await node_fetch_1.default(url, {
|
|
185
|
+
method: 'POST',
|
|
186
|
+
headers: { 'Content-Type': 'application/json' },
|
|
187
|
+
body: payload,
|
|
188
|
+
});
|
|
189
|
+
const text = await response.text();
|
|
190
|
+
debug('stepzen:introspection')(text);
|
|
191
|
+
json = JSON.parse(text);
|
|
192
|
+
}
|
|
193
|
+
catch (error) {
|
|
194
|
+
debug('stepzen:introspection')(error);
|
|
195
|
+
throw new errors_1.CLIError(errors_2.PERMANENT_STEPZEN_ERROR);
|
|
196
|
+
}
|
|
197
|
+
if (!json.data && !json.errors) {
|
|
198
|
+
debug('stepzen:introspection')('expected the response from the JSON introspection service ' +
|
|
199
|
+
'to contain either `data` or `errors`');
|
|
200
|
+
throw new errors_1.CLIError(errors_2.PERMANENT_STEPZEN_ERROR);
|
|
201
|
+
}
|
|
202
|
+
if (json.errors) {
|
|
203
|
+
const compileError = (error) => {
|
|
204
|
+
var _a;
|
|
205
|
+
return error.message +
|
|
206
|
+
(((_a = error.extensions) === null || _a === void 0 ? void 0 : _a.details) ? `: ${error.extensions.details}` : '');
|
|
207
|
+
};
|
|
208
|
+
return { error: json.errors.map(compileError).join('\n') };
|
|
209
|
+
}
|
|
210
|
+
if (!json.data[query.operation]) {
|
|
211
|
+
debug('stepzen:graphql2sdl')('expected the response from the JSON introspection service ' +
|
|
212
|
+
`to contain a \`${query.operation}\` object`);
|
|
213
|
+
throw new errors_1.CLIError(errors_2.PERMANENT_STEPZEN_ERROR);
|
|
214
|
+
}
|
|
215
|
+
const { config, sdl } = json.data[query.operation];
|
|
216
|
+
if (!config && !sdl) {
|
|
217
|
+
debug('stepzen:curl2sdl')('expected the response from the JSON introspection service ' +
|
|
218
|
+
`to contain at least one of \`${query.operation}.config\` or ` +
|
|
219
|
+
`\`${query.operation}.sdl\` properties`);
|
|
220
|
+
throw new errors_1.CLIError(errors_2.PERMANENT_STEPZEN_ERROR);
|
|
221
|
+
}
|
|
222
|
+
return { config, sdl };
|
|
223
|
+
};
|
|
224
|
+
exports.writeSdlAndConfig = async (name, source, onConflict, { config, sdl }) => {
|
|
225
|
+
// write out the generated config and schema files
|
|
226
|
+
const tmp = path.join(os.tmpdir(), `stepzen-introspection-${Date.now()}`);
|
|
227
|
+
fs.ensureDirSync(tmp);
|
|
228
|
+
// fs.ensureDirSync(path.join(tmp, subfolder))
|
|
229
|
+
if (config) {
|
|
230
|
+
fs.writeFileSync(path.join(tmp, 'config.yaml'), prettier.format(config, { parser: 'yaml' }));
|
|
231
|
+
}
|
|
232
|
+
if (sdl) {
|
|
233
|
+
fs.writeFileSync(path.join(tmp, 'index.graphql'), prettier.format(sdl, { parser: 'graphql' }));
|
|
234
|
+
}
|
|
235
|
+
const result = await transpiler.merge(source, {
|
|
165
236
|
name: name,
|
|
166
|
-
source:
|
|
237
|
+
source: tmp,
|
|
167
238
|
}, {
|
|
168
|
-
answers,
|
|
169
|
-
silent: true,
|
|
239
|
+
answers: {},
|
|
170
240
|
output: null,
|
|
241
|
+
silent: true,
|
|
242
|
+
mergeTypes: false,
|
|
243
|
+
onConflict,
|
|
171
244
|
});
|
|
172
|
-
return
|
|
245
|
+
return { outPath: result };
|
|
173
246
|
};
|
package/lib/generate/index.d.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
|
-
declare
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
export declare type GenerateOptions = {
|
|
2
|
+
schema: string;
|
|
3
|
+
name?: string;
|
|
4
|
+
onConflict: 'overwrite' | 'append';
|
|
5
|
+
source: string;
|
|
6
|
+
preAnswered?: {
|
|
7
|
+
[question: string]: string;
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
declare const _default: ({ schema, name, onConflict, source, preAnswered, }: GenerateOptions) => Promise<string>;
|
|
4
11
|
export default _default;
|
package/lib/generate/index.js
CHANGED
|
@@ -6,8 +6,9 @@ const core_1 = require("@oclif/core");
|
|
|
6
6
|
const fs = require("fs-extra");
|
|
7
7
|
const lodash = require("lodash");
|
|
8
8
|
const path = require("path");
|
|
9
|
+
const transpiler = require("@stepzen/transpiler");
|
|
9
10
|
const helpers_1 = require("./helpers");
|
|
10
|
-
exports.default = async (schema, name, source, preAnswered = {}) => {
|
|
11
|
+
exports.default = async ({ schema, name, onConflict, source, preAnswered = {}, }) => {
|
|
11
12
|
var _a, _b;
|
|
12
13
|
// Store the generator
|
|
13
14
|
let generator;
|
|
@@ -82,7 +83,15 @@ exports.default = async (schema, name, source, preAnswered = {}) => {
|
|
|
82
83
|
// if (generator.type === 'template')
|
|
83
84
|
files = path.join(templates, schema);
|
|
84
85
|
}
|
|
85
|
-
const output = await
|
|
86
|
+
const output = await transpiler.merge(source, {
|
|
87
|
+
name: name || schema,
|
|
88
|
+
source: files,
|
|
89
|
+
}, {
|
|
90
|
+
answers,
|
|
91
|
+
silent: true,
|
|
92
|
+
output: null,
|
|
93
|
+
onConflict,
|
|
94
|
+
});
|
|
86
95
|
core_1.CliUx.ux.action.stop();
|
|
87
96
|
// Housekeeping
|
|
88
97
|
cleanUp();
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { HeaderInput } from '
|
|
1
|
+
import { HeaderInput } from './header';
|
|
2
2
|
import { ParseError, CurlHeader } from './curl-parser';
|
|
3
3
|
export declare const parseHeaderParam: (headers: readonly CurlHeader[], headerParam: string) => {
|
|
4
4
|
header: HeaderInput;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export declare type NameValueHeaderInput = {
|
|
2
|
+
name: string;
|
|
3
|
+
value: string;
|
|
4
|
+
};
|
|
5
|
+
export declare type NamePartsHeaderInput = {
|
|
6
|
+
name: string;
|
|
7
|
+
parts: HeaderInputValuePart[];
|
|
8
|
+
};
|
|
9
|
+
export declare type HeaderInput = NameValueHeaderInput | NamePartsHeaderInput;
|
|
10
|
+
export declare type HeaderInputConstantValuePart = {
|
|
11
|
+
kind: 'Constant';
|
|
12
|
+
value: string;
|
|
13
|
+
};
|
|
14
|
+
export declare type HeaderInputVariableValuePart = {
|
|
15
|
+
kind: 'Variable';
|
|
16
|
+
value: string;
|
|
17
|
+
name: string;
|
|
18
|
+
};
|
|
19
|
+
export declare type HeaderInputValuePart = HeaderInputConstantValuePart | HeaderInputVariableValuePart;
|
|
20
|
+
export declare const HEADER_REGEX: RegExp;
|
|
21
|
+
export declare const parseHeaderNameValue: (input: string) => NameValueHeaderInput | null;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Copyright (c) 2020,2021,2022, StepZen, Inc.
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.parseHeaderNameValue = exports.HEADER_REGEX = void 0;
|
|
5
|
+
exports.HEADER_REGEX = /([\w-]+): (.*)/;
|
|
6
|
+
exports.parseHeaderNameValue = (input) => {
|
|
7
|
+
const match = input.match(exports.HEADER_REGEX);
|
|
8
|
+
if (match) {
|
|
9
|
+
const [name, value] = match.splice(1, 2);
|
|
10
|
+
return { name, value };
|
|
11
|
+
}
|
|
12
|
+
return null;
|
|
13
|
+
};
|
package/lib/shared/utils.d.ts
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
|
-
export declare const clearConsole: () => void;
|
|
2
1
|
export declare const getDirectory: (d?: string | undefined) => string;
|
|
2
|
+
/**
|
|
3
|
+
* Replace the path prefix with `~` if it is inside the user's home directory.
|
|
4
|
+
*
|
|
5
|
+
* @param {*} absPath an absolute file or directory path
|
|
6
|
+
* @returns {*} the same path that possibly looks more friendly to users
|
|
7
|
+
*/
|
|
8
|
+
export declare const homeRelative: (absPath: string) => string;
|
|
9
|
+
/**
|
|
10
|
+
* Replace the path prefix with `~` if it is inside the user's home directory.
|
|
11
|
+
*
|
|
12
|
+
* @param {*} absPath an absolute file or directory path
|
|
13
|
+
* @param {*} workspace an absolute file or the workspace root
|
|
14
|
+
* @returns {*} the same path that possibly looks more friendly to users
|
|
15
|
+
*/
|
|
16
|
+
export declare const workspaceRelative: (absPath: string, workspace: string) => string;
|
|
3
17
|
export declare const getStepZenExtensions: () => Promise<string>;
|
|
4
18
|
export declare const validateEndpoint: (endpoint: string) => any;
|
|
5
19
|
export declare const maskStepZenKey: (key: string) => string;
|
package/lib/shared/utils.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
// Copyright (c) 2020,2021,2022, StepZen, Inc.
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.maskStepZenKey = exports.validateEndpoint = exports.getStepZenExtensions = exports.
|
|
4
|
+
exports.maskStepZenKey = exports.validateEndpoint = exports.getStepZenExtensions = exports.workspaceRelative = exports.homeRelative = exports.getDirectory = void 0;
|
|
5
5
|
const errors_1 = require("@oclif/errors");
|
|
6
6
|
const debug = require("debug");
|
|
7
7
|
const node_fetch_1 = require("node-fetch");
|
|
@@ -10,12 +10,6 @@ const os = require("os");
|
|
|
10
10
|
const path = require("path");
|
|
11
11
|
const prettier = require("prettier");
|
|
12
12
|
const constants_1 = require("./constants");
|
|
13
|
-
exports.clearConsole = () => {
|
|
14
|
-
process.stdout.write(
|
|
15
|
-
// Taken from create-react-app
|
|
16
|
-
// eslint-disable-next-line unicorn/no-hex-escape
|
|
17
|
-
process.platform === 'win32' ? '\x1B[2J\x1B[0f' : '\x1B[2J\x1B[3J\x1B[H');
|
|
18
|
-
};
|
|
19
13
|
exports.getDirectory = (d = process.cwd()) => {
|
|
20
14
|
let directory = d;
|
|
21
15
|
// If it starts with `~`, expand this
|
|
@@ -31,6 +25,33 @@ exports.getDirectory = (d = process.cwd()) => {
|
|
|
31
25
|
}
|
|
32
26
|
return directory;
|
|
33
27
|
};
|
|
28
|
+
/**
|
|
29
|
+
* Replace the path prefix with `~` if it is inside the user's home directory.
|
|
30
|
+
*
|
|
31
|
+
* @param {*} absPath an absolute file or directory path
|
|
32
|
+
* @returns {*} the same path that possibly looks more friendly to users
|
|
33
|
+
*/
|
|
34
|
+
exports.homeRelative = (absPath) => {
|
|
35
|
+
let pretty = absPath;
|
|
36
|
+
if (absPath.startsWith(os.homedir())) {
|
|
37
|
+
pretty = '~' + absPath.substring(os.homedir().length);
|
|
38
|
+
}
|
|
39
|
+
return pretty;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Replace the path prefix with `~` if it is inside the user's home directory.
|
|
43
|
+
*
|
|
44
|
+
* @param {*} absPath an absolute file or directory path
|
|
45
|
+
* @param {*} workspace an absolute file or the workspace root
|
|
46
|
+
* @returns {*} the same path that possibly looks more friendly to users
|
|
47
|
+
*/
|
|
48
|
+
exports.workspaceRelative = (absPath, workspace) => {
|
|
49
|
+
let pretty = absPath;
|
|
50
|
+
if (absPath.startsWith(workspace)) {
|
|
51
|
+
pretty = '.' + absPath.substring(workspace.length);
|
|
52
|
+
}
|
|
53
|
+
return pretty;
|
|
54
|
+
};
|
|
34
55
|
exports.getStepZenExtensions = async () => {
|
|
35
56
|
const domain = constants_1.STEPZEN_DOMAIN.replace('.io', '.net');
|
|
36
57
|
const url = `https://www.${domain}/directives.graphql`;
|
|
@@ -1,2 +1,10 @@
|
|
|
1
1
|
import { Workspace } from './types';
|
|
2
|
+
export declare const validateWorkspaceName: (directory: string, name: string) => boolean;
|
|
3
|
+
export declare const generateWorkspaceName: (directory: string) => string;
|
|
4
|
+
export declare const guessSchemaRoot: (directory: string) => string;
|
|
2
5
|
export declare const getWorkspace: (directory: string) => Workspace | null;
|
|
6
|
+
export declare const initWorkspace: (args: {
|
|
7
|
+
directory?: string;
|
|
8
|
+
endpoint?: string;
|
|
9
|
+
yes?: boolean;
|
|
10
|
+
}) => Promise<Workspace>;
|