Skip to content

Commit 06a4fb6

Browse files
authored
[TS-CODEGEN] context types generation re-work (#480)
* [TS-CODEGEN] context types generation re-work
1 parent 01b63ec commit 06a4fb6

File tree

10 files changed

+1369
-32
lines changed

10 files changed

+1369
-32
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "minor",
3+
"comment": "Context type generation re-worked",
4+
"packageName": "@graphitation/cli",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "minor",
3+
"comment": "Context type generation re-worked",
4+
"packageName": "@graphitation/ts-codegen",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}

packages/cli/src/supermassive.ts

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,19 @@ import * as glob from "fast-glob";
1010

1111
type GenerateInterfacesOptions = {
1212
outputDir?: string;
13-
contextImport?: string;
14-
contextName?: string;
13+
contextTypePath?: string;
14+
contextTypeName?: string;
1515
enumsImport?: string;
1616
legacy?: boolean;
1717
legacyModels?: boolean;
1818
useStringUnionsInsteadOfEnums?: boolean;
1919
enumMigrationJsonFile?: string;
2020
enumMigrationExceptionsJsonFile?: string;
2121
generateOnlyEnums?: boolean;
22+
contextSubTypeNameTemplate?: string;
23+
contextSubTypePathTemplate?: string;
24+
defaultContextSubTypePath?: string;
25+
defaultContextSubTypeName?: string;
2226
scope?: string;
2327
};
2428

@@ -45,10 +49,26 @@ export function supermassive(): Command {
4549
"output directory relative to file, default generated",
4650
)
4751
.option(
48-
"-ci, --context-import [contextImport]",
52+
"-ci, --context-type-path [contextTypePath]",
4953
"from where to import context",
5054
)
51-
.option("-cn, --context-name [contextName]", "Context name")
55+
.option("-cn, --context-type-name [contextTypeName]", "Context type name")
56+
.option(
57+
"-dcp, --default-context-sub-type-path [defaultContextSubTypePath]",
58+
"From where the default context type will be exported",
59+
)
60+
.option(
61+
"-dcn, --default-context-sub-type-name [defaultContextSubTypeName]",
62+
"Default context type which will extend context sub type",
63+
)
64+
.option(
65+
"-cnt, --context-sub-type-name-template [contextSubTypeNameTemplate]",
66+
"context resource name template. You need to specify ${resourceName} in the parameter eg. `${resourceName}Context`",
67+
)
68+
.option(
69+
"-cpt, --context-sub-type-path-template [contextSubTypePathTemplate]",
70+
"context resource path template. You need to specify ${resourceName} in the parameter eg. `@package/preffix-${resourceName}-suffix`",
71+
)
5272
.option("-ei, --enums-import [enumsImport]", "from where to import enums")
5373
.option("-l, --legacy", "generate legacy types")
5474
.option("--legacy-models", "do not use models for object types")
@@ -91,16 +111,19 @@ function getFiles(inputs: Array<string>) {
91111
.flat()
92112
.filter(Boolean);
93113
}
94-
function getContextPath(outputDir: string, contextImport: string | undefined) {
95-
if (!contextImport) {
114+
function getContextPath(
115+
outputDir: string,
116+
contextTypePath: string | undefined,
117+
) {
118+
if (!contextTypePath) {
96119
return;
97120
}
98121

99-
if (!contextImport.startsWith(".")) {
100-
return contextImport;
122+
if (!contextTypePath.startsWith(".")) {
123+
return contextTypePath;
101124
}
102125

103-
const contextDir = path.join(process.cwd(), contextImport);
126+
const contextDir = path.join(process.cwd(), contextTypePath);
104127

105128
return path
106129
.relative(outputDir, contextDir)
@@ -171,8 +194,16 @@ async function generateInterfaces(
171194
const result = generateTS(document, {
172195
outputPath,
173196
documentPath: fullPath,
174-
contextImport: getContextPath(outputPath, options.contextImport) || null,
175-
contextName: options.contextName,
197+
contextTypePath:
198+
getContextPath(outputPath, options.contextTypePath) || null,
199+
contextTypeName: options.contextTypeName,
200+
contextSubTypeNameTemplate: options.contextSubTypeNameTemplate,
201+
contextSubTypePathTemplate: options.contextSubTypePathTemplate,
202+
defaultContextSubTypePath: getContextPath(
203+
outputPath,
204+
options.defaultContextSubTypePath,
205+
),
206+
defaultContextSubTypeName: options.defaultContextSubTypeName,
176207
enumsImport: getContextPath(outputPath, options.enumsImport) || null,
177208
legacyCompat: !!options.legacy,
178209
legacyNoModelsForObjects: !!options.legacyModels,
@@ -195,6 +226,16 @@ async function generateInterfaces(
195226
),
196227
);
197228

229+
if (result.contextMappingOutput) {
230+
outputs.push(
231+
fs.writeFile(
232+
path.join(outputPath, "schema-context-mapping-metadata.json"),
233+
JSON.stringify(result.contextMappingOutput, null, 2),
234+
{ encoding: "utf-8" },
235+
),
236+
);
237+
}
238+
198239
await Promise.all(outputs);
199240
}
200241
}

0 commit comments

Comments
 (0)