forked from Lilly-Protocol/lily-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodegen.ts
More file actions
69 lines (57 loc) · 2.15 KB
/
Copy pathcodegen.ts
File metadata and controls
69 lines (57 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env tsx
/**
* OpenAPI Codegen for Lily SDK
*
* Generates TypeScript types from the backend OpenAPI spec.
* Usage: npx tsx scripts/codegen.ts [spec-path]
*
* If no spec path is provided, uses the vendored spec at openapi/lily-backend.yaml.
* Generated output goes to src/generated/ and is committed to the repo.
*/
import { execSync } from 'node:child_process';
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
import { resolve, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const ROOT = resolve(__dirname, '..');
const DEFAULT_SPEC = resolve(ROOT, 'openapi', 'lily-backend.yaml');
const OUTPUT_DIR = resolve(ROOT, 'src', 'generated');
const OUTPUT_FILE = resolve(OUTPUT_DIR, 'types.ts');
function main() {
const specPath = process.argv[2] ? resolve(process.argv[2]) : DEFAULT_SPEC;
if (!existsSync(specPath)) {
console.error(`Error: OpenAPI spec not found at ${specPath}`);
console.error('Provide a spec path as argument or place it at openapi/lily-backend.yaml');
process.exit(1);
}
if (!existsSync(OUTPUT_DIR)) {
mkdirSync(OUTPUT_DIR, { recursive: true });
}
console.log(`Generating types from: ${specPath}`);
console.log(`Output: ${OUTPUT_FILE}`);
try {
execSync(
`npx openapi-typescript "${specPath}" -o "${OUTPUT_FILE}" --additional-properties false`,
{ cwd: ROOT, stdio: 'inherit' },
);
} catch (error) {
console.error('openapi-typescript failed. Ensure it is installed: npm install -D openapi-typescript');
process.exit(1);
}
// Add header comment to generated file
const generated = readFileSync(OUTPUT_FILE, 'utf-8');
const header = `/**
* AUTO-GENERATED FILE — DO NOT EDIT MANUALLY
*
* Generated by scripts/codegen.ts from OpenAPI spec.
* To regenerate: npm run codegen
*
* Hand-written overrides belong in src/models/ and src/types/contracts.ts.
* This file provides the raw schema types; client contracts add method signatures.
*/
`;
writeFileSync(OUTPUT_FILE, header + generated);
console.log('Codegen complete.');
}
main();