-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtsgen.ts
More file actions
227 lines (196 loc) · 6.97 KB
/
tsgen.ts
File metadata and controls
227 lines (196 loc) · 6.97 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
import { Command } from "@contentstack/cli-command";
import { flags, FlagInput } from "@contentstack/cli-utilities";
import * as path from "path";
import * as fs from "fs";
import { generateTS, graphqlTS } from "@contentstack/types-generator";
import { sanitizePath } 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" --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,
}),
"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 namespace = flags.namespace;
const outputPath = createOutputPath(filePath);
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 || 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 are not supported for GraphQL operations.",
);
}
// Prepare GraphQL config - only include host for custom regions
const graphqlConfig: any = {
apiKey: config.apiKey,
token: config.token,
environment: config.environment,
namespace: namespace,
};
// 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("GraphQL API returned no result");
}
fs.writeFileSync(outputPath, result);
this.log(
`Successfully added the GraphQL schema type definitions to '${outputPath}'.`,
);
} catch (error: any) {
const errorMessage =
error?.error_message ||
error?.message ||
"An error occurred while generating GraphQL types";
this.error(errorMessage, { exit: 1 });
}
} else {
// Generate the Content Types TypeScript definitions
try {
const result = await generateTS({
...config,
tokenType: "delivery",
includeDocumentation: includeDocumentation,
prefix,
systemFields: includeSystemFields,
isEditableTags: includeEditableTags,
});
fs.writeFileSync(outputPath, result || "");
// -- TODO : Add count support for the number of Content Types generated
this.log(`Successfully added the Content Types to '${outputPath}'.`);
// this.log(`Wrote ${outputPath} Content Types to '${result.outputPath}'.`)
} catch (error: any) {
const errorMessage =
error?.error_message ||
error?.message ||
"An error occurred while generating TypeScript types";
this.error(errorMessage, { exit: 1 });
}
}
} catch (error: any) {
const errorMessage =
error?.error_message ||
error?.message ||
"An unexpected error occurred";
this.error(errorMessage, { exit: 1 });
}
}
}