building out cli arguments
This commit is contained in:
parent
95bbf76997
commit
71b50b374b
57
ngrok.js
57
ngrok.js
|
@ -1,50 +1,62 @@
|
||||||
const ngrok = require('ngrok');
|
const ngrok = require("ngrok");
|
||||||
const fs = require('fs');
|
const fs = require("fs");
|
||||||
const { exec } = require("child_process");
|
const { exec } = require("child_process");
|
||||||
|
|
||||||
const dotenvFile = '../.env'
|
const dotenvFile = "../.env";
|
||||||
|
|
||||||
|
const optionDefinitions = [
|
||||||
|
{ name: "proto", type: String },
|
||||||
|
{ name: "addr", type: Number },
|
||||||
|
];
|
||||||
|
|
||||||
|
const commandLineArgs = require("command-line-args");
|
||||||
|
const options = commandLineArgs(optionDefinitions);
|
||||||
|
|
||||||
|
console.log(options);
|
||||||
|
|
||||||
// Read the .env file
|
// Read the .env file
|
||||||
fs.readFile(dotenvFile, 'utf8', (err, data) => {
|
fs.readFile(dotenvFile, "utf8", (err, data) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert string to string array, split at newlines
|
// Convert string to string array, split at newlines
|
||||||
let lines = data.split('\n') // string[]
|
let lines = data.split("\n"); // string[]
|
||||||
let token = null; // string || null
|
let authtoken = null; // string || null
|
||||||
|
|
||||||
// find the auth token
|
// find the auth token
|
||||||
lines.forEach(element => {
|
lines.forEach((element) => {
|
||||||
const [name, value] = element.split('=');
|
const [name, value] = element.split("=");
|
||||||
if (name === 'NGROK_AUTHTOKEN')
|
if (name === "NGROK_AUTHTOKEN") authtoken = value;
|
||||||
token = value;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// No token found
|
// No token found
|
||||||
if (!token) {
|
if (!authtoken) {
|
||||||
console.error("Setup NGROK_AUTHTOKEN in your .env file")
|
console.error("Setup NGROK_AUTHTOKEN in your .env file");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start ngrok
|
// Start ngrok
|
||||||
(async function () {
|
(async function () {
|
||||||
const url = await ngrok.connect({ authtoken: token, proto: 'http', addr: 3000 });
|
const proto = options?.proto ?? "http";
|
||||||
|
const addr = options?.addr ? options.addr : 3000;
|
||||||
|
|
||||||
|
// Start ngrok
|
||||||
|
const url = await ngrok.connect({ authtoken, proto, addr });
|
||||||
|
|
||||||
// Rebuild lines with the new url
|
// Rebuild lines with the new url
|
||||||
lines = lines.map(element => {
|
lines = lines.map((element) => {
|
||||||
const [name] = element.split('=');
|
const [name] = element.split("=");
|
||||||
if (name === 'NGROK_SERVERHOST')
|
if (name === "NGROK_SERVERHOST") return `${name}=${url}`;
|
||||||
return `${name}=${url}`
|
|
||||||
return element;
|
return element;
|
||||||
})
|
});
|
||||||
|
|
||||||
// convert back to string format
|
// convert back to string format
|
||||||
let writeData = ''
|
let writeData = "";
|
||||||
lines.forEach(element => {
|
lines.forEach((element) => {
|
||||||
writeData += element + '\n'
|
writeData += element + "\n";
|
||||||
})
|
});
|
||||||
writeData = writeData.slice(0, writeData.length - 1);
|
writeData = writeData.slice(0, writeData.length - 1);
|
||||||
|
|
||||||
// Write the new .env file
|
// Write the new .env file
|
||||||
|
@ -54,6 +66,7 @@ fs.readFile(dotenvFile, 'utf8', (err, data) => {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
console.clear();
|
console.clear();
|
||||||
|
console.log(`Started ngrok: protocol '${proto}', addr '${addr}'`)
|
||||||
console.log(`Your current ngrok url is ${url}.`);
|
console.log(`Your current ngrok url is ${url}.`);
|
||||||
console.log(`Your .env file has been updated.`);
|
console.log(`Your .env file has been updated.`);
|
||||||
});
|
});
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
"start": "node ngrok"
|
"start": "node ngrok"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"command-line-args": "^5.2.0",
|
||||||
"ngrok": "^4.2.2"
|
"ngrok": "^4.2.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue