@apistudio/apim-cli
Version:
CLI for API Management Products
133 lines (101 loc) • 3.38 kB
Markdown
This directory contains utility scripts for managing the inventory package.
Automatically syncs exports from `api-model-kinds_generated.ts` to `api-model-kinds-exports.ts`.
When the `api-model-kinds_generated.ts` file is regenerated (e.g., from OpenAPI specs), this script automatically:
1. Extracts all schema names from the generated file
2. Creates individual type exports for each schema
3. Creates a convenience `Schemas` object with all types
4. Generates a clean, organized exports file
### Usage
**Run directly:**
```bash
node packages/inventory/src/resources/scripts/sync-exports.js
```
**From the inventory package directory:**
```bash
cd packages/inventory
node src/resources/scripts/sync-exports.js
```
**Add to package.json scripts:**
```json
{
"scripts": {
"sync-exports": "node src/resources/scripts/sync-exports.js"
}
}
```
Then run:
```bash
npm run sync-exports
```
The script:
- ✅ Reads `src/api-model-kinds_generated.ts`
- ✅ Extracts all schema names from the `components.schemas` section
- ✅ Generates type exports: `export type SchemaName = components['schemas']['SchemaName'];`
- ✅ Creates a `Schemas` convenience object for runtime access
- ✅ Writes to `src/api-model-kinds-exports.ts` with auto-generation notice
- ✅ Sorts exports alphabetically for consistency
```typescript
/**
* Auto-generated exports from api-model-kinds_generated.ts
*
* This file is automatically generated by sync-exports.js
* Do not edit manually - run 'npm run sync-exports' to regenerate
*
* Generated on: 2025-11-03T11:51:27.766Z
*/
import { components } from './api-model-kinds_generated.js';
// Export individual schema types
export type Api = components['schemas']['Api'];
export type CORS = components['schemas']['CORS'];
// ... more exports
// Export a convenience object that contains all schemas
export const Schemas = {
Api: {} as Api,
CORS: {} as CORS,
// ... more schemas
};
```
Run this script whenever:
- `api-model-kinds_generated.ts` is regenerated
- New schemas are added to the OpenAPI specification
- Schema names change in the generated file
- You want to ensure exports are in sync
- 🚀 **Automation**: No manual export management needed
- 🔄 **Consistency**: Always in sync with generated types
- 📝 **Documentation**: Clear auto-generation notice
- 🎯 **Type Safety**: Maintains full TypeScript type information
- 🧹 **Clean**: Organized, sorted, and formatted output
- ♻️ **Reusable**: Can be run anytime schemas change
This script can be integrated into your build pipeline:
```json
{
"scripts": {
"generate": "openapi-typescript schema.yaml -o src/api-model-kinds_generated.ts",
"sync-exports": "node src/resources/scripts/sync-exports.js",
"build": "npm run generate && npm run sync-exports && tsc"
}
}
```
```
🔄 Starting export sync...
📖 Reading schemas from: /path/to/api-model-kinds_generated.ts
✨ Found 52 schemas:
1. AWSLambda
2. Antivirus
3. Api
...
52. test
📝 Generating exports file...
💾 Writing to: /path/to/api-model-kinds-exports.ts
✅ Successfully wrote exports to: /path/to/api-model-kinds-exports.ts
✅ Export sync completed successfully!
// Made with Bob