Skip to content

Commit

Permalink
feat: use debug module to help debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
dbstratta committed Mar 1, 2019
1 parent 250d2d9 commit 14668b1
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 2 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ to _your_ definition of valid. See [how to use it](#usage).
- [FAQ](#faq)
- [Where should I call `makeEnv` in my application?](#where-should-i-call-makeenv-in-my-application)
- [Does it support changing env variables dynamically?](#does-it-support-changing-env-variables-dynamically)
- [Can I use the `debug` module with `environment`?](#can-i-use-the-debug-module-with-environment)
- [Can I have more than one env object per application?](#can-i-have-more-than-one-env-object-per-application)
- [Node.js support](#nodejs-support)
- [Maintainers](#maintainers)
Expand Down Expand Up @@ -392,6 +393,10 @@ No, when you create an env object it will read the value of
changes to `process.env`, it will not be reflected in the
env object.

### Can I use the [`debug`](https://github.com/visionmedia/debug) module with `environment`?

Yes! Set `DEBUG=environment`.

### Can I have more than one env object per application?

Yes! You can have as many env objects as you want!
Expand Down
2 changes: 1 addition & 1 deletion babel.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = api => {
const babelEnv = api.env();

const debug = !!process.env.DEBUG;
const debug = !!process.env.DEBUG_BABEL;
const useBuiltIns = false;

const presets = [
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"scripts": {
"build": "yarn build-lib && yarn build-types",
"build-lib": "cross-env NODE_ENV=production babel --extensions \".ts\" --out-dir dist src",
"build-lib": "cross-env NODE_ENV=production babel --source-maps --extensions \".ts\" --out-dir dist src",
"build-types": "tsc --emitDeclarationOnly",
"prebuild": "yarn clean-dist",
"clean-dist": "rimraf dist",
Expand All @@ -29,6 +29,7 @@
"types": "dist/index.d.ts",
"private": false,
"dependencies": {
"debug": "^4.1.1",
"validator": "^10.11.0"
},
"devDependencies": {
Expand All @@ -42,6 +43,7 @@
"@commitlint/travis-cli": "7.5.0",
"@semantic-release/changelog": "3.0.2",
"@semantic-release/git": "7.0.8",
"@types/debug": "^4.1.2",
"@types/jest": "24.0.0",
"@types/node": "10.12.24",
"@types/validator": "10.9.0",
Expand Down
5 changes: 5 additions & 0 deletions src/debug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import debug from 'debug';

const namespace = 'environment';

export const logDebug = debug(namespace);
5 changes: 5 additions & 0 deletions src/environment.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import EnvironmentVariableError from './EnvironmentVariableError';
import { Parser } from './parsers';
import { logDebug } from './debug';

export type Env<TSchemaData> = {
[TKey in keyof Schema<TSchemaData>]: TSchemaData[TKey]
Expand Down Expand Up @@ -49,12 +50,16 @@ export function makeEnv<TSchemaData extends { [key: string]: any }>(
schema: Schema<TSchemaData>,
processEnv: NodeJS.ProcessEnv = process.env,
): Env<TSchemaData> {
logDebug('making env object...');

const env = Object.entries(schema).reduce((acc, [key, schemaEntry]) => {
const value = getValue(key, schemaEntry as any, processEnv);

return { ...acc, [key]: value };
}, {}) as Env<TSchemaData>;

logDebug('env object ready: %o', env);

return env;
}

Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,11 @@
into-stream "^4.0.0"
lodash "^4.17.4"

"@types/debug@^4.1.2":
version "4.1.2"
resolved "https://registry.yarnpkg.com/@types/debug/-/debug-4.1.2.tgz#84824e9259fc583dd9385635738359c9582f7f82"
integrity sha512-jkf6UiWUjcOqdQbatbvOm54/YbCdjt3JjiAzT/9KS2XtMmOkYHdKsI5u8fulhbuTUuiqNBfa6J5GSDiwjK+zLA==

"@types/jest@24.0.0":
version "24.0.0"
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-24.0.0.tgz#848492026c327b3548d92be0352a545c36a21e8a"
Expand Down

0 comments on commit 14668b1

Please sign in to comment.