diff --git a/packages/@cdktf/provider-generator/lib/get/generator/provider-generator.ts b/packages/@cdktf/provider-generator/lib/get/generator/provider-generator.ts index 8d63b27c9f..085563cdc2 100644 --- a/packages/@cdktf/provider-generator/lib/get/generator/provider-generator.ts +++ b/packages/@cdktf/provider-generator/lib/get/generator/provider-generator.ts @@ -15,6 +15,7 @@ import { ConstructsMakerProviderTarget, ConstructsMakerTarget, } from "../constructs-maker"; +import path from "path"; interface ProviderData { name: string; @@ -176,6 +177,8 @@ export class TerraformProviderGenerator { } this.emitIndexFile(name, files); + this.emitLazyIndexFile(name, files); + this.emitPackageJson(name, files); } private emitResourceReadme(resource: ResourceModel): void { @@ -207,6 +210,65 @@ export class TerraformProviderGenerator { this.code.closeFile(filePath); } + private emitLazyIndexFile(provider: ProviderName, files: string[]): void { + const folder = `providers/${provider}`; + const filePath = `${folder}/lazy-index.ts`; + this.code.openFile(filePath); + this.code.line("// generated by cdktf get"); + for (const file of files) { + const dirName = file.replace(`${folder}/`, "").replace("/index.ts", ""); + this.code.line( + `Object.defineProperty(exports, '${toCamelCase( + dirName + )}', { get: function () { return require('${dirName}'); } });` + ); + } + this.code.line(); + this.code.closeFile(filePath); + } + + private emitPackageJson(provider: ProviderName, files: string[]): void { + const folder = `providers/${provider}`; + const filePath = `${folder}/package.json`; + this.code.openFile(filePath); + const toDirName = (file: string) => + file.replace(`${folder}/`, "").replace("/index.ts", ""); + + const packageJsonExports = files.reduce( + (acc, file) => ({ + ...acc, + [`./${toDirName(file)}`]: `./${path.join( + ".", + toDirName(file), + "index.js" + )}`, + }), + {} + ); + this.code.line( + JSON.stringify( + { + name: provider, + version: "0.0.0", + description: `Terraform CDK Provider for ${provider}`, + main: "index.js", + types: "index.d.ts", + exports: { + ".": { + types: "./index.d.ts", + import: "./index.js", + require: "./lazy-index.js", + }, + ...packageJsonExports, + }, + }, + null, + 2 + ) + ); + this.code.closeFile(filePath); + } + private emitResource(resource: ResourceModel): string { this.code.openFile(resource.filePath); this.emitFileHeader(resource);