Skip to content

Commit

Permalink
improve args fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
Procaseycash committed Feb 1, 2024
1 parent 66b15bf commit 9741d0f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
28 changes: 16 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
const fs = require("fs");

const getArg = (name) => {
if (!name) return;
const getArgs = (argName = null) => {
const args = {};
const argvs = process.argv;
for (let arg of argvs) {
if (arg && arg.toLowerCase().includes(name.toLowerCase())) {
const pos = arg.indexOf("=");
const pos = arg.indexOf("=");
if (pos > -1) {
const key = arg.substring(0, pos);
const value = arg.substring(pos + 1);
return [key, value];
args[key] = value;
}
}
return [];
if (argName) return args[argName];
return args;
};

const getDir = (path) => {
Expand Down Expand Up @@ -108,13 +109,15 @@ const convertEnvToJson = (path) => {
};

const convertEnvJsonViaCMD = () => {
const filePath = getArg("--file")[1]?.trim();
const envString = getArg("--env")[1]?.trim();
const isConsole = getArg("--csl")[1]?.trim();
const isWriteToRoot = getArg("--wtr")[1]?.trim();
const outputPath = getArg("--out")[1]?.trim();
const argEnv = getArgs();
const filePath = argEnv["--file"]?.trim();
const envString = argEnv["--env"]?.trim();
const isConsole = argEnv["--csl"]?.trim();
const isWriteToRoot = argEnv["--wtr"]?.trim();
const outputPath = argEnv["--out"]?.trim();

if (!filePath && !envString) return;

const toEnv = filePath?.endsWith(".json");
let result = { location: null, jsonEnv: null };
if (toEnv) {
Expand Down Expand Up @@ -156,7 +159,8 @@ const convertEnvJsonViaCMD = () => {
module.exports = {
envFromPathToJson: convertEnvToJson,
envFromStringToJson: convertEnvStringToJson,
getArg,
getArg: (name) => getArgs(name),
args: getArgs(),
jsonFromPathToEnv: convertJsonToEnv,
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "envtwojson",
"version": "1.1.6",
"version": "1.1.7",
"description": "This package allows env-to-json and json-to-env on CMD and code level usage",
"main": "index.js",
"private": false,
Expand Down

0 comments on commit 9741d0f

Please sign in to comment.