-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild.ts
116 lines (99 loc) · 3.15 KB
/
build.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
import arg from 'arg'
import generatePatchBody from './generators/generatePatchBody'
import fs from 'fs'
import path from 'path'
import generateTerraformPropertyRules from './generators/generateTerraformPropertyRules'
const args = arg({
'--type': String,
'--integration-path': String,
'--agent-path': String,
'--result-path': String,
'--proxy-secret': String,
'--cdn-url': String,
'--ingress-url': String,
})
args['--type'] = args['--type'] ? args['--type'] : 'all'
export interface PatchBodyArgs {
integrationPath: string
agentPath: string
resultPath: string
proxySecret: string
cdnUrl: string
ingressUrl: string
}
const defaults = {
cdnUrl: 'fpcdn.io',
ingressUrl: 'api.fpjs.io',
}
export const patchBody = (_args?: PatchBodyArgs) => {
const bodyContent = generatePatchBody({
integrationPath: args['--integration-path'] ?? _args?.integrationPath,
agentPath: args['--agent-path'] ?? _args?.agentPath,
resultPath: args['--result-path'] ?? _args?.resultPath,
proxySecret: args['--proxy-secret'] ?? _args?.proxySecret,
cdnUrl: args['--cdn-url'] ?? _args?.cdnUrl ?? defaults.cdnUrl,
ingressUrl: args['--ingress-url'] ?? _args?.ingressUrl ?? defaults.ingressUrl,
})
fs.mkdirSync(path.relative(process.cwd(), 'dist/patch-body'), { recursive: true })
const bodyPath = path.relative(process.cwd(), 'dist/patch-body/body.json')
fs.writeFile(bodyPath, bodyContent, (err) => {
if (err) {
console.error(err)
process.exit(1)
}
})
}
const terraform = () => {
const { variablesBody, rulesBody } = generateTerraformPropertyRules({
cdnUrl: args['--cdn-url'] ?? defaults.cdnUrl,
ingressUrl: args['--ingress-url'] ?? defaults.ingressUrl,
})
const bodyPath = path.relative(process.cwd(), 'dist/terraform/json/fingerprint-property-rules.json')
const variablesPath = path.relative(process.cwd(), 'dist/terraform/json/fingerprint-property-variables.json')
fs.mkdirSync(path.relative(process.cwd(), 'dist/terraform/json'), { recursive: true })
fs.writeFile(variablesPath, variablesBody, (err) => {
if (err) {
console.error(err)
process.exit(1)
}
})
const terraformFilePath = path.relative(process.cwd(), 'assets/example.tf')
const targetPath = path.relative(process.cwd(), 'dist/terraform/example.tf')
fs.copyFile(terraformFilePath, targetPath, (err) => {
if (err) {
console.error(err)
process.exit(1)
}
})
fs.writeFile(bodyPath, rulesBody, (err) => {
if (err) {
console.error(err)
process.exit(1)
}
})
}
const flows = {
patchBody,
terraform,
}
const flowKeys = Object.keys(flows)
if (!args['--type'] || (args['--type'] && !['all', ...flowKeys].includes(args['--type']))) {
console.error(`You must specify a build type. Please select from these values: all, ${flowKeys.join(', ')}`)
process.exit(1)
}
fs.rmSync(path.relative(process.cwd(), 'dist'), { recursive: true, force: true })
switch (args['--type']) {
default:
case 'all':
const functions = Object.values(flows)
for (const func of functions) {
func()
}
break
case 'patchBody':
flows.patchBody()
break
case 'terraform':
flows.terraform()
break
}