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

cogenv

v1.0.2

Published

๐Ÿš€ An efficient and flexible JavaScript library to manage environment variables

Downloads

3

Readme

cogenv

An efficient and flexible JavaScript library to manage environment variables

๐Ÿ“ฆ Installation

# For npm
npm install --save cogenv
# For yarn
yarn add cogenv

๐Ÿš€ Get Started

Create an .env file in the root directory of your project. Add environment-specific variables on new lines in the form of NAME=VALUE. For example, NAME=VALUE:

APP_NAME=Cogenv
APP_PORT:string=3000
APP_PORT_NUMBER:number=3000
APP_URL=http://localhost:${APP_PORT}

# For Database
DB->dialect=mysql
DB->port=336
DB->port_string:string=336
DB->localhost=localhost
DB->user=root
DB->password=cogenv_password
DB->logging=true
DB->database=cogenv_database
DB->URL=http://localhost:${DB.port}

cogenv will magically transform into the following

{
   "APP_NAME": "Cogenv",
   "APP_PORT": "3000",
   "APP_PORT_NUMBER": 3000,
   "APP_URL": "http://localhost:3000",
   "DB": {
      "dialect": "mysql",
      "port": 336,
      "port_string": "336",
      "localhost": "localhost",
      "user": "root",
      "password": "cogenv_password",
      "logging": true,
      "database": "cogenv_database",
      "URL": "http://localhost:336"
   }
}

As soon as possible in your application, require and configure the cogenv.

// Javascript
require('cogenv').Config();

// ES6+ / Typescript
import Config from 'cogenv';
// Initializing
Config();

cogenv now has the keys and values you defined in your cog.env file.

import { env } from '@cogenv/core';

env('DB');
// or
cog.env.DB;
{
   "dialect": "mysql",
   "port": 336,
   "localhost": "localhost",
   "user": "root",
   "password": "cogenv_password",
   "logging": true,
   "database": "cogenv_database",
   "URL": "http://localhost:336",
   "port_string": "336"
}

๐ŸŽจ Typed

cogenv integrates typing to convert your variables to different types from the .env file

  • String
  • Boolean
  • Number
# This will return as type number => 3000
APP_NUMBER=3000
# This will return of type string => "3000"
APP_NUMBER:string=3000

# boolean type => true
APP_LOG=yes
# boolean type => "yes"
APP_LOG:string=yes
# boolean type => false
APP_LOG=false
# string type => "false"
APP_LOG:string=false
# string type => "true"
APP_LOG="true"
# boolean type => true
APP_LOG:boolean="true"

# type number => 8080
APP_PORT=8080
# type string => "8080"
APP_PORT:string=8080

๐ŸŽ‰ Method env

the env method allows you to obtain the environment variables; this method uses the dotfast package and its functionalities are available in this method

import { env } from '@cogenv/core';
env('APP_NAME'); //=> Cogenv
env('APP_URL'); //=> http://localhost:3000

to obtain from the objects is as follows

env('DB.dialect'); //=> mysql
env('DB.port'); //=> 336
env('DB.logging'); //=> true

from dotfast, to return some data I need, for example

  • I need the application port and the database name

this will return an object

env({
   PORT: 'APP_PORT',
   DB_NAME: 'DB.database',
});
/*
{
   PORT: 3000,
   DB_NAME: "cogenv_database"
}
*/

let's try an array

env(['APP_PORT', 'DB.database']);
/*
[
   3000,
   "cogenv_database"
]
*/

Options

  • Path: You may specify a custom path if your file containing environment variables is located elsewhere.
  • default: path.resolve(process.cwd(), '.env')
Config({
   path: '/custom/path/to/.env',
});
  • Encoding: You may specify the encoding of your file containing environment variables.
  • default: utf8
Config({
   encoding: 'latin1',
});
  • Logging: you can manipulate the messages on the console
  • default: true
Config({
   logging: false,
});
  • InterpolatePrefix: allows you to customize the interpolation prefix
  • default: $
Config({
   interpolatePrefix: '#',
});
  • TypedOptions: allows you to add typing options, such as mode (customized, auto)
  • default: {mode: "auto"}
Config({
   typedOptions: {
      mode: 'customized',
   },
});

Parse

The engine that analyzes the content of your file containing the environment variables is available for use. It accepts a string or buffer and returns an object with the analyzed keys and values.

import { Parse } from '@cogenv/core';
const buf = Buffer.from('BASIC=basic');
const config = Parse(buf); // will return an object
console.log(typeof config, config); // object { BASIC : 'basic' }

โญ Support for

cogenv is an open source project licensed by MIT. You can grow thanks to the sponsors and the support of the amazing sponsors. If you want to join them, contact me here.

๐ŸŽฉ Stay in touch

Contributors

Thanks to the wonderful people who collaborate with me !

๐Ÿ“œ License

cogenv under License MIT.