This repository has been archived by the owner on Feb 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
224 lines (193 loc) · 5.72 KB
/
index.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import fs from 'node:fs'
import DWDAV from 'dwdav'
import Steps from 'cli-step'
import chalk from 'chalk'
import archiver from 'archiver'
import sfccCi from 'sfcc-ci'
import type { Step } from 'cli-step'
interface SFCCDeployCredential {
hostname: string
}
export interface SFCCCIDeployCredential extends SFCCDeployCredential {
clientId: string
clientSecret: string
p12: string
passphrase: string
}
export interface DWDAVDeployCredential extends SFCCCIDeployCredential {
username: string
password: string
}
export type SFCCDeployCredentials = SFCCCIDeployCredential | DWDAVDeployCredential;
export interface SFCCDeployStepTextOptions {
options: SFCCDeployOptions
dwdav?: DWDAV
rootDir: string
step: Step
stepText: string
}
export type SFCCDeployStepText = string | ((opts: SFCCDeployStepTextOptions) => string);
export interface SFCCDeployAdditionalStep {
condition: string
name: SFCCDeployStepText
emoji: string
fn: (opts: SFCCDeployAdditionalStepOptions) => Promise<void>
specialFinish?: boolean
}
export interface SFCCDeployAdditionalStepOptions {
options: SFCCDeployOptions
dwdav?: DWDAV
token: string
useSfccCi: boolean
rootDir: string
step: Step
stepText: string
}
export interface SFCCDeployOptions {
[key: string]: undefined | boolean | string | SFCCDeployCredentials | SFCCDeployAdditionalStep[]
credentials: SFCCDeployCredentials
version: string
root?: string
additionalSteps?: SFCCDeployAdditionalStep[]
}
export default async (options: SFCCDeployOptions): Promise<void> => {
const {
credentials: config,
version,
root,
additionalSteps,
} = options
const useSfccCi = !!(config.clientId && config.clientSecret)
const rootDir = root ?? './dist/'
const dwdav = useSfccCi ? undefined : new DWDAV({
...config,
folder: 'Cartridges',
version,
})
let token: string
let stepText: string
let step: Step
let steps: Steps
const finishStep = (success: boolean) => {
if (success) {
step.success(`${stepText} - ${chalk.bold.green('OK')}`)
} else {
step.error(`${stepText} - ${chalk.bold.red('FAIL')}`)
}
return true
}
const defineStep = (
prmStepText: SFCCDeployStepText,
emoji: string,
fn: () => Promise<unknown>,
specialFinish?: boolean,
) => async () => {
stepText = typeof prmStepText === 'function' ? prmStepText({
options, dwdav, rootDir, step, stepText,
}) : prmStepText
step = steps
.advance(stepText, emoji)
.start()
const ret = await fn()
if (!specialFinish) {
finishStep(true)
}
return ret
}
const additionalActiveSteps = additionalSteps
? additionalSteps
.filter((additionalStep) => options[additionalStep.condition])
.map(({
name, emoji, fn, specialFinish,
}) => defineStep(name, emoji, () => fn({
options, dwdav, token, useSfccCi, rootDir, step, stepText,
}), specialFinish))
: []
const stepCount = (useSfccCi ? 2 : 6) + additionalActiveSteps.length
steps = new Steps(stepCount)
const zipFileName = 'cartridges.zip'
const zipFile = rootDir + zipFileName
const zipCartridges = defineStep('Creating ZIP', 'hammer', async () => {
const output = fs.createWriteStream(zipFile)
const archive = archiver('zip', {
zlib: { level: 9 },
})
archive.pipe(output)
archive.directory(`${rootDir}cartridges/`, useSfccCi ? version : false)
await archive.finalize()
const stats = fs.statSync(zipFile)
const sizeMb = (stats.size / 1024 / 1024).toLocaleString(undefined, {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
})
step.success(`${stepText} - ${chalk.green(sizeMb)} MB - ${chalk.bold.green('OK')}`)
}, true)
const checkConnection = defineStep('Check connection', 'earth_africa', async () => {
await (dwdav as DWDAV).get('..')
})
const checkCodeVersionExistance = defineStep('Check for existing code version', 'mag', async () => {
let codeVersionExists
try {
await (dwdav as DWDAV).get('.')
codeVersionExists = true
} catch {
codeVersionExists = false
}
if (codeVersionExists) {
await (dwdav as DWDAV).delete('.')
step.success(`${stepText} - ${chalk.bold.green('Deleted existing version')}`)
} else {
step.success(`${stepText} - ${chalk.bold.green('Not found')}`)
}
}, true)
const uploadZip = defineStep('Uploading ZIP', 'truck', async () => {
await (dwdav as DWDAV).post(`${rootDir}cartridges.zip`, rootDir)
})
const unzip = defineStep('Unzipping', 'gift', async () => {
await (dwdav as DWDAV).unzip(zipFileName)
})
const deleteZip = defineStep('Delete remote ZIP', 'wastebasket', async () => {
await (dwdav as DWDAV).delete(zipFileName)
})
const sfccCiDeploy = defineStep('Deploy code', 'truck', async () => {
token = await new Promise((resolve, reject) => {
sfccCi.auth.auth(config.clientId, config.clientSecret, (err, receivedToken) => {
if (err) {
reject(err)
} else {
resolve(receivedToken)
}
})
})
await new Promise((resolve, reject) => {
sfccCi.code.deploy(config.hostname, zipFile, token, {
pfx: config.p12 || undefined,
passphrase: config.passphrase ?? undefined,
}, (err) => {
if (err) {
reject(err)
} else {
resolve(undefined)
}
})
})
})
try {
await zipCartridges()
if (useSfccCi) {
await sfccCiDeploy()
} else {
await checkConnection()
await checkCodeVersionExistance()
await uploadZip()
await unzip()
await deleteZip()
}
additionalActiveSteps.forEach(async (additionalStep) => {
await additionalStep()
})
} catch (e) {
finishStep(false)
throw (e)
}
}