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
15 changes: 11 additions & 4 deletions packages/cli/src/actions/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ type Options = {
schema?: string;
output?: string;
silent: boolean;
lite: boolean;
liteOnly: boolean;
};

/**
Expand Down Expand Up @@ -88,10 +90,15 @@ async function runPlugins(schemaFile: string, model: Model, outputPath: string,
}
}

const defaultPlugins = [corePlugins['typescript']].reverse();
defaultPlugins.forEach((d) => {
if (!processedPlugins.some((p) => p.cliPlugin === d)) {
processedPlugins.push({ cliPlugin: d, pluginOptions: {} });
const defaultPlugins = [
{
plugin: corePlugins['typescript'],
options: { lite: options.lite, liteOnly: options.liteOnly },
},
];
defaultPlugins.forEach(({ plugin, options }) => {
if (!processedPlugins.some((p) => p.cliPlugin === plugin)) {
processedPlugins.push({ cliPlugin: plugin, pluginOptions: options });
}
});

Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ function createProgram() {
.addOption(schemaOption)
.addOption(noVersionCheckOption)
.addOption(new Option('-o, --output <path>', 'default output directory for code generation'))
.addOption(new Option('--lite', 'also generate a lite version of schema without attributes').default(false))
.addOption(new Option('--lite-only', 'only generate lite version of schema without attributes').default(false))
.addOption(new Option('--silent', 'suppress all output except errors').default(false))
.action(generateAction);

Expand Down
10 changes: 9 additions & 1 deletion packages/cli/src/plugins/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,22 @@ const plugin: CliPlugin = {
name: 'TypeScript Schema Generator',
statusText: 'Generating TypeScript schema',
async generate({ model, defaultOutputPath, pluginOptions }) {
// output path
let outDir = defaultOutputPath;
if (typeof pluginOptions['output'] === 'string') {
outDir = path.resolve(defaultOutputPath, pluginOptions['output']);
if (!fs.existsSync(outDir)) {
fs.mkdirSync(outDir, { recursive: true });
}
}
await new TsSchemaGenerator().generate(model, outDir);

// lite mode
const lite = pluginOptions['lite'] === true;

// liteOnly mode
const liteOnly = pluginOptions['liteOnly'] === true;

await new TsSchemaGenerator().generate(model, { outDir, lite, liteOnly });
},
};

Expand Down
14 changes: 14 additions & 0 deletions packages/cli/test/generate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,18 @@ describe('CLI generate command test', () => {
runCli('generate', workDir);
expect(fs.existsSync(path.join(workDir, 'bar/schema.ts'))).toBe(true);
});

it('should respect lite option', () => {
const workDir = createProject(model);
runCli('generate --lite', workDir);
expect(fs.existsSync(path.join(workDir, 'zenstack/schema.ts'))).toBe(true);
expect(fs.existsSync(path.join(workDir, 'zenstack/schema-lite.ts'))).toBe(true);
});

it('should respect liteOnly option', () => {
const workDir = createProject(model);
runCli('generate --lite-only', workDir);
expect(fs.existsSync(path.join(workDir, 'zenstack/schema.ts'))).toBe(false);
expect(fs.existsSync(path.join(workDir, 'zenstack/schema-lite.ts'))).toBe(true);
});
});
22 changes: 22 additions & 0 deletions packages/cli/test/ts-schema-gen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,4 +420,26 @@ model User {
},
});
});

it('supports lite schema generation', async () => {
const { schemaLite } = await generateTsSchema(
`
model User {
id String @id @default(uuid())
name String
email String @unique

@@map('users')
}
`,
undefined,
undefined,
undefined,
true,
);

expect(schemaLite!.models.User.attributes).toBeUndefined();
expect(schemaLite!.models.User.fields.id.attributes).toBeUndefined();
expect(schemaLite!.models.User.fields.email.attributes).toBeUndefined();
});
});
5 changes: 3 additions & 2 deletions packages/clients/tanstack-query/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
"type": "module",
"private": true,
"scripts": {
"build": "tsc --noEmit && tsup-node",
"build": "tsc --noEmit && tsup-node && pnpm test:generate && pnpm test:typecheck",
"watch": "tsup-node --watch",
"lint": "eslint src --ext ts",
"test": "vitest run",
"pack": "pnpm pack",
"test:generate": "tsx scripts/generate.ts"
"test:generate": "tsx scripts/generate.ts",
"test:typecheck": "tsc --noEmit --project tsconfig.test.json"
},
"keywords": [
"tanstack-query",
Expand Down
4 changes: 2 additions & 2 deletions packages/clients/tanstack-query/scripts/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ async function main() {

async function generate(schemaPath: string) {
const generator = new TsSchemaGenerator();
const outputDir = path.dirname(schemaPath);
const outDir = path.dirname(schemaPath);
const result = await loadDocument(schemaPath);
if (!result.success) {
throw new Error(`Failed to load schema from ${schemaPath}: ${result.errors}`);
}
await generator.generate(result.model, outputDir);
await generator.generate(result.model, { outDir, liteOnly: true });
}

main();
Loading