-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinit.ts
97 lines (73 loc) · 2.61 KB
/
init.ts
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { flags } from "@oclif/command"
import ux from "cli-ux"
import inquirer from "inquirer"
import environmentsManager from "../lib/environments"
import BaseInfrastructureCommand from "../BaseInfrastructureCommand"
import InfrastructureFactory from "../lib/infrastructures/InfrastructureFactory"
/**
* Represents the "scaffold init" command
* used to initialize an infrastructure.
*/
class Init extends BaseInfrastructureCommand {
static description = "initialize an infrastructure"
static flags = {
help: flags.help({ char: "h" }),
}
async run() {
await this.ensureAllRequirements()
const infrastructurePath = await this.infrastructurePath()
if (!infrastructurePath) {
this.error("No infrastructures found.")
}
const infrastructurePackage = await this.infrastructurePackage(infrastructurePath)
if (!infrastructurePackage) {
this.error("Invalid scaffold.json file.")
}
const infrastructure = InfrastructureFactory.infrastructure(infrastructurePackage.type)
if (!infrastructure) {
this.error("Unknown infrastructure.")
}
const environments = await environmentsManager.findAll(infrastructurePath)
const cdktfPath = this.cdktfPath(infrastructurePath)
const terraformBinaryPath = BaseInfrastructureCommand.terraformBinaryPath()
const terraformPlanPath = await this.terraformPlanPath(infrastructurePath)
if (environments.all.length === 0) {
ux.log("No environments found. Creating a new one.\n")
const environment = await environmentsManager.create(infrastructurePath, "dev")
await environmentsManager.configure(
environment,
false,
infrastructure,
infrastructurePath,
this.configFilePath,
cdktfPath,
terraformBinaryPath,
terraformPlanPath
)
return
}
if (environments.sandboxed.length > 0 || environments.configured.length > 0) {
ux.log("Infrastructure already initialized. Nothing to do.")
return
}
ux.log("No sandboxes found. Choose an environment to create a sandbox from.\n")
const environmentChoices = environments.all
const { environmentToSandbox } = await inquirer.prompt<{ environmentToSandbox: string }>([{
name: "environmentToSandbox",
message: "Environment to create a sandbox from:",
type: "list",
choices: environmentChoices,
}])
await environmentsManager.configure(
environmentToSandbox,
true,
infrastructure,
infrastructurePath,
this.configFilePath,
cdktfPath,
terraformBinaryPath,
terraformPlanPath
)
}
}
export default Init