Skip to content

Commit

Permalink
feat(provider-generator): use lazy loading of generated provider name…
Browse files Browse the repository at this point in the history
…spaces
  • Loading branch information
DanielMSchmidt committed Jul 17, 2023
1 parent 304e250 commit e6eca58
Showing 1 changed file with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
ConstructsMakerProviderTarget,
ConstructsMakerTarget,
} from "../constructs-maker";
import path from "path";

interface ProviderData {
name: string;
Expand Down Expand Up @@ -176,6 +177,8 @@ export class TerraformProviderGenerator {
}

this.emitIndexFile(name, files);
this.emitLazyIndexFile(name, files);
this.emitPackageJson(name, files);
}

private emitResourceReadme(resource: ResourceModel): void {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit e6eca58

Please sign in to comment.