Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(hcl2cdk): only run get conditionally #846

Merged
merged 3 commits into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions packages/@cdktf/hcl2cdk/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,14 @@ export function getTerraformConfigFromDir(importPath: string) {
return fileContents.join("\n");
}

type CdktfJson = Record<string, unknown> & {
terraformProviders: any[];
terraformModules: any[];
Comment on lines +305 to +306
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: they could also be undefined / not set for the input, but they won't be when they're outputted, right?

};
export async function convertProject(
combinedHcl: string,
inputMainFile: string,
inputCdktfJson: Record<string, unknown>,
inputCdktfJson: CdktfJson,
{ language }: ConvertOptions
) {
if (language !== "typescript") {
Expand Down Expand Up @@ -332,7 +336,7 @@ export async function convertProject(

return {
code: prettier.format(outputMainFile, { parser: "babel" }),
cdktfJson: prettier.format(JSON.stringify(cdktfJson), { parser: "json" }),
cdktfJson,
stats,
};
}
6 changes: 5 additions & 1 deletion packages/@cdktf/hcl2cdk/test/convertProject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
10 changes: 7 additions & 3 deletions packages/cdktf-cli/bin/cmds/helper/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,18 +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;
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.`
`The --from-terraform-project flag is only supported with the typescript template. The command will continue and ignore the flag.`
);
}
}
Expand Down