Git Product home page Git Product logo

gen-env-types's Introduction

gen-env-types

Takes your .env file as input

SESSION_SECRET=asdjpfowqip
STRIPE_ACCESS_TOKEN=qoi120wqe

And generates a .d.ts file

declare namespace NodeJS {
  export interface ProcessEnv {
    SESSION_SECRET: string;
    STRIPE_ACCESS_TOKEN: string;
  }
}

Include the generated file in your tsconfig.json if not already:

{
  "include": [
    "./env.d.ts"
  ]
}

Now process.env.SESSION_SECRET will autocomplete and be type-safe.

Customize

gen-env-types respects changes made to generated files, meaning you can overwrite .env.example and env.d.ts values, this can be helpful if you want a union type:

declare namespace NodeJS {
  export interface ProcessEnv {
    NODE_ENV: "development" | "production";
  }
}

Or if you want to persist .env.example values:

PORT=3000

Usage

npx gen-env-types path/to/.env

Options

  -V, --version               Show version number
  -h, --help                  Show usage information
  -o, --types-output          Output name/path for types file | defaults to `env.d.ts`
  -e, --example-env-path      Path to save .env.example file
  -O,  --optional [vars]      Make some of the environment variables optional.
                              Accepts a list of environment variables to be made optional.
  -r, --rename-example-env    Custom name for .env example output file | defaults to `env.example` if omitted
  -k, --keep-comments         Keep comments/blank lines in .env example output file | defaults to false if omitted.
                              Not accepting the value. When specified, it will be true.
  -b,  --browser              Output browser compatible types instead of NodeJS["ProcessEnv"] | defaults to false if omitted.                            

Examples with options

npx gen-env-types .env -o src/types/env.d.ts -e .
# With custom example env file name
npx gen-env-types .env -o src/types/env.d.ts -e . -r .env.test

gen-env-types's People

Contributors

alavkx avatar benawad avatar ekundayo-ab avatar guidodizi avatar luckywindsck avatar malinskibeniamin avatar n1md7 avatar timthedev07 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  avatar  avatar  avatar

gen-env-types's Issues

Feature: Tolerate comments in env.example

I know this can be arguable but if there was an option on whether or not comment should be kept would be nice.

My use case is following:

In my projects, I already have env.example file as a placeholder for how the actual env file is supposed to look. Which contains comments as well to give more clarity on what it is about. When I run the type generator it basically overwrites itself. However, blank lines and comments are removed ๐Ÿ˜‘

For example:

Command

npx gen-env-types .env.example -o src/env.d.ts -e .

env.example

# Application port
PORT=8110

# Keycloak configuration
KC_URL="http://127.0.0.1:8200/auth"
KC_REALM="master"
KC_CLIENT_ID="sse-local"
KC_CLIENT_SECRET=

# RabbitMQ configuration
# Fully Qualified URL - when fully qualified URL is provided other options get ignored
RABBITMQ_URL="amqp://127.0.0.1:5672"
# Other options
RABBITMQ_HOST="127.0.0.1"
RABBITMQ_VHOST="/"
RABBITMQ_PORT=5672
RABBITMQ_USER=
RABBITMQ_PW=

Result

PORT=8110
KC_URL="http://127.0.0.1:8200/auth"
KC_REALM="master"
KC_CLIENT_ID="sse-local"
KC_CLIENT_SECRET=
RABBITMQ_URL="amqp://127.0.0.1:5672"
RABBITMQ_HOST="127.0.0.1"
RABBITMQ_VHOST="/"
RABBITMQ_PORT=5672
RABBITMQ_USER=
RABBITMQ_PW=

Possible flag could be
--keep-comments, -k
Defaulted to false

npx gen-env-types .env.example -o src/env.d.ts -k -e .

Optional requiring env var to exist

Currently I have some variables in my local dev env that I do not use in prod. Currently I manually change after running gen-env-types, If there was a way to set whether or not the requirement that a variable exist on a variable by variable basis, thatd be pretty sweet.

don't clober .env.example

Please either add a flag to omit generating the .env.example file, or better yet read the current example file and don't change variables that already exist.

For example in my .env.example file I have

DB_TYPE=postgres

but after running gen-env, it contains

DB_TYPE=

and so is no longer an accurate example.

And while this doesn't affect me personally, dotenv and dotenv-save allow you to use a custom .env filename, so you may want to make this configurable in this tool as well (unless -e already does this?)

.env comments

Hi,
Thanks for this usefull package ๐Ÿ‘Œ
I think if you just skip lines that starts with # gonna do the trick
Maybe I'll work on a PR
Peace

ignore blank lines

It would be great if the tool ignored blank lines.

If you have a blank line in your .env, or more commonly just end the file with a newline, then the generated .example file has an extra = line.

.env

DB_TYPE=postgres
DB_USER=myapp

generated .example file

DB_TYPE=
DB_USER=
=

notice the last line with just a =

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.