Skip to content

Commit

Permalink
refactor(dotenv-flow): shorten .listDotenvFiles to .listFiles
Browse files Browse the repository at this point in the history
  • Loading branch information
kerimdzhanov committed Aug 21, 2023
1 parent 6dba93c commit 1dbc701
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ require('dotenv-flow').config({
The following API considered as internal, but it is also exposed to give the ability to be used programmatically by your own needs.


#### `.listDotenvFiles(dirname, [options]) => string[]`
#### `.listFiles(dirname, [options]) => string[]`

Returns a list of `.env*` filenames depending on the given `options.node_env`. The resulting list is ordered by the env files priority from lowest to highest.

Expand Down Expand Up @@ -440,10 +440,11 @@ A list of `.env*` filenames.
```js
const dotenvFlow = require('dotenv-flow');

const filenames = dotenvFlow.listDotenvFiles('/path/to/project', { node_env: 'development' });
const filenames = dotenvFlow.listFiles('/path/to/project', { node_env: 'development' });

console.log(filenames); // will output the following:
// > [ '/path/to/project/.env',
// > [ '/path/to/project/.env.defaults',
// > '/path/to/project/.env',
// > '/path/to/project/.env.local',
// > '/path/to/project/.env.development',
// > '/path/to/project/.env.development.local' ]
Expand Down
7 changes: 4 additions & 3 deletions lib/dotenv-flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const dotenv = require('dotenv');
* @param {string} [options.node_env] - node environment (development/test/production/etc,.)
* @return {string[]}
*/
function listDotenvFiles(dirname, options = {}) {
function listFiles(dirname, options = {}) {
const {node_env} = options;

return [
Expand Down Expand Up @@ -144,7 +144,7 @@ function config(options = {}) {
}

const existingFiles = (
listDotenvFiles(path, { node_env })
listFiles(path, { node_env })
.filter(filename => fs.existsSync(filename))
);

Expand All @@ -156,7 +156,8 @@ function config(options = {}) {
}

module.exports = {
listDotenvFiles,
listFiles,
listDotenvFiles: listFiles, // for API backward compatibility
parse,
load,
unload,
Expand Down
17 changes: 15 additions & 2 deletions test/integration/exports.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,28 @@ describe('exports', () => {
it('should load module using require', () => {
const dotenv = require('../..')

expect(dotenv).to.have.keys('listDotenvFiles', 'config', 'parse', 'load', 'unload');
expect(dotenv).to.include.keys([
'listFiles',
'config',
'parse',
'load',
'unload'
]);
});
});

describe('esm', () => {
it('should load module using import', async () => {
const dotenv = await import('dotenv-flow'); // self-import

expect(dotenv).to.have.keys('listDotenvFiles', 'config', 'parse', 'load', 'unload', 'default');
expect(dotenv).to.include.keys([
'listFiles',
'config',
'parse',
'load',
'unload',
'default'
]);
});

it('should load config entry point', async () => {
Expand Down
14 changes: 7 additions & 7 deletions test/unit/dotenv-flow-api.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ function isolateProcessEnv() {
}

describe('dotenv-flow (API)', () => {
describe('.listDotenvFiles', () => {
describe('.listFiles', () => {
describe('by default (when no options are given)', () => {
let filenames;

beforeEach('apply `.listDotenvFiles` without extra options', () => {
filenames = dotenvFlow.listDotenvFiles('/path/to/project')
beforeEach('apply `.listFiles` without extra options', () => {
filenames = dotenvFlow.listFiles('/path/to/project')
.map(p => normalizePosixPath(p));
});

Expand Down Expand Up @@ -62,8 +62,8 @@ describe('dotenv-flow (API)', () => {
describe('when the `node_env` option is given', () => {
let filenames;

beforeEach('apply `.listDotenvFiles` with the `node_env` option', () => {
filenames = dotenvFlow.listDotenvFiles('/path/to/project', { node_env: 'development' })
beforeEach('apply `.listFiles` with the `node_env` option', () => {
filenames = dotenvFlow.listFiles('/path/to/project', { node_env: 'development' })
.map(p => normalizePosixPath(p));
});

Expand Down Expand Up @@ -102,8 +102,8 @@ describe('dotenv-flow (API)', () => {
describe('when the `node_env` option is set to "test"', () => {
let filenames;

beforeEach('apply `.listDotenvFiles` with the `node_env` option value of "test"', () => {
filenames = dotenvFlow.listDotenvFiles('/path/to/project', { node_env: 'test' })
beforeEach('apply `.listFiles` with the `node_env` option value of "test"', () => {
filenames = dotenvFlow.listFiles('/path/to/project', { node_env: 'test' })
.map(p => normalizePosixPath(p));
});

Expand Down

0 comments on commit 1dbc701

Please sign in to comment.