Skip to content

Commit 160437a

Browse files
authored
feat(cli): allow importing with file extension in generated schema (#412)
fixes #378
1 parent 70b970f commit 160437a

File tree

3 files changed

+61
-6
lines changed

3 files changed

+61
-6
lines changed

packages/cli/src/actions/generate.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,19 @@ async function runPlugins(schemaFile: string, model: Model, outputPath: string,
8686
}
8787

8888
if (cliPlugin) {
89-
processedPlugins.push({ cliPlugin, pluginOptions: getPluginOptions(plugin) });
89+
const pluginOptions = getPluginOptions(plugin);
90+
91+
// merge CLI options
92+
if (provider === '@core/typescript') {
93+
if (pluginOptions['lite'] === undefined) {
94+
pluginOptions['lite'] = options.lite;
95+
}
96+
if (pluginOptions['liteOnly'] === undefined) {
97+
pluginOptions['liteOnly'] = options.liteOnly;
98+
}
99+
}
100+
101+
processedPlugins.push({ cliPlugin, pluginOptions });
90102
}
91103
}
92104

packages/cli/src/plugins/typescript.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,18 @@ const plugin: CliPlugin = {
2222
// liteOnly mode
2323
const liteOnly = pluginOptions['liteOnly'] === true;
2424

25-
await new TsSchemaGenerator().generate(model, { outDir, lite, liteOnly });
25+
// add .js extension when importing
26+
const importWithFileExtension = pluginOptions['importWithFileExtension'];
27+
if (importWithFileExtension && typeof importWithFileExtension !== 'string') {
28+
throw new Error('The "importWithFileExtension" option must be a string if specified.');
29+
}
30+
31+
await new TsSchemaGenerator().generate(model, {
32+
outDir,
33+
lite,
34+
liteOnly,
35+
importWithFileExtension: importWithFileExtension as string | undefined,
36+
});
2637
},
2738
};
2839

packages/sdk/src/ts-schema-generator.ts

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export type TsSchemaGeneratorOptions = {
5555
outDir: string;
5656
lite?: boolean;
5757
liteOnly?: boolean;
58+
importWithFileExtension?: string;
5859
};
5960

6061
export class TsSchemaGenerator {
@@ -117,6 +118,7 @@ export class TsSchemaGenerator {
117118
const schemaObject = this.createSchemaObject(model, lite);
118119

119120
// Now generate the import declaration with the correct imports
121+
// import { type SchemaDef, type OperandExpression, ExpressionUtils } from '@zenstackhq/orm/schema';
120122
const runtimeImportDecl = ts.factory.createImportDeclaration(
121123
undefined,
122124
ts.factory.createImportClause(
@@ -1290,7 +1292,15 @@ export class TsSchemaGenerator {
12901292
const statements: ts.Statement[] = [];
12911293

12921294
// generate: import { schema as $schema, type SchemaType as $Schema } from './schema';
1293-
statements.push(this.generateSchemaImport(model, true, true, !!(options.lite || options.liteOnly)));
1295+
statements.push(
1296+
this.generateSchemaImport(
1297+
model,
1298+
true,
1299+
true,
1300+
!!(options.lite || options.liteOnly),
1301+
options.importWithFileExtension,
1302+
),
1303+
);
12941304

12951305
// generate: import type { ModelResult as $ModelResult } from '@zenstackhq/orm';
12961306
statements.push(
@@ -1416,7 +1426,13 @@ export class TsSchemaGenerator {
14161426
fs.writeFileSync(outputFile, result);
14171427
}
14181428

1419-
private generateSchemaImport(model: Model, schemaObject: boolean, schemaType: boolean, useLite: boolean) {
1429+
private generateSchemaImport(
1430+
model: Model,
1431+
schemaObject: boolean,
1432+
schemaType: boolean,
1433+
useLite: boolean,
1434+
importWithFileExtension: string | undefined,
1435+
) {
14201436
const importSpecifiers = [];
14211437

14221438
if (schemaObject) {
@@ -1442,10 +1458,18 @@ export class TsSchemaGenerator {
14421458
);
14431459
}
14441460

1461+
let importFrom = useLite ? './schema-lite' : './schema';
1462+
if (importWithFileExtension) {
1463+
importFrom += importWithFileExtension.startsWith('.')
1464+
? importWithFileExtension
1465+
: `.${importWithFileExtension}`;
1466+
}
1467+
1468+
// import { schema as $schema, type SchemaType as $Schema } from './schema';
14451469
return ts.factory.createImportDeclaration(
14461470
undefined,
14471471
ts.factory.createImportClause(false, undefined, ts.factory.createNamedImports(importSpecifiers)),
1448-
ts.factory.createStringLiteral(useLite ? './schema-lite' : './schema'),
1472+
ts.factory.createStringLiteral(importFrom),
14491473
);
14501474
}
14511475

@@ -1466,7 +1490,15 @@ export class TsSchemaGenerator {
14661490
const statements: ts.Statement[] = [];
14671491

14681492
// generate: import { SchemaType as $Schema } from './schema';
1469-
statements.push(this.generateSchemaImport(model, false, true, !!(options.lite || options.liteOnly)));
1493+
statements.push(
1494+
this.generateSchemaImport(
1495+
model,
1496+
false,
1497+
true,
1498+
!!(options.lite || options.liteOnly),
1499+
options.importWithFileExtension,
1500+
),
1501+
);
14701502

14711503
// generate: import { CreateArgs as $CreateArgs, ... } from '@zenstackhq/orm';
14721504
const inputTypes = [

0 commit comments

Comments
 (0)