Skip to content

Commit dd60d5c

Browse files
committed
feat(schema): add option for generating lite schema
1 parent e919bb0 commit dd60d5c

File tree

20 files changed

+330
-105
lines changed

20 files changed

+330
-105
lines changed

packages/cli/src/actions/generate.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ type Options = {
1313
schema?: string;
1414
output?: string;
1515
silent: boolean;
16+
lite: boolean;
17+
liteOnly: boolean;
1618
};
1719

1820
/**
@@ -88,10 +90,15 @@ async function runPlugins(schemaFile: string, model: Model, outputPath: string,
8890
}
8991
}
9092

91-
const defaultPlugins = [corePlugins['typescript']].reverse();
92-
defaultPlugins.forEach((d) => {
93-
if (!processedPlugins.some((p) => p.cliPlugin === d)) {
94-
processedPlugins.push({ cliPlugin: d, pluginOptions: {} });
93+
const defaultPlugins = [
94+
{
95+
plugin: corePlugins['typescript'],
96+
options: { lite: options.lite, liteOnly: options.liteOnly },
97+
},
98+
];
99+
defaultPlugins.forEach(({ plugin, options }) => {
100+
if (!processedPlugins.some((p) => p.cliPlugin === plugin)) {
101+
processedPlugins.push({ cliPlugin: plugin, pluginOptions: options });
95102
}
96103
});
97104

packages/cli/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ function createProgram() {
5959
.addOption(schemaOption)
6060
.addOption(noVersionCheckOption)
6161
.addOption(new Option('-o, --output <path>', 'default output directory for code generation'))
62+
.addOption(new Option('--lite', 'also generate a lite version of schema without attributes').default(false))
63+
.addOption(new Option('--lite-only', 'only generate lite version of schema without attributes').default(false))
6264
.addOption(new Option('--silent', 'suppress all output except errors').default(false))
6365
.action(generateAction);
6466

packages/cli/src/plugins/typescript.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@ const plugin: CliPlugin = {
77
name: 'TypeScript Schema Generator',
88
statusText: 'Generating TypeScript schema',
99
async generate({ model, defaultOutputPath, pluginOptions }) {
10+
// output path
1011
let outDir = defaultOutputPath;
1112
if (typeof pluginOptions['output'] === 'string') {
1213
outDir = path.resolve(defaultOutputPath, pluginOptions['output']);
1314
if (!fs.existsSync(outDir)) {
1415
fs.mkdirSync(outDir, { recursive: true });
1516
}
1617
}
17-
await new TsSchemaGenerator().generate(model, outDir);
18+
19+
// lite mode
20+
const lite = pluginOptions['lite'] === true;
21+
22+
await new TsSchemaGenerator().generate(model, { outDir, lite });
1823
},
1924
};
2025

packages/clients/tanstack-query/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
"type": "module",
77
"private": true,
88
"scripts": {
9-
"build": "tsc --noEmit && tsup-node",
9+
"build": "tsc --noEmit && tsup-node && pnpm test:generate && pnpm test:typecheck",
1010
"watch": "tsup-node --watch",
1111
"lint": "eslint src --ext ts",
1212
"test": "vitest run",
1313
"pack": "pnpm pack",
14-
"test:generate": "tsx scripts/generate.ts"
14+
"test:generate": "tsx scripts/generate.ts",
15+
"test:typecheck": "tsc --noEmit --project tsconfig.test.json"
1516
},
1617
"keywords": [
1718
"tanstack-query",

packages/clients/tanstack-query/scripts/generate.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ async function main() {
1616

1717
async function generate(schemaPath: string) {
1818
const generator = new TsSchemaGenerator();
19-
const outputDir = path.dirname(schemaPath);
19+
const outDir = path.dirname(schemaPath);
2020
const result = await loadDocument(schemaPath);
2121
if (!result.success) {
2222
throw new Error(`Failed to load schema from ${schemaPath}: ${result.errors}`);
2323
}
24-
await generator.generate(result.model, outputDir);
24+
await generator.generate(result.model, { outDir, liteOnly: true });
2525
}
2626

2727
main();

0 commit comments

Comments
 (0)