GithubHelp home page GithubHelp logo

ladjs / dotenv-parse-variables Goto Github PK

View Code? Open in Web Editor NEW
123.0 1.0 13.0 301 KB

Parse dotenv files for Boolean, Array, and Number variable types, built for Lad

Home Page: https://lad.js.org

License: MIT License

JavaScript 100.00%
parse dotenv env twelve factor variables vars environment config dotfile

dotenv-parse-variables's People

Contributors

dcabreratp avatar ewrogers avatar jcblw avatar madoke avatar niftylettuce avatar rodw1995 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

dotenv-parse-variables's Issues

Allow inline comments

Looks like inline comments in .env aren't handled?

e.g a .env containing the following:

LIMITER_MAX_CALL=50 # limit number of calls to this value per LIMITER_TIME_INTERVAL

returns in env

{
  ...
  LIMITER_MAX_CALL="50 # limit number of calls to this value per LIMITER_TIME_INTERVAL"
  ...
}

Ideally everything from "#" onward should be stripped when parsing.

Example from readme.md is broken

import dotenv from 'dotenv';
import dotenvParseVariables from 'dotenv-parse-variables';

let env = dotenv.config({});
env = dotenvParseVariables(env);
➜ gulp --tasks
[10:13:58] Requiring external module babel-register
/Users/…/Sites/…/node_modules/dotenv-parse-variables/lib/index.js:39
  if (value.toLowerCase() === 'true' || value.toLowerCase() === 'false') {
            ^

TypeError: value.toLowerCase is not a function
    at parseKey (/Users/…/Sites/…/node_modules/dotenv-parse-variables/src/index.js:30:13)
    at /Users/…/Sites/…/node_modules/dotenv-parse-variables/src/index.js:10:35
    at Array.forEach (native)
    at exports.default (/Users/…/Sites/…/node_modules/dotenv-parse-variables/src/index.js:8:20)
    at Object.<anonymous> (/Users/…/Sites/…/gulp-config.js:5:7)
    at Module._compile (module.js:569:30)
    at loader (/Users/…/Sites/…/node_modules/babel-register/lib/node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/…/Sites/…/node_modules/babel-register/lib/node.js:154:7)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)

How to parse string with only numbers

If my .env has a variable that should be a string, but consists only of digits:

AWS_ACCESS_KEY_ID="123"

It will get parsed to number. How may I precent this? Are the quotation marks ignored?

parse object

Have you considered supporting parsing objects?

e.g. SOME_VARIABLE={"test": "hello"}, currently, we get SOME_VARIABLE: '{"test": "hello"}', but since that string is JSON parsible, would be nice to parse it into an object.

process.env not parsed

What's wrong with boolean parse?

require('dotenv').load();

const dotenvParseVariables = require('dotenv-parse-variables');

console.log(process.env);
process.env = dotenvParseVariables(process.env);
console.log(process.env);

result:

{
  "DEV": "true",
  "WWW": "http://localhost:3000",
  "PORT": "3000",
  "MONGODB_DSN": "mongodb://127.0.0.1/test",
  "MONGODB_DEBUG": "true"
}

{
  "DEV": "true",
  "WWW": "http://localhost:3000",
  "PORT": "3000",
  "MONGODB_DSN": "mongodb://127.0.0.1/test",
  "MONGODB_DEBUG": "true"
}

Array with just one variable

Currently when creating an array with just one item, it creates an array of two:

CORS_ORIGINS=myurl.com,
=> ['myurl.com', '']

It would be nice to be able to create an array even if there is just one item.

Variables are not being assigned/parsed into process.env

I'm probably doing something really wrong, but i can't find what...

import dotenv from 'dotenv'
import dotenvParseVariables from 'dotenv-parse-variables'

let env = dotenv.config()
if (env.error || !env.parsed) throw env.error
env = dotenvParseVariables(env.parsed, { assignToProcessEnv: true, overrideProcessEnv: true })
console.log(process.env)

My .env file:

PORT=3000
JWT_SECRET=token

What I got from process.env:

{
  PORT: '3000', // String
  JWT_SECRET: 'token'
}

What I expect:

{
  PORT: 3000, // Number
  JWT_SECRET: 'clinica'
}

What I'm doing wrong?

String with a comma

I have a token to access a third party API. This token contains a comma ",". In my .env file the token is saved as:

TOKEN=abcde,fghij

When I parse the .env variables with dotenv-parse-variables, the token is parsed as an array, e.g.
['abcde', 'fghij'] But of course I just want a string. Is this possible? Also with an asterisk, I am not able to get the desired result. Can you please help me?

Ignore functions in parsed config objects

Great library!

I'm using this with react-native-config, which includes a helper function in its configuration object. This function must first be manually stripped before passing the config to dotenv-parse-variables, otherwise it will throw an error: TypeError: value.indexOf is not a function.

As a convenience, it would be great if dotenv-parse-variables ignored any functions in the object being parsed.

Thanks!

[fix] long string number parsed incorrectly

Describe the bug

Node.js version: 20

Description: Big numbers like "12345678901234567" are parsed incorrectly to number

Actual behavior

"12345678901234567" becomes 12345678901234568 (number)

Expected behavior

"12345678901234567" becomes 12345678901234567 (bigint)

or

"12345678901234567" stays as "12345678901234567" (string)

Code to reproduce

.env:
ENV_VAR="12345678901234567"

Checklist

  • I have searched through GitHub issues for similar issues.
  • I have completely read through the README and documentation.
  • I have tested my code with the latest version of Node.js and this package and confirmed it is still not working.

Type-cast of variables not written to process.env

The README gives an example with console.log(process.env); which outputs type-casted variables e.g. BAZ: 2 vs BAZ: '2'.

This is not the case - any variable written back to process.env will be output as a string, and it does not appear possible to set and then read a type-casted variable from process.env.

The assignToProcessEnv and overrideProcessEnv work, they set the value to process.env as per the logic, but the type is lost.

You need to use the output of dotenvExtended.load() to see the type-casted variables, however this does not contain anything existing from process.env - so there is no place to get an entire process.env variable list with type-cast.

The way i have achieved this is by passing a copy of process.env into the package e.g.

const loadedDotEnv = dotenvExtended.load({
  silent: false,
  errorOnMissing: true
});
const env = require('dotenv-parse-variables')(Object.assign({}, process.env));

console.log(env);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.