initial commit
This commit is contained in:
parent
48d24dcfc2
commit
e6007ef6a4
|
@ -0,0 +1,3 @@
|
|||
node_modules/
|
||||
yarn.lock
|
||||
package.json.lock
|
|
@ -1 +1,3 @@
|
|||
# ngrok-to-dotenv
|
||||
|
||||
ngrok-to-dotenv is a node cli tool that you can use with projects that need ngrok. You clone it into your project's folder, and setup a package.json script to call "cd ngrok-to-dotenv && node ngrok"
|
|
@ -0,0 +1,61 @@
|
|||
const ngrok = require('ngrok');
|
||||
const fs = require('fs');
|
||||
const { exec } = require("child_process");
|
||||
|
||||
const dotenvFile = '../.env'
|
||||
|
||||
// Read the .env file
|
||||
fs.readFile(dotenvFile, 'utf8', (err, data) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
return;
|
||||
}
|
||||
|
||||
// Convert string to string array, split at newlines
|
||||
let lines = data.split('\n') // string[]
|
||||
let token = null; // string || null
|
||||
|
||||
// find the auth token
|
||||
lines.forEach(element => {
|
||||
const [name, value] = element.split('=');
|
||||
if (name === 'NGROK_AUTHTOKEN')
|
||||
token = value;
|
||||
});
|
||||
|
||||
// No token found
|
||||
if (!token) {
|
||||
console.error("Setup NGROK_AUTHTOKEN in your .env file")
|
||||
return;
|
||||
}
|
||||
|
||||
// Start ngrok
|
||||
(async function () {
|
||||
const url = await ngrok.connect({ authtoken: token, proto: 'http', addr: 3000 });
|
||||
|
||||
// Rebuild lines with the new url
|
||||
lines = lines.map(element => {
|
||||
const [name] = element.split('=');
|
||||
if (name === 'NGROK_SERVERHOST')
|
||||
return `${name}=${url}`
|
||||
return element;
|
||||
})
|
||||
|
||||
// convert back to string format
|
||||
let writeData = ''
|
||||
lines.forEach(element => {
|
||||
writeData += element + '\n'
|
||||
})
|
||||
writeData = writeData.slice(0, writeData.length - 1);
|
||||
|
||||
// Write the new .env file
|
||||
fs.writeFile(dotenvFile, writeData, (err) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
return;
|
||||
}
|
||||
console.clear();
|
||||
console.log(`Your current ngrok url is ${url}.`);
|
||||
console.log(`Your .env file has been updated.`);
|
||||
});
|
||||
})();
|
||||
});
|
|
@ -0,0 +1,15 @@
|
|||
{
|
||||
"name": "ngrok-to-dotenv",
|
||||
"version": "1.0.0",
|
||||
"description": "ngrok start-up wrapper that uses a dotenv file to get the authtoken and save the https address",
|
||||
"main": "ngrok.js",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"debug": "node --inspect ngrok.js",
|
||||
"start": "node ngrok"
|
||||
},
|
||||
"dependencies": {
|
||||
"ngrok": "^4.2.2"
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue