0.3.2 • Published 3 months ago
normal-env v0.3.2
This project aimed to cover base requirements in work with NODE_ENV environment variable and it equivalent in the other platforms with convenient interface.
Installation:
With npm
:
$ npm i normal-env --save-prod ( --save-dev )
With yarn
:
$ yarn add normal-env [ --dev ]
Usage
/* file: db-cursor.ks */
import { Env } from 'normal-env';
const env = new Env();
const config = fs.readFileSync(`db.${env}.json`).toString();
const connection = db.createConnection({
...config,
debug: env.isDevelopment()
})
/* file: webpack.config.js */
import { Env } from 'normal-env';
const env = new Env();
export default {
mode: env.toWebpackMode(),
devtool: env.isDevelopment()
? 'cheap-module-source-map'
: null,
optimization: env.isDevelopment()
? {
minimize: true,
minimizer: [new TerserPlugin()]
}
: null
// ... rest of webpack configuration
};
What happined here?
- We import named class "Env" (not default exported). It will take data from your environment based on your system (web, node.js, deno or bun);
- Make an instance of this class;
- It can serialize to string so we put it in filename to read. (check config keys in this file to find possible variations);
- We put config to abstract database client connection method and extends it with debug property and check is environment in development as a value;
How it work from terminal?
$ node ./server.js
will set environment to default value "development"
$ set NODE_ENV=tst
$ node -r "normal-env" -p "new Env().toString()"
> test
will set environment to "test" value
$ set NODE_ENV=prod
$ node -r "normal-env" -p "new Env().toString()"
> production
will set environment to "production" value
$ set NODE_ENV=ci
$ node -r "normal-env" -p "new Env().toString()"
> ci
will set environment to "ci" value