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" ) ;
59
610program
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)" )
917 . parse ( process . argv ) ;
1018
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+
1128// Function to run a command and handle success or failure
1229function runCommand ( command , successMessage , failureMessage ) {
1330 return new Promise ( ( resolve , reject ) => {
@@ -24,42 +41,44 @@ function runCommand(command, successMessage, failureMessage) {
2441 } ) ;
2542}
2643
27- async function generateAPI ( inputFilePath , outputFilePath ) {
44+ async function generateAPI ( inputFilePath , outputFilePath , config ) {
2845 try {
2946 // Convert YAML schema to JSON schema
3047 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+ ) ;
3253
3354 // 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+ ) ;
3661
3762 // 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 ) ;
4064
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" ) ;
4968
50- console . log ( ' API generation successful' ) ;
69+ console . log ( " API generation successful" ) ;
5170 process . exit ( 0 ) ;
5271 } catch ( error ) {
53- console . error ( ' API generation failed' ) ;
72+ console . error ( " API generation failed" , error ) ;
5473 process . exit ( 1 ) ;
5574 }
5675}
5776
58- const { input, output } = program . opts ( ) ;
77+ const { input, output, config } = program . opts ( ) ;
5978
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" ) ;
6281 process . exit ( 1 ) ;
6382}
6483
65- generateAPI ( input , output ) ;
84+ generateAPI ( input , output , config ) ;
0 commit comments