Skip to content

Commit 8d9778b

Browse files
committed
fix --lite-only option and add CLI test
1 parent dd60d5c commit 8d9778b

File tree

4 files changed

+51
-4
lines changed

4 files changed

+51
-4
lines changed

packages/cli/src/plugins/typescript.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ const plugin: CliPlugin = {
1919
// lite mode
2020
const lite = pluginOptions['lite'] === true;
2121

22-
await new TsSchemaGenerator().generate(model, { outDir, lite });
22+
// liteOnly mode
23+
const liteOnly = pluginOptions['liteOnly'] === true;
24+
25+
await new TsSchemaGenerator().generate(model, { outDir, lite, liteOnly });
2326
},
2427
};
2528

packages/cli/test/generate.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,18 @@ describe('CLI generate command test', () => {
4444
runCli('generate', workDir);
4545
expect(fs.existsSync(path.join(workDir, 'bar/schema.ts'))).toBe(true);
4646
});
47+
48+
it('should respect lite option', () => {
49+
const workDir = createProject(model);
50+
runCli('generate --lite', workDir);
51+
expect(fs.existsSync(path.join(workDir, 'zenstack/schema.ts'))).toBe(true);
52+
expect(fs.existsSync(path.join(workDir, 'zenstack/schema-lite.ts'))).toBe(true);
53+
});
54+
55+
it('should respect liteOnly option', () => {
56+
const workDir = createProject(model);
57+
runCli('generate --lite-only', workDir);
58+
expect(fs.existsSync(path.join(workDir, 'zenstack/schema.ts'))).toBe(false);
59+
expect(fs.existsSync(path.join(workDir, 'zenstack/schema-lite.ts'))).toBe(true);
60+
});
4761
});

packages/cli/test/ts-schema-gen.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,4 +420,26 @@ model User {
420420
},
421421
});
422422
});
423+
424+
it('supports lite schema generation', async () => {
425+
const { schemaLite } = await generateTsSchema(
426+
`
427+
model User {
428+
id String @id @default(uuid())
429+
name String
430+
email String @unique
431+
432+
@@map('users')
433+
}
434+
`,
435+
undefined,
436+
undefined,
437+
undefined,
438+
true,
439+
);
440+
441+
expect(schemaLite!.models.User.attributes).toBeUndefined();
442+
expect(schemaLite!.models.User.fields.id.attributes).toBeUndefined();
443+
expect(schemaLite!.models.User.fields.email.attributes).toBeUndefined();
444+
});
423445
});

packages/testtools/src/schema.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { invariant } from '@zenstackhq/common-helpers';
2-
import { TsSchemaGenerator } from '@zenstackhq/sdk';
32
import type { SchemaDef } from '@zenstackhq/schema';
3+
import { TsSchemaGenerator } from '@zenstackhq/sdk';
44
import { execSync } from 'node:child_process';
55
import crypto from 'node:crypto';
66
import fs from 'node:fs';
@@ -37,6 +37,7 @@ export async function generateTsSchema(
3737
provider: 'sqlite' | 'postgresql' = 'sqlite',
3838
dbUrl?: string,
3939
extraSourceFiles?: Record<string, string>,
40+
withLiteSchema?: boolean,
4041
) {
4142
const workDir = createTestProject();
4243

@@ -50,7 +51,7 @@ export async function generateTsSchema(
5051
}
5152

5253
const generator = new TsSchemaGenerator();
53-
await generator.generate(result.model, { outDir: workDir });
54+
await generator.generate(result.model, { outDir: workDir, lite: withLiteSchema });
5455

5556
if (extraSourceFiles) {
5657
for (const [fileName, content] of Object.entries(extraSourceFiles)) {
@@ -72,7 +73,14 @@ async function compileAndLoad(workDir: string) {
7273

7374
// load the schema module
7475
const module = await import(path.join(workDir, 'schema.js'));
75-
return { workDir, schema: module.schema as SchemaDef };
76+
77+
let moduleLite: any;
78+
try {
79+
moduleLite = await import(path.join(workDir, 'schema-lite.js'));
80+
} catch {
81+
// ignore
82+
}
83+
return { workDir, schema: module.schema as SchemaDef, schemaLite: moduleLite?.schema as SchemaDef | undefined };
7684
}
7785

7886
export function generateTsSchemaFromFile(filePath: string) {

0 commit comments

Comments
 (0)