-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtsgen.ts
More file actions
247 lines (215 loc) · 7.91 KB
/
tsgen.ts
File metadata and controls
247 lines (215 loc) · 7.91 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import { Command } from "@contentstack/cli-command";
import { flags, FlagInput, log } from "@contentstack/cli-utilities";
import * as path from "path";
import * as fs from "fs";
import { cliux } from "@contentstack/cli-utilities";
import { generateTS, graphqlTS } from "@contentstack/types-generator";
import { sanitizePath, printFormattedError } from "../lib/helper";
import { StackConnectionConfig } from "../types";
function createOutputPath(outputFile: string) {
const outputPath = path.resolve(
sanitizePath(process.cwd()),
sanitizePath(outputFile),
);
const dirName = path.dirname(outputPath);
fs.mkdirSync(dirName, { recursive: true });
return outputPath;
}
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" --include-referenced-entry',
'$ 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" ',
];
// Check if a region is a default Contentstack region
private isDefaultRegion(region: string): boolean {
const defaultRegions = [
"US",
"EU",
"AU",
"AZURE_NA",
"AZURE_EU",
"GCP_NA",
"GCP_EU",
];
return defaultRegions.includes(region.toUpperCase());
}
static flags: FlagInput = {
"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,
}),
"include-editable-tags": flags.boolean({
description: "include editable tags in generated types",
default: false,
}),
"include-referenced-entry": flags.boolean({
description:
"Includes the ReferencedEntry interface in generated types. Use this option to add a generic interface for handling referenced entries when the exact content type is unknown or when you need a flexible reference type",
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 filePath = flags.output;
const branch = flags.branch;
const includeSystemFields = flags["include-system-fields"];
const includeEditableTags = flags["include-editable-tags"];
const includeReferencedEntry = flags["include-referenced-entry"];
const namespace = flags.namespace;
const outputPath = createOutputPath(filePath);
if (token.type !== "delivery") {
this.warn(
"You might be using a management token. Connection may fail. Use a delivery token instead.",
);
}
if (!outputPath || !outputPath.trim()) {
this.error("Output path is required.", { 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 || undefined,
host: this.cdaHost,
};
// Generate the GraphQL schema TypeScript definitions
if (flags["api-type"] === "graphql") {
try {
if (config.region === "us") {
config.region = "US";
}
// Check if token has delivery type (required for GraphQL)
if (token.type !== "delivery") {
throw new Error(
"GraphQL API requires a delivery token. Management tokens aren't supported.",
);
}
// Prepare GraphQL config - only include host for custom regions
const graphqlConfig: any = {
apiKey: config.apiKey,
token: config.token,
environment: config.environment,
namespace: namespace,
logger: log,
};
// Add region or host based on whether it's a custom region
if (config.host && !this.isDefaultRegion(config.region)) {
// Custom region - include both region and host
graphqlConfig.region = config.region;
graphqlConfig.host = config.host;
} else {
// Default region - only include region
graphqlConfig.region = config.region;
}
const result = await graphqlTS(graphqlConfig);
if (!result) {
throw new Error("No result returned by the GraphQL API.");
}
fs.writeFileSync(outputPath, result);
this.log(
`Successfully added the GraphQL schema type definitions to '${outputPath}'.`,
);
} catch (error: any) {
printFormattedError(error, error?.context || "graphql");
process.exit(1);
}
} else {
// Generate the Content Types TypeScript definitions
try {
const result = await generateTS({
...config,
tokenType: "delivery",
includeDocumentation: includeDocumentation,
prefix,
systemFields: includeSystemFields,
isEditableTags: includeEditableTags,
includeReferencedEntry,
logger: log,
});
fs.writeFileSync(outputPath, result || "");
// Check if we have any skipped content types and show summary
if (
result &&
typeof result === "string" &&
result.includes(
"Generation completed successfully with partial schema",
)
) {
cliux.print("");
log.success("Type generation completed successfully with partial schema!");
log.warn(
"Some content types were skipped due to validation issues, but types were generated for valid content types."
);
log.info(
"Check the output above for details on what was skipped and suggestions for fixing issues."
);
} else {
log.success(`Successfully added the Content Types to '${outputPath}'.`);
}
// this.log(`Wrote ${outputPath} Content Types to '${result.outputPath}'.`)
} catch (error: any) {
printFormattedError(error, error?.context || "tsgen");
process.exit(1);
}
}
} catch (error: any) {
printFormattedError(error, error?.context || "tsgen");
process.exit(1);
}
}
}