feat(sdk): move the generate sdk ts types to nestjs command
This commit is contained in:
@@ -27,7 +27,7 @@
|
||||
"cli:tenants:migrate:rollback": "ts-node -r tsconfig-paths/register src/cli.ts tenants:migrate:rollback",
|
||||
"cli:tenants:migrate:make": "ts-node -r tsconfig-paths/register src/cli.ts tenants:migrate:make",
|
||||
"cli:tenants:list": "ts-node -r tsconfig-paths/register src/cli.ts tenants:list",
|
||||
"openapi:export": "ts-node -r tsconfig-paths/register scripts/export-openapi.ts",
|
||||
"openapi:export": "ts-node -r tsconfig-paths/register src/cli.ts openapi:export",
|
||||
"cli:system:seed:latest": "ts-node -r tsconfig-paths/register src/cli.ts system:seed:latest",
|
||||
"cli:tenants:seed:latest": "ts-node -r tsconfig-paths/register src/cli.ts tenants:seed:latest"
|
||||
},
|
||||
|
||||
@@ -1,46 +0,0 @@
|
||||
/**
|
||||
* Exports the OpenAPI document from the NestJS app to shared/sdk-ts/openapi.json.
|
||||
* Run from packages/server: pnpm run openapi:export
|
||||
*/
|
||||
/// <reference path="../src/common/types/Objection.d.ts" />
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
|
||||
import { ClsMiddleware } from 'nestjs-cls';
|
||||
import * as path from 'path';
|
||||
import * as fs from 'fs';
|
||||
import '@/utils/moment-mysql';
|
||||
import { AppModule } from '@/modules/App/App.module';
|
||||
import { NestExpressApplication } from '@nestjs/platform-express';
|
||||
|
||||
async function exportOpenApi() {
|
||||
global.__public_dirname = path.join(__dirname, '..', 'public');
|
||||
global.__static_dirname = path.join(__dirname, '..', 'static');
|
||||
global.__views_dirname = path.join(global.__static_dirname, '/views');
|
||||
global.__images_dirname = path.join(global.__static_dirname, '/images');
|
||||
|
||||
const app = await NestFactory.create<NestExpressApplication>(AppModule, {
|
||||
rawBody: true,
|
||||
});
|
||||
app.set('query parser', 'extended');
|
||||
app.setGlobalPrefix('/api');
|
||||
app.use(new ClsMiddleware({}).use);
|
||||
|
||||
const config = new DocumentBuilder()
|
||||
.setTitle('Bigcapital')
|
||||
.setDescription('Financial accounting software')
|
||||
.setVersion('1.0')
|
||||
.build();
|
||||
|
||||
const document = SwaggerModule.createDocument(app, config);
|
||||
await app.close();
|
||||
|
||||
const outputPath = path.resolve(__dirname, '../../../shared/sdk-ts/openapi.json');
|
||||
fs.mkdirSync(path.dirname(outputPath), { recursive: true });
|
||||
fs.writeFileSync(outputPath, JSON.stringify(document, null, 2), 'utf-8');
|
||||
console.log(`OpenAPI spec written to ${outputPath}`);
|
||||
}
|
||||
|
||||
exportOpenApi().catch((err) => {
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
});
|
||||
@@ -1,3 +1,4 @@
|
||||
/// <reference path="./common/types/Objection.d.ts" />
|
||||
import { CommandFactory } from 'nest-commander';
|
||||
import { CLIModule } from './modules/CLI/CLI.module';
|
||||
|
||||
|
||||
@@ -123,7 +123,7 @@ import { AppThrottleModule } from './AppThrottle.module';
|
||||
useFactory: () => ({
|
||||
fallbackLanguage: 'en',
|
||||
loaderOptions: {
|
||||
path: join(__dirname, '/../../i18n/'),
|
||||
path: join(__dirname, '../../i18n/'),
|
||||
watch: true,
|
||||
},
|
||||
}),
|
||||
|
||||
@@ -11,6 +11,7 @@ import { TenantsMigrateMakeCommand } from './commands/TenantsMigrateMake.command
|
||||
import { TenantsListCommand } from './commands/TenantsList.command';
|
||||
import { SystemSeedLatestCommand } from './commands/SystemSeedLatest.command';
|
||||
import { TenantsSeedLatestCommand } from './commands/TenantsSeedLatest.command';
|
||||
import { OpenApiExportCommand } from './commands/OpenApiExport.command';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@@ -30,6 +31,7 @@ import { TenantsSeedLatestCommand } from './commands/TenantsSeedLatest.command';
|
||||
TenantsListCommand,
|
||||
SystemSeedLatestCommand,
|
||||
TenantsSeedLatestCommand,
|
||||
OpenApiExportCommand,
|
||||
],
|
||||
})
|
||||
export class CLIModule { }
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
import { Command, CommandRunner } from 'nest-commander';
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
|
||||
import { ClsMiddleware } from 'nestjs-cls';
|
||||
import * as path from 'path';
|
||||
import * as fs from 'fs';
|
||||
import '@/utils/moment-mysql';
|
||||
import { AppModule } from '@/modules/App/App.module';
|
||||
import { NestExpressApplication } from '@nestjs/platform-express';
|
||||
|
||||
@Command({
|
||||
name: 'openapi:export',
|
||||
description: 'Export the OpenAPI document from the NestJS app to shared/sdk-ts/openapi.json',
|
||||
})
|
||||
export class OpenApiExportCommand extends CommandRunner {
|
||||
async run(): Promise<void> {
|
||||
const serverRoot = process.cwd();
|
||||
global.__public_dirname = path.join(serverRoot, 'public');
|
||||
global.__static_dirname = path.join(serverRoot, 'static');
|
||||
global.__views_dirname = path.join(global.__static_dirname, '/views');
|
||||
global.__images_dirname = path.join(global.__static_dirname, '/images');
|
||||
|
||||
const app = await NestFactory.create<NestExpressApplication>(AppModule, {
|
||||
rawBody: true,
|
||||
});
|
||||
app.set('query parser', 'extended');
|
||||
app.setGlobalPrefix('/api');
|
||||
app.use(new ClsMiddleware({}).use);
|
||||
|
||||
const config = new DocumentBuilder()
|
||||
.setTitle('Bigcapital')
|
||||
.setDescription('Financial accounting software')
|
||||
.setVersion('1.0')
|
||||
.build();
|
||||
|
||||
const document = SwaggerModule.createDocument(app, config);
|
||||
await app.close();
|
||||
|
||||
const outputPath = path.resolve(process.cwd(), '../../shared/sdk-ts/openapi.json');
|
||||
fs.mkdirSync(path.dirname(outputPath), { recursive: true });
|
||||
fs.writeFileSync(outputPath, JSON.stringify(document, null, 2), 'utf-8');
|
||||
console.log(`OpenAPI spec written to ${outputPath}`);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user