-
Notifications
You must be signed in to change notification settings - Fork 7
/
.hooks.sscaff.js
53 lines (44 loc) · 1.75 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
44
45
46
47
48
49
50
51
52
53
const { execSync } = require('child_process');
const { chmodSync } = require('fs');
const { readFileSync, writeFileSync } = require('fs');
const os = require('os');
exports.pre = () => {
try {
if (os.platform() === 'win32') {
execSync('where poetry')
}
else {
execSync('which poetry')
}
} catch {
console.error(`Unable to find "poetry". Install from https://python-poetry.org/docs/#installation`)
process.exit(1);
}
};
exports.post = options => {
// Terraform Cloud configuration settings if the organization name and workspace is set.
if (options.OrganizationName != '') {
console.log(`\nGenerating Terraform Cloud configuration for '${options.OrganizationName}' organization and '${options.WorkspaceName}' workspace.....`)
terraformCloudConfig(options.$base, options.OrganizationName, options.WorkspaceName)
}
const pypi_cdktf = options.pypi_cdktf;
if (!pypi_cdktf) {
throw new Error(`missing context "pypi_cdktf"`);
}
execSync('poetry install', { stdio: 'inherit' });
execSync(`poetry add ${pypi_cdktf}`, { stdio: 'inherit' });
chmodSync('main.py', '700');
console.log(readFileSync('./help', 'utf-8'));
};
function terraformCloudConfig(baseName, organizationName, workspaceName) {
template = readFileSync('./main.py', 'utf-8');
const templateWithImports = template.replace(`from cdktf import App, TerraformStack`,
`from cdktf import App, TerraformStack, RemoteBackend, NamedRemoteWorkspace`)
const result = templateWithImports.replace(`MyStack(app, "${baseName}")`, `stack = MyStack(app, "${baseName}")
RemoteBackend(stack,
hostname='app.terraform.io',
organization='${organizationName}',
workspaces=NamedRemoteWorkspace('${workspaceName}')
)`);
writeFileSync('./main.py', result, 'utf-8');
}