Skip to content

Commit

Permalink
Added support for automatic APP_ROOT environment variable.
Browse files Browse the repository at this point in the history
  • Loading branch information
swernerx committed Jun 9, 2018
1 parent 8c5bf41 commit 38a54d8
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 48 deletions.
5 changes: 2 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,15 @@ Universal DotEnv - A Robust Environment Configuration for Universal Applications
## Features

- Supports loading `.env` files with overriding between different `NODE_ENV` settings and `BUILD_TARGET` configurations.
- Automatically adds a `APP_ROOT` which points to the absolute root folder.
- Serializes all `APP_` prefixed environment variables for usage as `raw`, `stringified` or `webpack` (for `DefinePlugin`)
- Supports variable expansion between different settings.
- Allows local overrides using files which use a ".local" postfix.


## All Strings

It's always a good concept to keep in mind that environment variables
are always strings. Even if you define `true` or numbers like `100` they are
still send over as strings. See also: https://github.com/motdotla/dotenv/issues/51
It is important to remember that all environment variables are always stored as strings. Even numbers and booleans. The casting to other types must therefore take place in the application code. See also: https://github.com/motdotla/dotenv/issues/51


## Variables
Expand Down
18 changes: 4 additions & 14 deletions src/__snapshots__/build.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Also serializes BUILD_TARGET 1`] = `
exports[`Serializes BUILD_TARGET 1`] = `
Object {
"APP_ROOT": Any<String>,
"APP_TEST_IS_CLIENT": "true",
"APP_TEST_OVERRIDE": "overridden",
"APP_TEST_PATH": "one/two/three.js",
Expand All @@ -10,24 +11,13 @@ Object {
}
`;
exports[`Also serializes BUILD_TARGET 2`] = `
exports[`Serializes BUILD_TARGET 2`] = `
Object {
"APP_ROOT": Any<String>,
"APP_TEST_IS_CLIENT": "\\"true\\"",
"APP_TEST_OVERRIDE": "\\"overridden\\"",
"APP_TEST_PATH": "\\"one/two/three.js\\"",
"BUILD_TARGET": "\\"client\\"",
"NODE_ENV": "\\"test\\"",
}
`;

exports[`Also serializes BUILD_TARGET 3`] = `
Object {
"process.env": Object {
"APP_TEST_IS_CLIENT": "\\"true\\"",
"APP_TEST_OVERRIDE": "\\"overridden\\"",
"APP_TEST_PATH": "\\"one/two/three.js\\"",
"BUILD_TARGET": "\\"client\\"",
"NODE_ENV": "\\"test\\"",
},
}
`;
25 changes: 4 additions & 21 deletions src/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

exports[`Ignores non app-specific settings 1`] = `
Object {
"APP_ROOT": Any<String>,
"APP_TEST_OVERRIDE": "overridden",
"APP_TEST_PATH": "one/two/three.js",
"NODE_ENV": "test",
Expand All @@ -10,25 +11,17 @@ Object {
exports[`Ignores non app-specific settings 2`] = `
Object {
"APP_ROOT": Any<String>,
"APP_TEST_OVERRIDE": "\\"overridden\\"",
"APP_TEST_PATH": "\\"one/two/three.js\\"",
"NODE_ENV": "\\"test\\"",
}
`;
exports[`Ignores non app-specific settings 3`] = `
Object {
"process.env": Object {
"APP_TEST_OVERRIDE": "\\"overridden\\"",
"APP_TEST_PATH": "\\"one/two/three.js\\"",
"NODE_ENV": "\\"test\\"",
},
}
`;

exports[`Supports manually defined envs 1`] = `
Object {
"APP_DATA": "yellow",
"APP_ROOT": Any<String>,
"APP_TEST_OVERRIDE": "overridden",
"APP_TEST_PATH": "one/two/three.js",
"NODE_ENV": "test",
Expand All @@ -38,19 +31,9 @@ Object {
exports[`Supports manually defined envs 2`] = `
Object {
"APP_DATA": "\\"yellow\\"",
"APP_ROOT": Any<String>,
"APP_TEST_OVERRIDE": "\\"overridden\\"",
"APP_TEST_PATH": "\\"one/two/three.js\\"",
"NODE_ENV": "\\"test\\"",
}
`;

exports[`Supports manually defined envs 3`] = `
Object {
"process.env": Object {
"APP_DATA": "\\"yellow\\"",
"APP_TEST_OVERRIDE": "\\"overridden\\"",
"APP_TEST_PATH": "\\"one/two/three.js\\"",
"NODE_ENV": "\\"test\\"",
},
}
`;
17 changes: 13 additions & 4 deletions src/build.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
process.env.BUILD_TARGET = "client"

const snapshotOpts = {
APP_ROOT: expect.any(String)
}

/* eslint-disable import/no-commonjs */
// We can't use ESM when relying on the fact the the env from the top is correctly respected.
const api = require(".")

test("Also serializes BUILD_TARGET", () => {
test("Serializes BUILD_TARGET", () => {
const { raw, stringified, webpack } = api.getEnvironment()
expect(raw).toMatchSnapshot()
expect(stringified).toMatchSnapshot()
expect(webpack).toMatchSnapshot()
expect(raw).toMatchSnapshot(snapshotOpts)
expect(stringified).toMatchSnapshot(snapshotOpts)
expect(webpack).toBeDefined()
})

test("Exports BUILD_TARGET", () => {
const { raw } = api.getEnvironment()
expect(raw.BUILD_TARGET).toBe("client")
})
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ export function getEnvironment() {
raw.BUILD_TARGET = BUILD_TARGET
}

// Add hint about root folder
raw.APP_ROOT = appRoot.get()

// Stringify all values so we can feed into Webpack DefinePlugin
const stringified = {}
const webpack = { "process.env": stringified }
Expand Down
26 changes: 20 additions & 6 deletions src/index.test.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
import { getEnvironment } from "."

const snapshotOpts = {
APP_ROOT: expect.any(String)
}

test("Sets up process.env", () => {
expect(process.env.APP_TEST_PATH).toBe("one/two/three.js")
expect(process.env.TEST_IGNORE).toBe("123")
})

test("Ignores non app-specific settings", () => {
const { raw, stringified, webpack } = getEnvironment()
expect(raw).toMatchSnapshot()
expect(stringified).toMatchSnapshot()
expect(webpack).toMatchSnapshot()
expect(raw).toMatchSnapshot(snapshotOpts)
expect(stringified).toMatchSnapshot(snapshotOpts)
expect(webpack).toBeDefined()
})

test("Supports manually defined envs", () => {
process.env.APP_DATA = "yellow"
const { raw, stringified, webpack } = getEnvironment()
expect(raw).toMatchSnapshot()
expect(stringified).toMatchSnapshot()
expect(webpack).toMatchSnapshot()
expect(raw).toMatchSnapshot(snapshotOpts)
expect(stringified).toMatchSnapshot(snapshotOpts)
expect(webpack).toBeDefined()
delete process.env.APP_DATA
})

test("Exports NODE_ENV", () => {
const { raw } = getEnvironment()
expect(raw.NODE_ENV).toBe("test")
})

test("Exports APP_ROOT", () => {
const { raw } = getEnvironment()
expect(raw.APP_ROOT).toMatch(/universal-dotenv$/)
})

0 comments on commit 38a54d8

Please sign in to comment.