import { promises , readFileSync , writeFileSync } from 'fs' ; import { emitConstructForApiObject , findApiObjectDefinitions } from './codegen-constructs' ; import { CodeMaker } from 'codemaker' ; import { JSONSchema4 } from 'json-schema' ; import * as path from "path" ; export interface Options { readonly apiVersion : string ; } export async function generateAllApiObjects ( outdir , inputFile ) { const version = '2.52.0' ; const code = new CodeMaker ( ) ; code . indentation = 2 ; const schema = await downloadSchema ( inputFile ) ; const topLevelObjects = findApiObjectDefinitions ( schema ) ; for ( const provider of Object . keys ( topLevelObjects ) ) { const files = [ ] ; for ( const type of Object . keys ( topLevelObjects [ provider ] ) ) { for ( const o of topLevelObjects [ provider ] [ type ] ) { files . push ( emitConstructForApiObject ( code , o , provider , type , version ) ) ; } } writeIndex ( code , files , provider ) ; } await promises . mkdir ( outdir , { recursive : true } ) ; await code . save ( outdir ) ; for ( const provider of Object . keys ( topLevelObjects ) ) { writePackageJson ( path . join ( outdir , provider ) , provider ) ; } } async function downloadSchema ( inputFile ) { const output = readFileSync ( path . join ( process . cwd ( ) , inputFile ) ) ; return JSON . parse ( output . toString ( ) ) as JSONSchema4 ; } async function writeIndex ( code , files , filePath ) { const sourceFile = path . join ( filePath , 'index.ts' ) ; code . openFile ( sourceFile ) ; code . line ( `// generated by terrastack.io` ) ; code . line ( ) ; files . forEach ( file => { code . line ( `export * from './${ file . replace ( `${ filePath }/` , '' ) }';` ) ; } ) ; code . line ( ) ; code . closeFile ( sourceFile ) ; } async function writePackageJson ( workdir , provider ) { const main = 'index' ; const pkg = { name : `@terrastack/${ provider }-provider` , version : '0.0.0' , author : "sebastian@korfmann.net" , main : `${ main }.js` , types : `${ main }.d.ts` , license : 'Apache-2.0' , scripts : { build : "jsii" , watch : "jsii -w" , package : "jsii-pacmak" , } , repository : { url : 'https://github.com/terrastackio/terrastack' , type : 'git' } , jsii : { outdir : "dist" , targets : { python : { distName : `terrastack_${ provider }_provider` , module : `terrastack_${ provider }_provider` } } } , dependencies : { "@aws-cdk/core" : "^1.27.0" , "@terrastack/core" : "^0.15.0" , } , devDependencies : { "@types/node" : "^13.7.7" , jsii : "^0.22.0" , "jsii-pacmak" : "^0.22.0" } , peerDependencies : { "@aws-cdk/core" : "^1.27.0" , "@terrastack/core" : "^0.15.0" , } } ; writeFileSync ( path . join ( workdir , 'package.json' ) , JSON . stringify ( pkg , undefined , 2 ) ) ; }