Skip to content

Instantly share code, notes, and snippets.

@Ethan-Arrowood
Created December 28, 2017 04:27
Show Gist options
  • Save Ethan-Arrowood/f9f460ee146d5acbec629b41f9f66e0b to your computer and use it in GitHub Desktop.
Save Ethan-Arrowood/f9f460ee146d5acbec629b41f9f66e0b to your computer and use it in GitHub Desktop.
Automatically generate .env variables for HarperDB connection.
const fs = require("fs");
fs.readFile("./install_log.log", (err, data) => {
if (err) throw err;
const s = data.toString();
// prettier-ignore
const regex_ports = /HTTPS?_PORT\\\"\:\\\"(\d+)/g;
let port_match = regex_ports.exec(s);
const http_port = port_match[1]
port_match = regex_ports.exec(s);
const https_port = port_match[1];
// prettier-ignore
const regex_creds = /HDB_ADMIN_\w+\\\":\\\"([^:]+(?=\\\"(?:,|})))/g;
let cred_match = regex_creds.exec(s);
const username = cred_match[1];
cred_match = regex_creds.exec(s);
const password = cred_match[1];
if (username === undefined
|| password === undefined
|| http_port === undefined
|| https_port === undefined )
throw new Error("Error reading log file. Please add .env manually.");
const envVars =
`HDB_HTTP_PORT=${http_port}\nHDB_HTTPS_PORT=${https_port}\nHDB_USERNAME=${username}\nHDB_PASSWORD=${password}`;
fs.writeFile(".env", envVars, err => {
if (err) throw err;
console.log("The file has been saved!");
console.log(`envVars: \n${envVars}`);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment