1
- const { program } = require ( 'commander' ) ;
2
- const { exec } = require ( 'child_process' ) ;
3
- const { renameSync, rmdirSync, unlinkSync } = require ( 'fs' ) ;
4
- const { dirname, basename, join } = require ( 'path' ) ;
1
+ #! /usr/bin/env node
2
+ var { program } = require ( "commander" ) ;
3
+ var { exec } = require ( "child_process" ) ;
4
+ var { renameSync, rmdirSync, unlinkSync } = require ( "fs" ) ;
5
+ var { dirname, basename, join } = require ( "path" ) ;
6
+ var fs = require ( "fs" ) ;
7
+ var ts = require ( "typescript" ) ;
8
+ var compileConfig = require ( "./apiCompilerConfig.json" ) ;
5
9
6
10
program
7
- . option ( '-i, --input <input>' , 'Input YAML schema file path' )
8
- . option ( '-o, --output <output>' , 'Output path including the filename (e.g., /output/api.js)' )
11
+ . option ( "-i, --input <input>" , "Input YAML schema file path" )
12
+ . option (
13
+ "-o, --output <output>" ,
14
+ "Output path including the filename (e.g., /output/api.js)"
15
+ )
16
+ . option ( "-c, --config <config>" , "Config file path (e.g., /config.json)" )
9
17
. parse ( process . argv ) ;
10
18
19
+ const compileApi = ( tsPath , outputPath ) => {
20
+ // read ts file
21
+ const tsFile = fs . readFileSync ( tsPath , "utf8" ) ;
22
+ const result = ts . transpile ( tsFile , compileConfig ) ;
23
+ // write the result to output file
24
+ fs . writeFileSync ( outputPath , result ) ;
25
+ console . log ( "wrote output to " + outputPath ) ;
26
+ } ;
27
+
11
28
// Function to run a command and handle success or failure
12
29
function runCommand ( command , successMessage , failureMessage ) {
13
30
return new Promise ( ( resolve , reject ) => {
@@ -24,42 +41,44 @@ function runCommand(command, successMessage, failureMessage) {
24
41
} ) ;
25
42
}
26
43
27
- async function generateAPI ( inputFilePath , outputFilePath ) {
44
+ async function generateAPI ( inputFilePath , outputFilePath , config ) {
28
45
try {
29
46
// Convert YAML schema to JSON schema
30
47
const yamlToJSONCommand = `redocly bundle ${ inputFilePath } -o ./openapi.json --ext json` ;
31
- await runCommand ( yamlToJSONCommand , 'JSON bundle generation successful' , 'JSON bundle generation failed' ) ;
48
+ await runCommand (
49
+ yamlToJSONCommand ,
50
+ "JSON bundle generation successful" ,
51
+ "JSON bundle generation failed"
52
+ ) ;
32
53
33
54
// Run OpenAPI generator
34
- const openApiGeneratorCommand = 'npx @rtk-query/codegen-openapi openapi-config.ts' ;
35
- await runCommand ( openApiGeneratorCommand , 'OpenAPI generation successful' , 'OpenAPI generation failed' ) ;
55
+ const openApiGeneratorCommand = `npx @rtk-query/codegen-openapi ${ config } ` ;
56
+ await runCommand (
57
+ openApiGeneratorCommand ,
58
+ "OpenAPI generation successful" ,
59
+ "OpenAPI generation failed"
60
+ ) ;
36
61
37
62
// Run TypeScript compilation
38
- const tscCommand = 'tsc --build tsconfig.json' ;
39
- await runCommand ( tscCommand , 'TypeScript compilation successful' , 'TypeScript compilation failed' ) ;
63
+ compileApi ( "./api.ts" , outputFilePath ) ;
40
64
41
- // Move api.js from the generated folder to the output path
42
- console . log ( 'Removing Build Artifacts' ) ;
43
- const outputPath = dirname ( outputFilePath ) ;
44
- const outputFilename = basename ( outputFilePath ) ;
45
- renameSync ( './dist/api.js' , join ( outputPath , outputFilename ) ) ;
46
- rmdirSync ( './dist' , { recursive : true } ) ;
47
- unlinkSync ( './openapi.json' ) ;
48
- unlinkSync ( './api.ts' ) ;
65
+ console . log ( "Removing Build Artifacts" ) ;
66
+ unlinkSync ( "./openapi.json" ) ;
67
+ unlinkSync ( "./api.ts" ) ;
49
68
50
- console . log ( ' API generation successful' ) ;
69
+ console . log ( " API generation successful" ) ;
51
70
process . exit ( 0 ) ;
52
71
} catch ( error ) {
53
- console . error ( ' API generation failed' ) ;
72
+ console . error ( " API generation failed" , error ) ;
54
73
process . exit ( 1 ) ;
55
74
}
56
75
}
57
76
58
- const { input, output } = program . opts ( ) ;
77
+ const { input, output, config } = program . opts ( ) ;
59
78
60
- if ( ! input || ! output ) {
61
- console . error ( ' Please provide both input and output options.' ) ;
79
+ if ( ! input || ! output || ! config ) {
80
+ console . error ( " Please provide input, output, and config paths" ) ;
62
81
process . exit ( 1 ) ;
63
82
}
64
83
65
- generateAPI ( input , output ) ;
84
+ generateAPI ( input , output , config ) ;
0 commit comments