Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion packages/cli/src/actions/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,19 @@ async function runPlugins(schemaFile: string, model: Model, outputPath: string,
}

if (cliPlugin) {
processedPlugins.push({ cliPlugin, pluginOptions: getPluginOptions(plugin) });
const pluginOptions = getPluginOptions(plugin);

// merge CLI options
if (provider === '@core/typescript') {
if (pluginOptions['lite'] === undefined) {
pluginOptions['lite'] = options.lite;
}
if (pluginOptions['liteOnly'] === undefined) {
pluginOptions['liteOnly'] = options.liteOnly;
}
}

processedPlugins.push({ cliPlugin, pluginOptions });
}
}

Expand Down
13 changes: 12 additions & 1 deletion packages/cli/src/plugins/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,18 @@ const plugin: CliPlugin = {
// liteOnly mode
const liteOnly = pluginOptions['liteOnly'] === true;

await new TsSchemaGenerator().generate(model, { outDir, lite, liteOnly });
// add .js extension when importing
const importWithFileExtension = pluginOptions['importWithFileExtension'];
if (importWithFileExtension && typeof importWithFileExtension !== 'string') {
throw new Error('The "importWithFileExtension" option must be a string if specified.');
}

await new TsSchemaGenerator().generate(model, {
outDir,
lite,
liteOnly,
importWithFileExtension: importWithFileExtension as string | undefined,
});
},
};

Expand Down
40 changes: 36 additions & 4 deletions packages/sdk/src/ts-schema-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export type TsSchemaGeneratorOptions = {
outDir: string;
lite?: boolean;
liteOnly?: boolean;
importWithFileExtension?: string;
};

export class TsSchemaGenerator {
Expand Down Expand Up @@ -117,6 +118,7 @@ export class TsSchemaGenerator {
const schemaObject = this.createSchemaObject(model, lite);

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

// generate: import { schema as $schema, type SchemaType as $Schema } from './schema';
statements.push(this.generateSchemaImport(model, true, true, !!(options.lite || options.liteOnly)));
statements.push(
this.generateSchemaImport(
model,
true,
true,
!!(options.lite || options.liteOnly),
options.importWithFileExtension,
),
);

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

private generateSchemaImport(model: Model, schemaObject: boolean, schemaType: boolean, useLite: boolean) {
private generateSchemaImport(
model: Model,
schemaObject: boolean,
schemaType: boolean,
useLite: boolean,
importWithFileExtension: string | undefined,
) {
const importSpecifiers = [];

if (schemaObject) {
Expand All @@ -1442,10 +1458,18 @@ export class TsSchemaGenerator {
);
}

let importFrom = useLite ? './schema-lite' : './schema';
if (importWithFileExtension) {
importFrom += importWithFileExtension.startsWith('.')
? importWithFileExtension
: `.${importWithFileExtension}`;
}

// import { schema as $schema, type SchemaType as $Schema } from './schema';
return ts.factory.createImportDeclaration(
undefined,
ts.factory.createImportClause(false, undefined, ts.factory.createNamedImports(importSpecifiers)),
ts.factory.createStringLiteral(useLite ? './schema-lite' : './schema'),
ts.factory.createStringLiteral(importFrom),
);
}

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

// generate: import { SchemaType as $Schema } from './schema';
statements.push(this.generateSchemaImport(model, false, true, !!(options.lite || options.liteOnly)));
statements.push(
this.generateSchemaImport(
model,
false,
true,
!!(options.lite || options.liteOnly),
options.importWithFileExtension,
),
);

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