-
Notifications
You must be signed in to change notification settings - Fork 440
/
.hooks.sscaff.js
43 lines (34 loc) · 1.63 KB
/
.hooks.sscaff.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const { execSync } = require('child_process');
const { readFileSync, writeFileSync } = require('fs');
exports.post = ctx => {
// Terraform Cloud configuration settings if the organization name and workspace is set.
if (ctx.OrganizationName != '') {
console.log(`\nGenerating Terraform Cloud configuration for '${ctx.OrganizationName}' organization and '${ctx.WorkspaceName}' workspace.....`)
terraformCloudConfig(ctx.$base, ctx.OrganizationName, ctx.WorkspaceName);
}
const npm_cdktf = ctx.npm_cdktf;
if (!npm_cdktf) { throw new Error(`missing context "npm_cdktf"`); }
installDeps([npm_cdktf, `constructs@10`]);
installDeps(['@types/node', 'typescript', 'jest@^26.6.3', '@types/jest', "ts-jest", "ts-node"], true);
console.log(readFileSync('./help', 'utf-8'));
};
function installDeps(deps, isDev) {
const devDep = isDev ? '-D' : '';
// make sure we're installing dev dependencies as well
const env = Object.assign({}, process.env)
env['NODE_ENV'] = 'development'
execSync(`npm install ${devDep} ${deps.join(' ')}`, { stdio: 'inherit', env });
}
function terraformCloudConfig(baseName, organizationName, workspaceName) {
template = readFileSync('./main.ts', 'utf-8');
result = template.replace(`import { App, TerraformStack } from 'cdktf';`, `import { App, TerraformStack, RemoteBackend } from 'cdktf';`);
result = result.replace(`new MyStack(app, '${baseName}');`, `const stack = new MyStack(app, '${baseName}');
new RemoteBackend(stack, {
hostname: 'app.terraform.io',
organization: '${organizationName}',
workspaces: {
name: '${workspaceName}'
}
});`);
writeFileSync('./main.ts', result, 'utf-8');
}