Skip to content

Yuri-Lima/JsonToEnv

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

56 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Updates Test CI code style: prettier emoji-log npm npm License: MIT

Convert Json to .Env File

Supports for CommonJS (CJS) and ECMAScript (ESM).

⭐ Features

  1. Convert json to .env file.
  2. Create env.json file optionally.
  3. Overwrite existing .env file optionally.
  4. Instantes log messages to show the progress of the process, optionally.
  5. Accept up to 5 levels deep of Objects [nested objects]
  • INPUT Json File Example:
    {
        // First Level
        "port": 6666,
        "array": ["a", "b", "c"],
        "dbUri": {
            // Second Level
            "host": "127.0.0.1",
            "port": 666,
            "db": {
                // Third Level
                "name": "DB_X1",
                "array_2": ["1", "2", "3"],
                "user": {
                    // Fourth Level
                    "name": "ABC",
                    "password": null,
                    "role": {
                        // Fifth Level
                        "name": "ADMIN",
                        "permissions": ["READ", "WRITE", "DELETE"]
                    },
                    "number": 789
                },
                "password": 12345678
            }
        },
        "logLevel": true,
    }
  • 1.OUTPUT .Env File Example [Overwrite][optional]:
    PORT=6666
    ARRAY_0='a'
    ARRAY_1='b'
    ARRAY_2='c'
    DBURI_HOST='127.0.0.1'
    DBURI_PORT=666
    DBURI_DB_NAME='DB_X1'
    DBURI_DB_ARRAY_2_0='1'
    DBURI_DB_ARRAY_2_1='2'
    DBURI_DB_ARRAY_2_2='3'
    DBURI_DB_USER_NAME='ABC'
    DBURI_DB_USER_PASSWORD=null
    DBURI_DB_USER_ROLE_NAME='ADMIN'
    DBURI_DB_USER_ROLE_PERMISSIONS=READ,WRITE,DELETE
    DBURI_DB_USER_NUMBER=789
    DBURI_DB_PASSWORD=12345678
    LOGLEVEL=true
  • 2.OUTPUT env.json File Example [Optional][optional]:
    {
    "PORT": 6666,
    "ARRAY_0": "a",
    "ARRAY_1": "b",
    "ARRAY_2": "c",
    "DBURI_HOST": "127.0.0.1",
    "DBURI_PORT": 666,
    "DBURI_DB_NAME": "DB_X1",
    "DBURI_DB_ARRAY_2_0": "1",
    "DBURI_DB_ARRAY_2_1": "2",
    "DBURI_DB_ARRAY_2_2": "3",
    "DBURI_DB_USER_NAME": "ABC",
    "DBURI_DB_USER_PASSWORD": null,
    "DBURI_DB_USER_ROLE_NAME": "'ADMIN'",
    "DBURI_DB_USER_ROLE_PERMISSIONS": [
        "READ",
        "WRITE",
        "DELETE"
    ],
    "DBURI_DB_USER_NUMBER": 789,
    "DBURI_DB_PASSWORD": 12345678,
    "LOGLEVEL": true
    }

Installation

npm install --save-dev dynamic.envs

πŸš€ Getting started

Setup

ESM example:

import JsonToEnv from "dynamic.envs";

const set = new JsonToEnv({
    readFileFrom: path.join(__dirname, "..","FILE_NAME.json"),
    saveFileTo: path.join(__dirname, "..", ".env")
  }, {
    overWrite_Original_Env: true, // if you dont want to overwrite your original .env file, set this to true
    createJsonFile: true, // If you want to create a new json file, set this to true
    createEnvFile: true, // If you want to create a new env file, set this to true
    log: true, // If you want to log the result, set this to true
  });
set.setEnv(); // This will create a new .env file depending on the options you set

CJS example:

const JsonToEnv = require("dynamic.envs").default;

const Set = {
    readFileFrom: path.join(__dirname, "..","FILE_NAME.json"),
    saveFileTo: path.join(__dirname, "..", ".env")
  }
const Options = {
    overWrite_Original_Env: false, // if you dont want to overwrite your original .env file, set this to true
    createJsonFile: false, // If you want to create a new json file, set this to true
    createEnvFile: true, // If you want to create a new env file, set this to true
    log: false // If you want to log the result, set this to true
  }
const sett = new JsonToEnv(Set,Options);
set.setEnv(); // This will create a new .env file depending on the options you set

Setting up Source and Destination

Option Type Required Description
readFileFrom String Yes The path to the json file you want to convert to .env file.
saveFileTo String Yes The path to the .env file you want to create.

Options

Option Type Default Description
overWrite_Original_Env Boolean False If True, it will overWrite the orinal .env file
createEnvFile Boolean True If True and overWrite_Original_Env is False is will create a new .env file however if overWrite_Original_Env is True it will oveWrite the original
createJsonFile Boolean False If True, it will create a new .env.json file
log Boolean False If True, it will log the result to the console

🚨 ALERT

  • The interface Options_Set_Env has an important property called overWrite_Original_Env.
  • By default this property is set to false. For safety reasons.
  • This property once set to true will OVER WRITE the ORIGINAL .env file.(depends on the .env path)
  • It's very important to keep set it as false if you want to keep the original .env file.

⚠️ WARNING

  1. For while this is a very simple converter.
  2. Thats a brand new project. So, still in basics development.
  3. It does not check if the json file is valid.
  4. It does not check if the json file is nested.
  5. It does not check if the json file is empty.
  6. It does not find .env file automatically.[YET]

πŸ†• Features Coming Soon

  • Accept nested arrays.
  • Accept nested objects of arrays.
  • Find .env file automatically.

πŸ€“ What technology are we using?

πŸ’ͺ Motivation

  • I used to use those tools below to deal with .env files but I found them to be too boring sometimes annoing then I decided to use JSON instead, because I think they are more flexible, readble and easy to use. However, I was not able to find a way to convert JSON to .env file automatically. So I decided to write a tool to do that. I hope you will like it.

If you love this project, please consider giving me a ⭐.

πŸ—£οΈ Discussion

Issues Page

  • You can discuss or ask for help in issues.