@trenskow/dotenv 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -11,7 +11,7 @@ import { resolve, dirname } from 'path';
11
11
 
12
12
  const __dirname = new URL('.', import.meta.url).pathname;
13
13
 
14
- const dotenv = (directory = dirname(resolve(process.cwd(), process.argv[1])) || __dirname) => {
14
+ const dotenv = ({ directory = dirname(resolve(process.cwd(), process.argv[1])) || __dirname } = {}) => {
15
15
 
16
16
  const env = process.env.NODE_ENV || 'development';
17
17
 
@@ -20,6 +20,8 @@ const dotenv = (directory = dirname(resolve(process.cwd(), process.argv[1])) ||
20
20
  resolve(directory, `.env.${env}`)
21
21
  ];
22
22
 
23
+ const result = {};
24
+
23
25
  paths.forEach((path) => {
24
26
  try {
25
27
  const data = readFileSync(path, 'utf8');
@@ -29,13 +31,19 @@ const dotenv = (directory = dirname(resolve(process.cwd(), process.argv[1])) ||
29
31
  .filter((line) => line && line[0] !== '#')
30
32
  .forEach((line) => {
31
33
  const [key, value] = line.split('=');
32
- process.env[key] = value.trim().replace(/(^['"]|['"]$)/g, '');
34
+ result[key] = value.trim().replace(/(^['"]|['"]$)/g, '');
33
35
  });
34
36
  } catch (_) { }
35
37
  });
36
38
 
37
- if (directory !== '/') dotenv(resolve(directory, '..'));
39
+ if (directory !== '/') Object.assign(result, dotenv({ directory: resolve(directory, '..') }));
40
+
41
+ return result;
38
42
 
39
43
  };
40
44
 
41
- export default dotenv;
45
+ export default ({ apply, ...others } = { apply: true }) => {
46
+ const env = dotenv({ ...others });
47
+ if (apply) Object.assign(process.env, env);
48
+ return env;
49
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trenskow/dotenv",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Parses .env files.",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -29,4 +29,4 @@
29
29
  "globals": "^15.11.0",
30
30
  "mocha": "^10.7.3"
31
31
  }
32
- }
32
+ }
package/test/index.js CHANGED
@@ -11,10 +11,15 @@ import { expect } from 'chai';
11
11
  import dotenv from '../index.js';
12
12
 
13
13
  describe('config', () => {
14
- before(() => {
15
- dotenv();
14
+ describe('unapplied', () => {
15
+ it ('must contain variable from `.env` file.', () => {
16
+ expect(dotenv({ apply: false }).THIS_IS_A_TEST).to.equal('yes');
17
+ });
16
18
  });
17
- it ('must contain variable from `.env` file.', () => {
18
- expect(process.env.THIS_IS_A_TEST).to.equal('yes');
19
+ describe('applied', () => {
20
+ before(() => dotenv());
21
+ it ('must contain variable from `.env` file.', () => {
22
+ expect(process.env.THIS_IS_A_TEST).to.equal('yes');
23
+ });
19
24
  });
20
25
  });