diff --git a/__tests__/__fixtures__/dotenv-flow-explicit-local/.babelrc b/__tests__/__fixtures__/dotenv-flow-explicit-local/.babelrc new file mode 100644 index 0000000..2665d2e --- /dev/null +++ b/__tests__/__fixtures__/dotenv-flow-explicit-local/.babelrc @@ -0,0 +1,7 @@ +{ + "plugins": [ + ["../../../", { + "path": "__tests__/__fixtures__/dotenv-flow/.env" + }] + ] +} diff --git a/__tests__/__fixtures__/dotenv-flow-explicit-local/.env b/__tests__/__fixtures__/dotenv-flow-explicit-local/.env new file mode 100644 index 0000000..75a4caf --- /dev/null +++ b/__tests__/__fixtures__/dotenv-flow-explicit-local/.env @@ -0,0 +1 @@ +API_KEY=default-key diff --git a/__tests__/__fixtures__/dotenv-flow-explicit-local/.env.development b/__tests__/__fixtures__/dotenv-flow-explicit-local/.env.development new file mode 100644 index 0000000..360db36 --- /dev/null +++ b/__tests__/__fixtures__/dotenv-flow-explicit-local/.env.development @@ -0,0 +1 @@ +API_KEY=development-key diff --git a/__tests__/__fixtures__/dotenv-flow-explicit-local/.env.development.local b/__tests__/__fixtures__/dotenv-flow-explicit-local/.env.development.local new file mode 100644 index 0000000..a013e01 --- /dev/null +++ b/__tests__/__fixtures__/dotenv-flow-explicit-local/.env.development.local @@ -0,0 +1 @@ +API_KEY=development-local-key diff --git a/__tests__/__fixtures__/dotenv-flow-explicit-local/.env.local b/__tests__/__fixtures__/dotenv-flow-explicit-local/.env.local new file mode 100644 index 0000000..1b61c79 --- /dev/null +++ b/__tests__/__fixtures__/dotenv-flow-explicit-local/.env.local @@ -0,0 +1 @@ +API_KEY=local-key diff --git a/__tests__/__fixtures__/dotenv-flow-explicit-local/.env.production b/__tests__/__fixtures__/dotenv-flow-explicit-local/.env.production new file mode 100644 index 0000000..ef4d895 --- /dev/null +++ b/__tests__/__fixtures__/dotenv-flow-explicit-local/.env.production @@ -0,0 +1 @@ +API_KEY=production-key diff --git a/__tests__/__fixtures__/dotenv-flow-explicit-local/source.js b/__tests__/__fixtures__/dotenv-flow-explicit-local/source.js new file mode 100644 index 0000000..33c545e --- /dev/null +++ b/__tests__/__fixtures__/dotenv-flow-explicit-local/source.js @@ -0,0 +1,3 @@ +import {API_KEY} from '@env' + +console.log(API_KEY) diff --git a/__tests__/__fixtures__/dotenv-flow-mode-local/.babelrc b/__tests__/__fixtures__/dotenv-flow-mode-local/.babelrc new file mode 100644 index 0000000..2665d2e --- /dev/null +++ b/__tests__/__fixtures__/dotenv-flow-mode-local/.babelrc @@ -0,0 +1,7 @@ +{ + "plugins": [ + ["../../../", { + "path": "__tests__/__fixtures__/dotenv-flow/.env" + }] + ] +} diff --git a/__tests__/__fixtures__/dotenv-flow-mode-local/.env b/__tests__/__fixtures__/dotenv-flow-mode-local/.env new file mode 100644 index 0000000..75a4caf --- /dev/null +++ b/__tests__/__fixtures__/dotenv-flow-mode-local/.env @@ -0,0 +1 @@ +API_KEY=default-key diff --git a/__tests__/__fixtures__/dotenv-flow-mode-local/.env.development b/__tests__/__fixtures__/dotenv-flow-mode-local/.env.development new file mode 100644 index 0000000..360db36 --- /dev/null +++ b/__tests__/__fixtures__/dotenv-flow-mode-local/.env.development @@ -0,0 +1 @@ +API_KEY=development-key diff --git a/__tests__/__fixtures__/dotenv-flow-mode-local/.env.development.local b/__tests__/__fixtures__/dotenv-flow-mode-local/.env.development.local new file mode 100644 index 0000000..a013e01 --- /dev/null +++ b/__tests__/__fixtures__/dotenv-flow-mode-local/.env.development.local @@ -0,0 +1 @@ +API_KEY=development-local-key diff --git a/__tests__/__fixtures__/dotenv-flow-mode-local/.env.local b/__tests__/__fixtures__/dotenv-flow-mode-local/.env.local new file mode 100644 index 0000000..1b61c79 --- /dev/null +++ b/__tests__/__fixtures__/dotenv-flow-mode-local/.env.local @@ -0,0 +1 @@ +API_KEY=local-key diff --git a/__tests__/__fixtures__/dotenv-flow-mode-local/.env.production b/__tests__/__fixtures__/dotenv-flow-mode-local/.env.production new file mode 100644 index 0000000..ef4d895 --- /dev/null +++ b/__tests__/__fixtures__/dotenv-flow-mode-local/.env.production @@ -0,0 +1 @@ +API_KEY=production-key diff --git a/__tests__/__fixtures__/dotenv-flow-mode-local/source.js b/__tests__/__fixtures__/dotenv-flow-mode-local/source.js new file mode 100644 index 0000000..33c545e --- /dev/null +++ b/__tests__/__fixtures__/dotenv-flow-mode-local/source.js @@ -0,0 +1,3 @@ +import {API_KEY} from '@env' + +console.log(API_KEY) diff --git a/__tests__/__fixtures__/dotenv-flow-mode/.babelrc b/__tests__/__fixtures__/dotenv-flow-mode/.babelrc new file mode 100644 index 0000000..2665d2e --- /dev/null +++ b/__tests__/__fixtures__/dotenv-flow-mode/.babelrc @@ -0,0 +1,7 @@ +{ + "plugins": [ + ["../../../", { + "path": "__tests__/__fixtures__/dotenv-flow/.env" + }] + ] +} diff --git a/__tests__/__fixtures__/dotenv-flow-mode/.env b/__tests__/__fixtures__/dotenv-flow-mode/.env new file mode 100644 index 0000000..75a4caf --- /dev/null +++ b/__tests__/__fixtures__/dotenv-flow-mode/.env @@ -0,0 +1 @@ +API_KEY=default-key diff --git a/__tests__/__fixtures__/dotenv-flow-mode/.env.development b/__tests__/__fixtures__/dotenv-flow-mode/.env.development new file mode 100644 index 0000000..360db36 --- /dev/null +++ b/__tests__/__fixtures__/dotenv-flow-mode/.env.development @@ -0,0 +1 @@ +API_KEY=development-key diff --git a/__tests__/__fixtures__/dotenv-flow-mode/.env.development.local b/__tests__/__fixtures__/dotenv-flow-mode/.env.development.local new file mode 100644 index 0000000..a013e01 --- /dev/null +++ b/__tests__/__fixtures__/dotenv-flow-mode/.env.development.local @@ -0,0 +1 @@ +API_KEY=development-local-key diff --git a/__tests__/__fixtures__/dotenv-flow-mode/.env.local b/__tests__/__fixtures__/dotenv-flow-mode/.env.local new file mode 100644 index 0000000..1b61c79 --- /dev/null +++ b/__tests__/__fixtures__/dotenv-flow-mode/.env.local @@ -0,0 +1 @@ +API_KEY=local-key diff --git a/__tests__/__fixtures__/dotenv-flow-mode/.env.production b/__tests__/__fixtures__/dotenv-flow-mode/.env.production new file mode 100644 index 0000000..ef4d895 --- /dev/null +++ b/__tests__/__fixtures__/dotenv-flow-mode/.env.production @@ -0,0 +1 @@ +API_KEY=production-key diff --git a/__tests__/__fixtures__/dotenv-flow-mode/source.js b/__tests__/__fixtures__/dotenv-flow-mode/source.js new file mode 100644 index 0000000..33c545e --- /dev/null +++ b/__tests__/__fixtures__/dotenv-flow-mode/source.js @@ -0,0 +1,3 @@ +import {API_KEY} from '@env' + +console.log(API_KEY) diff --git a/__tests__/__fixtures__/dotenv-flow/.babelrc b/__tests__/__fixtures__/dotenv-flow/.babelrc new file mode 100644 index 0000000..2665d2e --- /dev/null +++ b/__tests__/__fixtures__/dotenv-flow/.babelrc @@ -0,0 +1,7 @@ +{ + "plugins": [ + ["../../../", { + "path": "__tests__/__fixtures__/dotenv-flow/.env" + }] + ] +} diff --git a/__tests__/__fixtures__/dotenv-flow/.env b/__tests__/__fixtures__/dotenv-flow/.env new file mode 100644 index 0000000..75a4caf --- /dev/null +++ b/__tests__/__fixtures__/dotenv-flow/.env @@ -0,0 +1 @@ +API_KEY=default-key diff --git a/__tests__/__fixtures__/dotenv-flow/.env.development b/__tests__/__fixtures__/dotenv-flow/.env.development new file mode 100644 index 0000000..360db36 --- /dev/null +++ b/__tests__/__fixtures__/dotenv-flow/.env.development @@ -0,0 +1 @@ +API_KEY=development-key diff --git a/__tests__/__fixtures__/dotenv-flow/.env.development.local b/__tests__/__fixtures__/dotenv-flow/.env.development.local new file mode 100644 index 0000000..a013e01 --- /dev/null +++ b/__tests__/__fixtures__/dotenv-flow/.env.development.local @@ -0,0 +1 @@ +API_KEY=development-local-key diff --git a/__tests__/__fixtures__/dotenv-flow/.env.local b/__tests__/__fixtures__/dotenv-flow/.env.local new file mode 100644 index 0000000..1b61c79 --- /dev/null +++ b/__tests__/__fixtures__/dotenv-flow/.env.local @@ -0,0 +1 @@ +API_KEY=local-key diff --git a/__tests__/__fixtures__/dotenv-flow/.env.production b/__tests__/__fixtures__/dotenv-flow/.env.production new file mode 100644 index 0000000..ef4d895 --- /dev/null +++ b/__tests__/__fixtures__/dotenv-flow/.env.production @@ -0,0 +1 @@ +API_KEY=production-key diff --git a/__tests__/__fixtures__/dotenv-flow/source.js b/__tests__/__fixtures__/dotenv-flow/source.js new file mode 100644 index 0000000..33c545e --- /dev/null +++ b/__tests__/__fixtures__/dotenv-flow/source.js @@ -0,0 +1,3 @@ +import {API_KEY} from '@env' + +console.log(API_KEY) diff --git a/__tests__/index.test.js b/__tests__/index.test.js index 17b5b3a..ee29ae9 100644 --- a/__tests__/index.test.js +++ b/__tests__/index.test.js @@ -198,4 +198,30 @@ describe('react-native-dotenv', () => { const {code} = transformFileSync(FIXTURES + 'env-name/source.js') expect(code).toBe('console.log("abc123456");\nconsole.log("username123456");') }) + + it('should follow dotenv-flow variable precedence (default)', () => { + const {code} = transformFileSync(FIXTURES + 'dotenv-flow/source.js') + expect(code).toBe('console.log("local-key");') + }) + + it('should follow dotenv-flow variable precedence (mode)', () => { + process.env.NODE_ENV = 'production' + + const {code} = transformFileSync(FIXTURES + 'dotenv-flow-mode/source.js', {}) + expect(code).toBe('console.log("production-key");') + }) + + it('should follow dotenv-flow variable precedence (mode local)', () => { + process.env.NODE_ENV = 'development' + + const {code} = transformFileSync(FIXTURES + 'dotenv-flow-mode-local/source.js', {}) + expect(code).toBe('console.log("development-local-key");') + }) + + it('should follow dotenv-flow variable precedence (explicit local)', () => { + process.env.NODE_ENV = 'local' + + const {code} = transformFileSync(FIXTURES + 'dotenv-flow-explicit-local/source.js', {}) + expect(code).toBe('console.log("local-key");') + }) }) diff --git a/index.js b/index.js index e2a360d..d172805 100644 --- a/index.js +++ b/index.js @@ -57,7 +57,6 @@ function mtime(filePath) { module.exports = (api, options) => { const t = api.types - let env = {} options = { envName: 'APP_ENV', moduleName: '@env', @@ -93,8 +92,15 @@ module.exports = (api, options) => { const localParsed = parseDotenvFile(localFilePath, options.verbose) const modeParsed = parseDotenvFile(modeFilePath, options.verbose) const modeLocalParsed = parseDotenvFile(modeLocalFilePath, options.verbose) - env = (options.safe) ? safeObjectAssign(undefObjectAssign(undefObjectAssign(undefObjectAssign(parsed, modeParsed), localParsed), modeLocalParsed), dotenvTemporary, ['NODE_ENV', 'BABEL_ENV', options.envName]) - : undefObjectAssign(undefObjectAssign(undefObjectAssign(undefObjectAssign(parsed, modeParsed), localParsed), modeLocalParsed), dotenvTemporary) + + let env = parsed + env = undefObjectAssign(env, localParsed) + env = undefObjectAssign(env, modeParsed) + env = undefObjectAssign(env, modeLocalParsed) + + env = options.safe ? + safeObjectAssign(env, dotenvTemporary, ['NODE_ENV', 'BABEL_ENV', options.envName]) : + undefObjectAssign(env, dotenvTemporary) api.addExternalDependency(path.resolve(options.path)) api.addExternalDependency(path.resolve(modeFilePath))