From e685b5a277d4cbb189ce51ffe97cb81a35f69b2f Mon Sep 17 00:00:00 2001 From: Daniel Schmidt Date: Mon, 26 Jul 2021 18:42:08 +0200 Subject: [PATCH 1/3] fix(hcl2cdk): only run get conditionally --- packages/@cdktf/hcl2cdk/lib/index.ts | 2 +- packages/@cdktf/hcl2cdk/test/convertProject.test.ts | 6 +++++- packages/cdktf-cli/bin/cmds/helper/init.ts | 11 +++++++++-- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/packages/@cdktf/hcl2cdk/lib/index.ts b/packages/@cdktf/hcl2cdk/lib/index.ts index 487af27e19..a231304ee4 100644 --- a/packages/@cdktf/hcl2cdk/lib/index.ts +++ b/packages/@cdktf/hcl2cdk/lib/index.ts @@ -332,7 +332,7 @@ export async function convertProject( return { code: prettier.format(outputMainFile, { parser: "babel" }), - cdktfJson: prettier.format(JSON.stringify(cdktfJson), { parser: "json" }), + cdktfJson, stats, }; } diff --git a/packages/@cdktf/hcl2cdk/test/convertProject.test.ts b/packages/@cdktf/hcl2cdk/test/convertProject.test.ts index 321acc84a8..eeabab1eec 100644 --- a/packages/@cdktf/hcl2cdk/test/convertProject.test.ts +++ b/packages/@cdktf/hcl2cdk/test/convertProject.test.ts @@ -206,7 +206,11 @@ describe("convertProject", () => { ); fs.writeFileSync(path.resolve(targetPath, "main.ts"), code, "utf8"); - fs.writeFileSync(path.resolve(targetPath, "cdktf.json"), cdktfJson, "utf8"); + fs.writeFileSync( + path.resolve(targetPath, "cdktf.json"), + JSON.stringify(cdktfJson), + "utf8" + ); const currentPlan = getCdkPlan(targetPath); diff --git a/packages/cdktf-cli/bin/cmds/helper/init.ts b/packages/cdktf-cli/bin/cmds/helper/init.ts index a1fc33b14a..98f5aeade1 100644 --- a/packages/cdktf-cli/bin/cmds/helper/init.ts +++ b/packages/cdktf-cli/bin/cmds/helper/init.ts @@ -141,15 +141,22 @@ This means that your Terraform state file will be stored locally on disk in a fi fs.writeFileSync(path.resolve(destination, "main.ts"), code, "utf8"); fs.writeFileSync( path.resolve(destination, "cdktf.json"), - cdktfJson, + JSON.stringify(cdktfJson, null, 2), "utf8" ); + const { terraformModules, terraformProviders } = cdktfJson as { + terraformModules: string[]; + terraformProviders: string[]; + }; + if (terraformModules.length + terraformProviders.length > 0) { + execSync("npm run get", { cwd: destination }); + } + telemetryData.conversionStats = stats; } catch (err) { throw Errors.Internal("init", err, { fromTerraformProject: true }); } - execSync("npm run get", { cwd: destination }); } else { console.error( `The --from-terraform-project flag is only support with the typescript template. The command will continue and ignore the flag.` From c5c09b0343f6b3558a60e46dde1c6c0217139c3f Mon Sep 17 00:00:00 2001 From: Daniel Schmidt Date: Thu, 29 Jul 2021 15:14:05 +0200 Subject: [PATCH 2/3] fix(cli): fix typo --- packages/cdktf-cli/bin/cmds/helper/init.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cdktf-cli/bin/cmds/helper/init.ts b/packages/cdktf-cli/bin/cmds/helper/init.ts index 98f5aeade1..63343353b6 100644 --- a/packages/cdktf-cli/bin/cmds/helper/init.ts +++ b/packages/cdktf-cli/bin/cmds/helper/init.ts @@ -159,7 +159,7 @@ This means that your Terraform state file will be stored locally on disk in a fi } } else { console.error( - `The --from-terraform-project flag is only support with the typescript template. The command will continue and ignore the flag.` + `The --from-terraform-project flag is only supported with the typescript template. The command will continue and ignore the flag.` ); } } From 69c142468a09c94993009509e6adb314123472ec Mon Sep 17 00:00:00 2001 From: Daniel Schmidt Date: Thu, 29 Jul 2021 15:17:08 +0200 Subject: [PATCH 3/3] refactor(cli): type cdktf json --- packages/@cdktf/hcl2cdk/lib/index.ts | 6 +++++- packages/cdktf-cli/bin/cmds/helper/init.ts | 5 +---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/@cdktf/hcl2cdk/lib/index.ts b/packages/@cdktf/hcl2cdk/lib/index.ts index a231304ee4..e533b6a26a 100644 --- a/packages/@cdktf/hcl2cdk/lib/index.ts +++ b/packages/@cdktf/hcl2cdk/lib/index.ts @@ -301,10 +301,14 @@ export function getTerraformConfigFromDir(importPath: string) { return fileContents.join("\n"); } +type CdktfJson = Record & { + terraformProviders: any[]; + terraformModules: any[]; +}; export async function convertProject( combinedHcl: string, inputMainFile: string, - inputCdktfJson: Record, + inputCdktfJson: CdktfJson, { language }: ConvertOptions ) { if (language !== "typescript") { diff --git a/packages/cdktf-cli/bin/cmds/helper/init.ts b/packages/cdktf-cli/bin/cmds/helper/init.ts index 63343353b6..bb960f74f8 100644 --- a/packages/cdktf-cli/bin/cmds/helper/init.ts +++ b/packages/cdktf-cli/bin/cmds/helper/init.ts @@ -145,10 +145,7 @@ This means that your Terraform state file will be stored locally on disk in a fi "utf8" ); - const { terraformModules, terraformProviders } = cdktfJson as { - terraformModules: string[]; - terraformProviders: string[]; - }; + const { terraformModules, terraformProviders } = cdktfJson; if (terraformModules.length + terraformProviders.length > 0) { execSync("npm run get", { cwd: destination }); }