npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

env2ts

v1.4.0

Published

Dead simple env file parsing for TypeScript

Downloads

231

Readme


env2ts is a CLI that parses your env file and generates a TypeScript file with all your variables in it.

Usage

It can be easily used with npx.

$ npx env2ts

This will look for an .env file in your current directory and will write to config.ts.

Options

| arg | required | description | default | | ----------------------- | -------- | ----------------------------------------------------------------------------------------------- | ------------- | | --in path/to/.env | no | Specify an .env file | ./.env | | --out path/to/file.ts | no | Specify the output file  | ./config.ts | | --watch | no | Watch the input file for changes and write the env variables automatically into the output file | no | | --raw | no | Print the output directly to the console | no |

Example

Consider the following .env

# secrets/.env

# Port
PORT=5000 # @team Do not change this!!

# AWS Config
ACCESS_KEY_ID = AF391430TEST
SECRET_ACCESS_KEY=ajg391mt1kf1036fh # Another comment

# API Config
CLIENT_ROOT=http://localhost:3000
TOKEN_SECRET=reallystrongsecret
# Keep it private
REFRESH_SECRET=anothersecretpass

Run env2ts

$ npx env2ts --in ./secrets/.env --out ./src/config/index.ts

env2ts will parse it, stripping all comments, blank lines and whitespaces.

The output will look something similar to this:

// src/config/index.ts

import * as dotenv from "dotenv";

dotenv.config();

export const PORT: string = process.env.PORT!;
export const ACCESS_KEY_ID: string = process.env.ACCESS_KEY_ID!;
export const SECRET_ACCESS_KEY: string = process.env.SECRET_ACCESS_KEY!;
export const CLIENT_ROOT: string = process.env.CLIENT_ROOT!;
export const TOKEN_SECRET: string = process.env.TOKEN_SECRET!;
export const REFRESH_SECRET: string = process.env.REFRESH_SECRET!;

Notes

env2ts will warn you before overwriting an existing output file

$ npx env2ts

? File 'config.ts' already exists, overwrite? › (y/N)

env2ts will prompt you to install dotenv if you happen to not have it already

$ npx env2ts

? Module dotenv is not installed, choose your package manager › - Use arrow-keys. Return to submit.
❯   npm
    yarn
    Do not install

Contributing

This package is still in development. Here's a rough todo of what's left:

  • [x] Add --watch mode
  • [ ] Add --js flag to allow non-TypeScript users to use it
  • [ ] Add --require flag to choose between require('dotenv').config() and ES6 Modules
  • [ ] Parse env variables to get the appropriate type (now we're using string for everything)

Feel free to open a pull request adding new features or fixing bugs, but PR to the dev branch.

For anything else, please open an issue.