@@ -5,6 +5,11 @@ import {createGenerator, SchemaGenerator} from "ts-json-schema-generator";
5
5
import { camelCase , find , get , isArray , isEmpty , kebabCase , mergeWith , set , upperFirst } from "lodash" ;
6
6
import { ApiGatewayEvent } from "serverless/plugins/aws/package/compile/events/apiGateway/lib/validate" ;
7
7
import { mapKeysDeep , mapValuesDeep } from 'deepdash/standalone'
8
+ import path from "path" ;
9
+ import { promisify } from "util" ;
10
+ import { exec } from 'child_process' ;
11
+
12
+ const execAsync = promisify ( exec ) ;
8
13
9
14
interface Options {
10
15
typescriptApiPath ?: string ;
@@ -208,14 +213,29 @@ export default class ServerlessOpenapiTypeScript {
208
213
}
209
214
}
210
215
211
- postProcessOpenApi ( ) {
216
+ async postProcessOpenApi ( ) {
212
217
// @ts -ignore
213
218
const outputFile = this . serverless . processedInput . options . output || 'openapi.json' ;
214
219
const openApi = yaml . load ( fs . readFileSync ( outputFile ) ) ;
215
220
this . patchOpenApiVersion ( openApi ) ;
216
221
this . enrichMethodsInfo ( openApi ) ;
217
222
const encodedOpenAPI = this . encodeOpenApiToStandard ( openApi ) ;
218
223
fs . writeFileSync ( outputFile , outputFile . endsWith ( 'json' ) ? JSON . stringify ( encodedOpenAPI , null , 2 ) : yaml . dump ( encodedOpenAPI ) ) ;
224
+
225
+ const s3Bucket = this . serverless . service . custom . documentation ?. s3Bucket ;
226
+ if ( s3Bucket ) {
227
+ await this . uploadFileToS3UsingCLI ( outputFile , s3Bucket ) ;
228
+ }
229
+ }
230
+
231
+ async uploadFileToS3UsingCLI ( filePath : string , bucketName : string ) {
232
+ const s3Path = `s3://${ bucketName } /${ path . basename ( filePath ) } ` ;
233
+ try {
234
+ await execAsync ( `aws s3 cp "${ filePath } " "${ s3Path } "` ) ;
235
+ this . log ( `File uploaded successfully to ${ s3Path } ` ) ;
236
+ } catch ( error ) {
237
+ this . log ( `Error uploading file: ${ error . message } ` ) ;
238
+ }
219
239
}
220
240
221
241
// OpenApi spec define ^[a-zA-Z0-9\.\-_]+$ for legal fields - https://spec.openapis.org/oas/v3.1.0#components-object
@@ -292,8 +312,8 @@ export default class ServerlessOpenapiTypeScript {
292
312
if ( typeof schema === "object" && schema !== null ) {
293
313
const newSchema = JSON . parse ( JSON . stringify ( schema ) ) ;
294
314
if ( newSchema . hasOwnProperty ( "const" ) ) {
295
- const { const : _ , ...rest } = newSchema ;
296
- return { ...rest , enum : [ newSchema . const ] } ;
315
+ const { const : _ , ...rest } = newSchema ;
316
+ return { ...rest , enum : [ newSchema . const ] } ;
297
317
}
298
318
299
319
for ( const key of Object . keys ( newSchema ) ) {
0 commit comments