|
1 | 1 | // @ts-check
|
2 | 2 |
|
3 |
| -import { execSync } from 'child_process' |
4 |
| -import { existsSync, rmSync, openSync, writeSync, closeSync, readFileSync, writeFileSync, readdirSync } from 'fs' |
| 3 | +import { execSync } from 'child_process'; |
| 4 | +import { |
| 5 | + existsSync, |
| 6 | + rmSync, |
| 7 | + openSync, |
| 8 | + writeSync, |
| 9 | + closeSync, |
| 10 | + readFileSync, |
| 11 | + writeFileSync, |
| 12 | + readdirSync, |
| 13 | +} from 'fs'; |
5 | 14 | // @ts-ignore
|
6 |
| -import YAML from 'yaml' |
| 15 | +import YAML from 'yaml'; |
7 | 16 |
|
8 | 17 | // get the list of env variable values
|
9 | 18 | export const getEnvs = (...envs) => {
|
10 | 19 | if (envs?.length) {
|
11 |
| - return envs.map(name => { |
12 |
| - const value = process.env[name] |
| 20 | + return envs.map((name) => { |
| 21 | + const value = process.env[name]; |
13 | 22 | if (!value) {
|
14 |
| - throw new Error(`Env variable ${name} is required.`) |
| 23 | + throw new Error(`Env variable ${name} is required.`); |
15 | 24 | }
|
16 |
| - return value |
17 |
| - }) |
| 25 | + return value; |
| 26 | + }); |
18 | 27 | } else {
|
19 |
| - throw new Error(`Missing env variable list.`) |
| 28 | + throw new Error(`Missing env variable list.`); |
20 | 29 | }
|
21 |
| -} |
| 30 | +}; |
22 | 31 |
|
23 |
| -const isFeatureBranch = (name) => name.startsWith('feat-') |
| 32 | +const isFeatureBranch = (name) => name.startsWith('feat-'); |
24 | 33 |
|
25 | 34 | // get feature branch template (value file) path
|
26 | 35 | export const getValueFilesPath = (name) => {
|
27 | 36 | if (!name) {
|
28 |
| - throw new Error(`Missing app name.`) |
| 37 | + throw new Error(`Missing app name.`); |
29 | 38 | }
|
30 |
| - const basePath = 'charts/jumper-exchange' |
31 |
| - const files = readdirSync(basePath) |
32 |
| - const valueFiles = files.filter(f => f.startsWith(`values-${name}`)) |
| 39 | + const basePath = 'charts/jumper-exchange'; |
| 40 | + const files = readdirSync(basePath); |
| 41 | + const valueFiles = files.filter((f) => f.startsWith(`values-${name}`)); |
33 | 42 | if (!valueFiles.length) {
|
34 | 43 | // we do not throw an error since if a feature branch value file
|
35 | 44 | // does not exists, we want to create it instead (first deploy)
|
36 | 45 | if (isFeatureBranch(name)) {
|
37 |
| - return [`${basePath}/values-${name}.yaml`] |
| 46 | + return [`${basePath}/values-${name}.yaml`]; |
38 | 47 | }
|
39 |
| - throw new Error(`Cannot find value file for ${name}.`) |
| 48 | + throw new Error(`Cannot find value file for ${name}.`); |
40 | 49 | }
|
41 |
| - return valueFiles.map(v => `${basePath}/${v}`) |
42 |
| -} |
| 50 | + return valueFiles.map((v) => `${basePath}/${v}`); |
| 51 | +}; |
43 | 52 |
|
44 | 53 | // get a deployment template (value file)
|
45 | 54 | export const getDeploymentTemplates = (appName) => {
|
46 |
| - const valueFiles = getValueFilesPath(appName) |
47 |
| - const templates = [] |
| 55 | + const valueFiles = getValueFilesPath(appName); |
| 56 | + const templates = []; |
48 | 57 | for (const valueFile of valueFiles) {
|
49 | 58 | if (existsSync(valueFile)) {
|
50 |
| - const file = readFileSync(valueFile, 'utf8') |
51 |
| - templates.push({ valueFile, template: YAML.parse(file) }) |
| 59 | + const file = readFileSync(valueFile, 'utf8'); |
| 60 | + templates.push({ valueFile, template: YAML.parse(file) }); |
52 | 61 | } else {
|
53 |
| - throw new Error(`Missing ${valueFile} file.`) |
| 62 | + throw new Error(`Missing ${valueFile} file.`); |
54 | 63 | }
|
55 | 64 | }
|
56 |
| - return templates |
57 |
| -} |
| 65 | + return templates; |
| 66 | +}; |
58 | 67 |
|
59 | 68 | // update a deployment template (value file)
|
60 | 69 | export const updateDeploymentTemplate = ({ valueFile, template }) => {
|
61 | 70 | if (template) {
|
62 | 71 | if (template?.feature?.version) {
|
63 |
| - template.feature.version += 1 |
| 72 | + template.feature.version += 1; |
64 | 73 | }
|
65 |
| - const file = openSync(valueFile, 'w') // w: write and create if does not exist |
66 |
| - const yamlData = YAML.stringify(template) |
| 74 | + const file = openSync(valueFile, 'w'); // w: write and create if does not exist |
| 75 | + const yamlData = YAML.stringify(template); |
67 | 76 | if (yamlData.length) {
|
68 |
| - writeSync(file, yamlData) |
69 |
| - closeSync(file) |
| 77 | + writeSync(file, yamlData); |
| 78 | + closeSync(file); |
70 | 79 | } else {
|
71 |
| - closeSync(file) |
72 |
| - throw new Error(`Yaml data shouldn't be empty.`) |
| 80 | + closeSync(file); |
| 81 | + throw new Error(`Yaml data shouldn't be empty.`); |
73 | 82 | }
|
74 |
| - execSync(`git add ${valueFile}`, { shell: '/bin/bash' }) |
| 83 | + execSync(`git add ${valueFile}`, { shell: '/bin/bash' }); |
75 | 84 | } else {
|
76 |
| - throw new Error(`The template shouldn't be empty.`) |
| 85 | + throw new Error(`The template shouldn't be empty.`); |
77 | 86 | }
|
78 |
| -} |
| 87 | +}; |
79 | 88 |
|
80 | 89 | // delete the feature branch template (value file)
|
81 | 90 | // do not throw an error if missing (already deleted)
|
82 | 91 | export const deleteFeatureTemplates = (appName) => {
|
83 |
| - const valueFiles = getValueFilesPath(appName) |
| 92 | + const valueFiles = getValueFilesPath(appName); |
84 | 93 | for (const valueFile of valueFiles) {
|
85 | 94 | if (existsSync(valueFile)) {
|
86 |
| - rmSync(valueFile, { force: true }) |
87 |
| - execSync(`git add ${valueFile}`, { shell: '/bin/bash' }) |
| 95 | + rmSync(valueFile, { force: true }); |
| 96 | + execSync(`git add ${valueFile}`, { shell: '/bin/bash' }); |
88 | 97 | }
|
89 | 98 | }
|
90 |
| -} |
| 99 | +}; |
91 | 100 |
|
92 | 101 | // get the app list in the apps/value.yaml file of the deployment repo
|
93 | 102 | export const getFeatureBranchList = () => {
|
94 |
| - const file = readFileSync('apps/values.yaml', 'utf8') |
95 |
| - const valueFile = YAML.parse(file) |
| 103 | + const file = readFileSync('apps/values.yaml', 'utf8'); |
| 104 | + const valueFile = YAML.parse(file); |
96 | 105 | if (valueFile?.featureBranchesJumper) {
|
97 |
| - return valueFile.featureBranchesJumper |
| 106 | + return valueFile.featureBranchesJumper; |
98 | 107 | } else {
|
99 |
| - throw new Error('Missing feature branch list in apps/values.yaml') |
| 108 | + throw new Error('Missing feature branch list in apps/values.yaml'); |
100 | 109 | }
|
101 |
| -} |
| 110 | +}; |
102 | 111 |
|
103 | 112 | // replace the app list in the apps/value.yaml file of the deployment repo
|
104 | 113 | const saveFeatureBranchList = (newList) => {
|
105 | 114 | if (!newList) {
|
106 |
| - throw new Error(`App list must be an array.`) |
| 115 | + throw new Error(`App list must be an array.`); |
107 | 116 | }
|
108 |
| - const valueFile = YAML.parse(readFileSync('apps/values.yaml', 'utf8')) |
| 117 | + const valueFile = YAML.parse(readFileSync('apps/values.yaml', 'utf8')); |
109 | 118 | if (valueFile?.featureBranchesJumper) {
|
110 |
| - valueFile.featureBranchesJumper = newList |
111 |
| - writeFileSync('apps/values.yaml', YAML.stringify(valueFile)) |
| 119 | + valueFile.featureBranchesJumper = newList; |
| 120 | + writeFileSync('apps/values.yaml', YAML.stringify(valueFile)); |
112 | 121 | } else {
|
113 |
| - throw new Error(`Yaml data shouldn't be empty.`) |
| 122 | + throw new Error(`Yaml data shouldn't be empty.`); |
114 | 123 | }
|
115 |
| - execSync(`git add apps/values.yaml`, { shell: '/bin/bash' }) |
116 |
| -} |
| 124 | + execSync(`git add apps/values.yaml`, { shell: '/bin/bash' }); |
| 125 | +}; |
117 | 126 |
|
118 | 127 | // update the app list in the apps/value.yaml file of the deployment repo
|
119 | 128 | export const updateFeatureBranchList = (appName, remove = false) => {
|
120 | 129 | if (!appName) {
|
121 |
| - throw new Error(`Missing feature branch name.`) |
| 130 | + throw new Error(`Missing feature branch name.`); |
122 | 131 | }
|
123 | 132 | // get the list and add or remove the new app
|
124 |
| - const list = getFeatureBranchList() |
125 |
| - const index = list.findIndex(a => a === appName) |
| 133 | + const list = getFeatureBranchList(); |
| 134 | + const index = list.findIndex((a) => a === appName); |
126 | 135 | if (remove) {
|
127 | 136 | if (index > -1) {
|
128 |
| - list.splice(index, 1) |
129 |
| - saveFeatureBranchList(list) |
| 137 | + list.splice(index, 1); |
| 138 | + saveFeatureBranchList(list); |
130 | 139 | } else {
|
131 |
| - console.warn(`${appName} does not exist in app list.`) |
| 140 | + console.warn(`${appName} does not exist in app list.`); |
132 | 141 | }
|
133 | 142 | } else {
|
134 | 143 | if (list.indexOf(appName) === -1) {
|
135 |
| - list.push(appName) |
136 |
| - saveFeatureBranchList(list) |
| 144 | + list.push(appName); |
| 145 | + saveFeatureBranchList(list); |
137 | 146 | } else {
|
138 |
| - console.warn(`${appName} is already in app list.`) |
| 147 | + console.warn(`${appName} is already in app list.`); |
139 | 148 | }
|
140 | 149 | }
|
141 |
| -} |
| 150 | +}; |
0 commit comments