-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathconvert.ts
172 lines (146 loc) · 5.09 KB
/
convert.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* Copyright (C) 2018-2023 Garden Technologies, Inc. <[email protected]>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
import { StringMap } from "../../config/common"
import { ConvertModuleParams } from "../../plugin/handlers/Module/convert"
import { ExecActionConfig, ExecBuildConfig, defaultStatusTimeout } from "./config"
import { ExecModule } from "./moduleConfig"
export function prepareExecBuildAction(params: ConvertModuleParams<ExecModule>): ExecBuildConfig | undefined {
const { module, convertBuildDependency, dummyBuild } = params
const needsBuild =
!!dummyBuild ||
!!module.spec.build?.command ||
// We create a single Build action if there are no other entities
// (otherwise nothing is created, which would be unexpected for users).
module.serviceConfigs.length + module.taskConfigs.length + module.testConfigs.length === 0
if (needsBuild) {
return {
kind: "Build",
type: "exec",
name: module.name,
...params.baseFields,
...dummyBuild,
buildAtSource: module.spec.local,
dependencies: module.build.dependencies.map(convertBuildDependency),
timeout: module.build.timeout,
spec: {
shell: true, // This keeps the old pre-0.13 behavior
command: module.spec.build?.command,
env: module.spec.env,
},
}
}
return
}
export async function convertExecModule(params: ConvertModuleParams<ExecModule>) {
const { module, services, tasks, tests, convertBuildDependency, convertRuntimeDependencies } = params
const actions: ExecActionConfig[] = []
const buildAction = prepareExecBuildAction(params)
buildAction && actions.push(buildAction)
function prepRuntimeDeps(deps: string[]): string[] {
if (buildAction) {
return convertRuntimeDependencies(deps)
} else {
// If we don't return a Build action, we must still include any declared build dependencies
return [...module.build.dependencies.map(convertBuildDependency), ...convertRuntimeDependencies(deps)]
}
}
// Instead of doing this at runtime, we fold together env vars from the module top-level and the individual
// runtime actions at conversion time.
function prepareEnv(env: StringMap) {
return { ...module.spec.env, ...env }
}
for (const service of services) {
let persistent: any = false
let deployCommand = service.spec.deployCommand
let statusCommand = service.spec.statusCommand
if (service.spec.syncMode) {
// Maintain compatibility with devMode on exec modules
persistent = "${this.mode == 'sync'}"
if (service.spec.syncMode.command) {
deployCommand = <any>{
$if: persistent,
$then: service.spec.syncMode.command,
$else: service.spec.deployCommand,
}
}
if (service.spec.syncMode.statusCommand) {
statusCommand = <any>{
$if: persistent,
$then: service.spec.syncMode.statusCommand,
$else: service.spec.statusCommand,
}
}
}
actions.push({
kind: "Deploy",
type: "exec",
name: service.name,
...params.baseFields,
disabled: service.disabled,
build: buildAction ? buildAction.name : undefined,
dependencies: prepRuntimeDeps(service.spec.dependencies),
timeout: service.spec.timeout,
spec: {
shell: true, // This keeps the old pre-0.13 behavior
persistent,
cleanupCommand: service.spec.cleanupCommand,
deployCommand,
statusCommand,
statusTimeout: service.spec.syncMode?.timeout || defaultStatusTimeout,
env: prepareEnv(service.spec.env),
},
})
}
for (const task of tasks) {
actions.push({
kind: "Run",
type: "exec",
name: task.name,
...params.baseFields,
disabled: task.disabled,
build: buildAction ? buildAction.name : undefined,
dependencies: prepRuntimeDeps(task.spec.dependencies),
timeout: task.spec.timeout,
spec: {
shell: true, // This keeps the old pre-0.13 behavior
command: task.spec.command,
artifacts: task.spec.artifacts,
env: prepareEnv(task.spec.env),
},
})
}
for (const test of tests) {
actions.push({
kind: "Test",
type: "exec",
name: module.name + "-" + test.name,
...params.baseFields,
disabled: test.disabled,
build: buildAction ? buildAction.name : undefined,
dependencies: prepRuntimeDeps(test.spec.dependencies),
timeout: test.spec.timeout,
spec: {
shell: true, // This keeps the old pre-0.13 behavior
command: test.spec.command,
artifacts: test.spec.artifacts,
env: prepareEnv(test.spec.env),
},
})
}
return {
group: {
// This is an annoying TypeScript limitation :P
kind: <"Group">"Group",
name: module.name,
path: module.path,
actions,
variables: module.variables,
varfiles: module.varfile ? [module.varfile] : undefined,
},
}
}