From 71b50b374b9e98b192989b7d67fd793e7329cce2 Mon Sep 17 00:00:00 2001 From: nightness Date: Wed, 10 Nov 2021 17:24:03 -0600 Subject: [PATCH] building out cli arguments --- ngrok.js | 109 ++++++++++++++++++++++++++++----------------------- package.json | 11 +++--- 2 files changed, 67 insertions(+), 53 deletions(-) diff --git a/ngrok.js b/ngrok.js index 7098798..9ac3a2a 100644 --- a/ngrok.js +++ b/ngrok.js @@ -1,61 +1,74 @@ -const ngrok = require('ngrok'); -const fs = require('fs'); +const ngrok = require("ngrok"); +const fs = require("fs"); 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 -fs.readFile(dotenvFile, 'utf8', (err, data) => { - if (err) { - console.error(err); - return; - } +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 + // 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') - token = value; - }); + // find the auth token + lines.forEach((element) => { + const [name, value] = element.split("="); + if (name === "NGROK_AUTHTOKEN") authtoken = value; + }); - // No token found - if (!token) { - console.error("Setup NGROK_AUTHTOKEN in your .env file") - return; - } + // 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; // Start ngrok - (async function () { - const url = await ngrok.connect({ authtoken: token, proto: 'http', addr: 3000 }); + const url = await ngrok.connect({ authtoken, proto, addr }); - // Rebuild lines with the new url - lines = lines.map(element => { - const [name] = element.split('='); - if (name === 'NGROK_SERVERHOST') - return `${name}=${url}` - return element; - }) + // 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); + // 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.`); - }); - })(); + // 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.`); + }); + })(); }); diff --git a/package.json b/package.json index 5355dde..c7b1a22 100644 --- a/package.json +++ b/package.json @@ -4,12 +4,13 @@ "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" - }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "debug": "node --inspect ngrok.js", + "start": "node ngrok" + }, "dependencies": { + "command-line-args": "^5.2.0", "ngrok": "^4.2.2" } }