2021-11-10 17:24:03 -06:00
|
|
|
const ngrok = require("ngrok");
|
|
|
|
const fs = require("fs");
|
2021-11-10 16:35:27 -06:00
|
|
|
const { exec } = require("child_process");
|
|
|
|
|
2021-11-10 17:24:03 -06:00
|
|
|
const dotenvFile = "../.env";
|
|
|
|
|
|
|
|
const optionDefinitions = [
|
2021-11-10 18:01:49 -06:00
|
|
|
{ name: "proto", alias: 'p', type: String },
|
|
|
|
{ name: "addr", alias: 'a', type: Number },
|
2021-11-10 17:24:03 -06:00
|
|
|
];
|
|
|
|
|
|
|
|
const commandLineArgs = require("command-line-args");
|
|
|
|
const options = commandLineArgs(optionDefinitions);
|
|
|
|
|
|
|
|
console.log(options);
|
2021-11-10 16:35:27 -06:00
|
|
|
|
|
|
|
// Read the .env file
|
2021-11-10 17:24:03 -06:00
|
|
|
fs.readFile(dotenvFile, "utf8", (err, data) => {
|
|
|
|
if (err) {
|
|
|
|
console.error(err);
|
|
|
|
return;
|
|
|
|
}
|
2021-11-10 16:35:27 -06:00
|
|
|
|
2021-11-10 17:24:03 -06:00
|
|
|
// Convert string to string array, split at newlines
|
|
|
|
let lines = data.split("\n"); // string[]
|
|
|
|
let authtoken = null; // string || null
|
|
|
|
|
|
|
|
// find the auth token
|
|
|
|
lines.forEach((element) => {
|
|
|
|
const [name, value] = element.split("=");
|
|
|
|
if (name === "NGROK_AUTHTOKEN") authtoken = value;
|
|
|
|
});
|
|
|
|
|
|
|
|
// No token found
|
|
|
|
if (!authtoken) {
|
|
|
|
console.error("Setup NGROK_AUTHTOKEN in your .env file");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start ngrok
|
|
|
|
(async function () {
|
|
|
|
const proto = options?.proto ?? "http";
|
|
|
|
const addr = options?.addr ? options.addr : 3000;
|
2021-11-10 16:35:27 -06:00
|
|
|
|
|
|
|
// Start ngrok
|
2021-11-10 17:50:49 -06:00
|
|
|
try {
|
|
|
|
const url = await ngrok.connect({ authtoken, proto, addr });
|
2021-11-10 17:24:03 -06:00
|
|
|
|
2021-11-10 17:50:49 -06:00
|
|
|
// Rebuild lines with the new url
|
2021-11-10 18:25:28 -06:00
|
|
|
let found = false;
|
2021-11-10 17:50:49 -06:00
|
|
|
lines = lines.map((element) => {
|
|
|
|
const [name] = element.split("=");
|
2021-11-10 18:25:28 -06:00
|
|
|
if (name === "NGROK_SERVERHOST") {
|
|
|
|
found = true;
|
|
|
|
return `${name}=${url}`;
|
|
|
|
}
|
2021-11-10 17:50:49 -06:00
|
|
|
return element;
|
|
|
|
});
|
2021-11-10 17:24:03 -06:00
|
|
|
|
2021-11-10 18:25:28 -06:00
|
|
|
// Is this variable already in the .env, if not add it.
|
|
|
|
if (!found) {
|
|
|
|
lines.unshift(`NGROK_SERVERHOST=${url}`)
|
|
|
|
}
|
|
|
|
|
2021-11-10 17:50:49 -06:00
|
|
|
// convert back to string format
|
|
|
|
let writeData = "";
|
|
|
|
lines.forEach((element) => {
|
|
|
|
writeData += element + "\n";
|
|
|
|
});
|
|
|
|
writeData = writeData.slice(0, writeData.length - 1);
|
2021-11-10 17:24:03 -06:00
|
|
|
|
2021-11-10 17:50:49 -06:00
|
|
|
// Write the new .env file
|
|
|
|
fs.writeFile(dotenvFile, writeData, (err) => {
|
|
|
|
if (err) {
|
|
|
|
console.error(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
console.clear();
|
|
|
|
console.log(`Started ngrok: protocol '${proto}', addr '${addr}'`);
|
|
|
|
console.log(`Your current ngrok url is ${url}.`);
|
|
|
|
console.log(`Your .env file has been updated.`);
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err.message);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2021-11-10 17:24:03 -06:00
|
|
|
})();
|
2021-11-10 16:35:27 -06:00
|
|
|
});
|