-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigure.js
307 lines (222 loc) · 10.4 KB
/
configure.js
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// -------------------------------------------------------------------------------
// MIT License
//
// Copyright (c) 2022 Gareth Brown
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// -------------------------------------------------------------------------------
// -------------------------------------------------------------------------------
// For custom software development and consulting contact [email protected]
// -------------------------------------------------------------------------------
// -------------------------------------------------------------------------------
// Script recursively searches file tree for environment template
// files and replaces in variables for environment as indicated
// by data in top level .env file name.
// -------------------------------------------------------------------------------
var fs = require('fs');
var path = require('path');
const readline = require('readline');
String.prototype.toPascalCase = function () {
return this.replace(new RegExp(/[-_]+/, 'g'), ' ')
.replace(new RegExp(/[^\w\s]/, 'g'), '')
.replace(new RegExp(/\s+(.)(\w+)/, 'g'), ($1, $2, $3) => `${$2.toUpperCase() + $3.toLowerCase()}`)
.replace(new RegExp(/\s/, 'g'), '')
.replace(new RegExp(/\w/), (s) => s.toUpperCase());
};
String.prototype.replaceAllCaseInsensitive = function (strReplace, strWith) {
// https://stackoverflow.com/questions/7313395/case-insensitive-replace-all
var esc = strReplace.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
var reg = new RegExp(esc, 'ig');
return this.replace(reg, strWith);
};
// Recursive search of file tree looking for files matching environment template
// patterns.
let allEnvKeys = [];
let fileEnvKeyWarnings = [];
var recurseFindEnvTemplates = function (dir, environmentName, envVariables) {
// Read a file list syncronously
const list = fs.readdirSync(dir);
list.forEach(function (filePath, i) {
filePath = path.resolve(dir, filePath);
let info = fs.statSync(filePath);
if (info && info.isDirectory()) {
recurseFindEnvTemplates(filePath, environmentName, envVariables);
} else {
let fileName = path.basename(filePath);
let directoryName = path.dirname(filePath);
let isMatch = /\.envt$/.test(fileName);
if (isMatch) {
console.log(`Result: ${filePath} ${i}`);
let outputFileNameEnvString;
let outputFileName = fileName;
if (fileName.includes('.Env.')) {
outputFileNameEnvString = environmentName.toPascalCase();
} else if (fileName.includes('.env.')) {
outputFileNameEnvString = environmentName.toLowerCase();
} else if (fileName.includes('.ENV.')) {
outputFileNameEnvString = environmentName.toUpperCase();
} else {
// No environment name in file path, but this is not necessarily an error
}
// Replace file extension first - that way if target output file extension is .env
// we don't inadvertently replace with environment name if not intended.
outputFileName = outputFileName.replace('.envt', '');
outputFileName = outputFileName.replaceAllCaseInsensitive('.env.', `.${outputFileNameEnvString}.`);
console.log(`Generated file name: ${outputFileName}`);
let templateText = fs.readFileSync(filePath, 'utf8');
// Search for all env keys in current file and track number of times replaced
let envKeysRegex = /\$\{([A-Z_]+)\}/g;
const fileEnvKeysMatchArray = [...templateText.matchAll(envKeysRegex)];
let fileEnvKeys = [];
fileEnvKeysMatchArray.forEach(function(val, i) {
if(!fileEnvKeys.some(e => e.key === val[1]))
{
fileEnvKeys.push({
key: val[1],
count: 0
});
}
});
for (let i = 0; i < envVariables.length; i++) {
// Need to replace all instances, not just first. Regex version
// was fiddly with ${} template string, so use while loop.
const findKeyTemplate = '${' + envVariables[i].key + '}';
// Replace text in string and track number of times replaced, globally and in current file
let replaceInstancesLocal = 0;
while (templateText.includes(findKeyTemplate)) {
// .replace runs only for first instance found so run until no further instances found
templateText = templateText.replace(findKeyTemplate, envVariables[i].value);
replaceInstancesLocal++;
}
if(!allEnvKeys.some(e => e.key === envVariables[i].key))
{
allEnvKeys.push({
key: envVariables[i].key,
count: replaceInstancesLocal
})
}
else
{
allEnvKeys.filter(e => e.key === envVariables[i].key)[0].count += replaceInstancesLocal;
}
var matchingFileEnvKeys = fileEnvKeys.filter(e => e.key === envVariables[i].key);
if(matchingFileEnvKeys.length > 0)
{
// Should not be more than 0
matchingFileEnvKeys[0].count += replaceInstancesLocal;
}
}
// Where any env keys were not replaced in currenyt file, add a warning
fileEnvKeys.filter(e => e.count === 0).forEach(function(nonReplacedEnvKey, it) {
fileEnvKeyWarnings.push({
key: nonReplacedEnvKey.key,
filePath: filePath
});
});
console.log(templateText);
fs.writeFileSync(directoryName + '/' + outputFileName, templateText);
}
}
});
};
// Execute, read args (first 2 are 'node', path and can be ignored)
const argsArray = process.argv.slice(2);
if (argsArray.length === 2) {
const envVariablesFileName = argsArray[0];
const initPath = argsArray[1];
if (envVariablesFileName.endsWith('.env')) {
// Environment variable files have extension '.env'. Replace
// the extension to get environment name - the part of the file name
// preceeding the extension will be used as the environment name for
// naming output files if the .envt filename contains environment name
// replace pattern.
const environmentName = envVariablesFileName.replace('.env', '');
// Read environment variable file and split into line data
const envVariablesFileText = fs.readFileSync(`./${envVariablesFileName}`, 'utf8');
const lineTextArray = envVariablesFileText.split(/\r?\n/);
let envVariables = [];
for (let i = 0; i < lineTextArray.length; i++) {
let lineText = lineTextArray[i];
if (lineText) {
lineText = lineText.trim();
// Check line is not empty
if (lineText.length > 0) {
// Check line is not comment
if (!lineText.startsWith('#')) {
// Split on first occurence of =
let lineVariables = lineText.split('=');
// Check length following split. Treat single item array
// as value with empty string, otherwise take raw value as item for replace in.
// No JSON encode is attempted, we want to replace raw string. This means that strings
// with chars with special meanings in JSON will need to be manually escaped in
// environment variable files, but allows for array strings etc to be replaced in
// where required.
console.log(lineVariables)
var envVariable;
if (lineVariables.length === 1) {
// Have key, no value
envVariable = {
key: lineVariables[0],
value: '',
};
} else if (lineVariables.length === 2) {
// Standard key value
envVariable = {
key: lineVariables[0],
value: lineVariables[1],
};
} else {
// Value contains '=' char. Take the portion after the key
// and rejoin
envVariable = {
key: lineVariables[0],
value: lineVariables.slice(1).join('=')
};
}
// Remove existing array items of same value so that
// later entries take precedence
envVariables = envVariables.filter(function(obj) {
return obj.key !== envVariable.key;
});
envVariables.push(envVariable);
}
}
}
}
recurseFindEnvTemplates(initPath, environmentName, envVariables);
// Log out any warnings in yellow
const nonReplacedKeys = allEnvKeys.filter(e => e.count === 0);
if(nonReplacedKeys.length > 0)
{
nonReplacedKeys.forEach(function (key, i) {
console.log('\x1b[33m%s\x1b[0m', `WARNING: No replace template found in any file for: ${key.key}`);
});
}
if(fileEnvKeyWarnings.length > 0)
{
fileEnvKeyWarnings.forEach(function (fileKeyWarning, i) {
console.log('\x1b[33m%s\x1b[0m', `WARNING: No environment variable found ${fileKeyWarning.key} (${fileKeyWarning.filePath})`);
});
}
} else {
throw "Error: Environment variables configuration file is expected to have '.env' extension.";
}
} else {
throw 'Error: Environment variables configuration file is required at arg[0].';
}