This commit is contained in:
Simon Larsen
2022-11-09 14:23:14 +00:00
parent 501ad7565e
commit 6f54275e95
2 changed files with 43 additions and 42 deletions

View File

@@ -1,40 +1,42 @@
// This script merges config.env.tpl to config.env
const fs = require('fs');
const fs: any = require('fs');
const init = (): void => {
const init: Function = (): void => {
const tempate: string = fs.readFileSync('./config.tpl.env', 'utf8');
const env: string = fs.readFileSync('./config.env', 'utf8');
const linesInTemplate: Array<string> = tempate.split("\n");
const linesInEnv: Array<string> = env.split("\n");
const linesInTemplate: Array<string> = tempate.split('\n');
const linesInEnv: Array<string> = env.split('\n');
for (const line of linesInTemplate) {
// this is a comment, ignore.
if (line.startsWith("//")) {
// this is a comment, ignore.
if (line.startsWith('//')) {
continue;
}
// comment. Ignore.
if (line.startsWith("#")) {
// comment. Ignore.
if (line.startsWith('#')) {
continue;
}
// if the line is present in template but is not present in env file then add it to the env file. We assume, values in template file are default values.
if (line.split("=").length > 0) {
if (linesInEnv.filter((envLine) => {
return envLine.split("=").length > 0 && envLine.split("=")[0] === line.split("=")[0]
}).length === 0) {
// if the line is present in template but is not present in env file then add it to the env file. We assume, values in template file are default values.
if (line.split('=').length > 0) {
if (
linesInEnv.filter((envLine: string) => {
return (
envLine.split('=').length > 0 &&
envLine.split('=')[0] === line.split('=')[0]
);
}).length === 0
) {
linesInEnv.push(line);
}
}
}
// write the file back to disk and exit.
fs.writeFileSync('./config.env.temp', linesInEnv.join("\n"));
}
// write the file back to disk and exit.
fs.writeFileSync('./config.env.temp', linesInEnv.join('\n'));
};
init();
init();

View File

@@ -1,49 +1,48 @@
// This script merges config.env.tpl to config.env
const fs = require('fs');
const fs: any = require('fs');
const init = (): void => {
const init: Function = (): void => {
const env: string = fs.readFileSync('./config.env', 'utf8');
const envValToReplace: string | undefined = process.argv[2];
if (!envValToReplace) {
console.log("Please have env var to replace");
// eslint-disable-next-line
console.log('Please have env var to replace');
return;
}
const envValToReplaceWith: string | undefined = process.argv[3];
if (!envValToReplaceWith) {
console.log("Please have env var to replace with");
// eslint-disable-next-line
console.log('Please have env var to replace with');
return;
}
const linesInEnv: Array<string> = env.split("\n");
const linesInEnv: Array<string> = env.split('\n');
const linesToRender: Array<string> = [];
let found: boolean = false;
for (let line of linesInEnv) {
// this is a comment, ignore.
// this is a comment, ignore.
if (!line.startsWith(envValToReplace)) {
linesToRender.push(line);
} else {
found = true;
const items = line.split("=");
const items: Array<string> = line.split('=');
items[1] = envValToReplaceWith;
line = items.join("=");
line = items.join('=');
linesToRender.push(line);
}
}
if (!found) {
linesToRender.push(envValToReplace + "=" + envValToReplaceWith);
linesToRender.push(envValToReplace + '=' + envValToReplaceWith);
}
// write the file back to disk and exit.
fs.writeFileSync('./config.env', linesToRender.join("\n"));
}
// write the file back to disk and exit.
fs.writeFileSync('./config.env', linesToRender.join('\n'));
};
init();