-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtsgen.ts
132 lines (115 loc) · 4.47 KB
/
tsgen.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import {Command} from '@contentstack/cli-command'
import {flags} from '@contentstack/cli-utilities'
import {getGlobalFields, stackConnect, StackConnectionConfig, generateGraphQLTypeDef} from '../lib/stack/client'
import {ContentType} from '../lib/stack/schema'
import tsgenRunner from '../lib/tsgen/runner'
export default class TypeScriptCodeGeneratorCommand extends Command {
static description = 'generate TypeScript typings from a Stack';
static examples = [
'$ csdx tsgen -a "delivery token alias" -o "contentstack/generated.d.ts"',
'$ csdx tsgen -a "delivery token alias" -o "contentstack/generated.d.ts" -p "I"',
'$ csdx tsgen -a "delivery token alias" -o "contentstack/generated.d.ts" --no-doc',
'$ csdx tsgen -a "delivery token alias" -o "contentstack/generated.d.ts" --api-type graphql',
'$ csdx tsgen -a "delivery token alias" -o "contentstack/generated.d.ts" --api-type graphql --namespace "GraphQL" ',
];
static flags: any = {
'token-alias': flags.string({
char: 'a',
description: 'delivery token alias',
hidden: false,
multiple: false,
required: true,
}),
output: flags.string({
char: 'o',
description: 'full path to output',
hidden: false,
multiple: false,
required: true,
}),
prefix: flags.string({
char: 'p',
description: 'interface prefix, e.g. "I"',
hidden: false,
multiple: false,
default: '',
required: false,
}),
doc: flags.boolean({
char: 'd',
description: 'include documentation comments',
default: true,
allowNo: true,
}),
branch: flags.string({
description: 'branch',
hidden: false,
multiple: false,
}),
'include-system-fields': flags.boolean({
description: 'include system fields in generated types',
default: false,
}),
'api-type': flags.string({
default: 'rest',
multiple: false,
options: ['rest', 'graphql'],
description: '[Optional] Please enter an API type to generate the type definitions.',
}),
namespace: flags.string({
description: '[Optional]Please enter a namespace for the GraphQL API type to organize the generated types.',
}),
};
async run() {
try {
const {flags} = await this.parse(TypeScriptCodeGeneratorCommand)
const token = this.getToken(flags['token-alias'])
const prefix = flags.prefix
const includeDocumentation = flags.doc
const outputPath = flags.output
const branch = flags.branch
const includeSystemFields = flags['include-system-fields']
const namespace = flags.namespace
if (token.type !== 'delivery') {
this.warn('Possibly using a management token. You may not be able to connect to your Stack. Please use a delivery token.')
}
if (!outputPath || !outputPath.trim()) {
this.error('Please provide an output path.', {exit: 2})
}
const config: StackConnectionConfig = {
apiKey: token.apiKey,
token: token.token,
region: (this.region.name === 'NA') ? 'us' : this.region.name.toLowerCase(),
environment: token.environment || '',
branch: branch || null,
}
if (flags['api-type'] === 'graphql') {
const result = await generateGraphQLTypeDef(config, outputPath, namespace)
if (result) {
this.log(`Successfully added the GraphQL schema type definitions to '${result.outputPath}'.`)
} else {
this.log('No schema found in the stack! Please use a valid stack.')
}
} else {
const [client, globalFields] = await Promise.all([stackConnect(this.deliveryAPIClient.Stack, config, this.cdaHost), getGlobalFields(config, this.cdaHost)])
let schemas: ContentType[] = []
if (client.types?.length) {
if ((globalFields as any)?.global_fields?.length) {
schemas = schemas.concat((globalFields as any).global_fields as ContentType)
schemas = schemas.map(schema => ({
...schema,
schema_type: 'global_field',
}))
}
schemas = schemas.concat(client.types)
const result = await tsgenRunner(outputPath, schemas, prefix, includeDocumentation, includeSystemFields)
this.log(`Wrote ${result.definitions} Content Types to '${result.outputPath}'.`)
} else {
this.log('No Content Types exist in the Stack.')
}
}
} catch (error: any) {
this.error(error as any, {exit: 1})
}
}
}