Skip to content

Commit

Permalink
convert to factory function, add options support
Browse files Browse the repository at this point in the history
  • Loading branch information
killmenot committed Aug 29, 2018
1 parent d978796 commit 5dd46e8
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 27 deletions.
23 changes: 17 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,31 @@

```
|
-- config.developent.json
-- config.integration.json
-- config.json
|-- bar
|-- config.json
|-- config.developent.json
|-- config.integration.json
|-- config.json
```

```js
const requireJson = require('qewd-require-json');
const requireJsonFactory = require('qewd-require-json');
const requireJson = requireJsonFactory();

// basic
const config = requireJson('./config.json') // config.json loaded

// pass version via NODE_ENV
process.env.NODE_ENV = 'development';
const config = requireJson('./config.json') // config.development.json loaded
const config = requireJson('./config.json') // config.development.json is loaded

// pass version via TEST_ENV
process.env.NODE_ENV = 'staging';
process.env.TEST_ENV = 'integration';
const config = requireJson('./config.json') // config.integration.json loaded
const config = requireJson('./config.json') // config.integration.json is loaded

// overwrite cwd via options
const options = { cwd: '/path/to/bar' }
const requireJson = requireJsonFactory(options);
const config = requireJson('./config.json') // config.json from bar folder is loaded
```
40 changes: 22 additions & 18 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,31 @@
const path = require('path');
const fs = require('fs');

module.exports = function (modulePath) {
const absoluteModulePath = path.join(process.cwd(), modulePath);
const nodeEnv = (process.env.NODE_ENV || '').toLowerCase();
const testEnv = (process.env.TEST_ENV || '').toLowerCase();
module.exports = (options = {}) => {
const cwd = options.cwd || '';

const dirname = path.dirname(absoluteModulePath);
const basename = path.basename(absoluteModulePath, '.json');
return (modulePath) => {
const absoluteModulePath = path.join(cwd || process.cwd(), modulePath);
const nodeEnv = (process.env.NODE_ENV || '').toLowerCase();
const testEnv = (process.env.TEST_ENV || '').toLowerCase();

let newAbsoluteModulePath;
const dirname = path.dirname(absoluteModulePath);
const basename = path.basename(absoluteModulePath, '.json');

if (testEnv) {
newAbsoluteModulePath = `${dirname}/${basename}.${testEnv}.json`;
} else if (nodeEnv) {
newAbsoluteModulePath = `${dirname}/${basename}.${nodeEnv}.json`;
}
let newAbsoluteModulePath;

try {
fs.accessSync(newAbsoluteModulePath);
} catch (err) {
newAbsoluteModulePath = absoluteModulePath;
}
if (testEnv) {
newAbsoluteModulePath = `${dirname}/${basename}.${testEnv}.json`;
} else if (nodeEnv) {
newAbsoluteModulePath = `${dirname}/${basename}.${nodeEnv}.json`;
}

return require(newAbsoluteModulePath);
try {
fs.accessSync(newAbsoluteModulePath);
} catch (err) {
newAbsoluteModulePath = absoluteModulePath;
}

return require(newAbsoluteModulePath);
};
};
3 changes: 3 additions & 0 deletions spec/fixtures/bar/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"value": "bar"
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
27 changes: 24 additions & 3 deletions spec/unit/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
'use strict';

const path = require('path');
const requireJson = require('../..');
const requireJsonFactory = require('../..');

describe('qewd-require-json', () => {
let requireJson;

beforeAll(() => {
const fixturesDir = path.join(process.cwd(), 'spec/fixtures');
process.chdir(fixturesDir);
const cwd = path.join(process.cwd(), 'spec/fixtures/foo');
process.chdir(cwd);
});

beforeEach(() => {
requireJson = requireJsonFactory();
});

afterEach(() => {
Expand Down Expand Up @@ -79,4 +85,19 @@ describe('qewd-require-json', () => {

expect(actual).toEqual(expected);
});

it('should return config when options.cwd', () => {
const expected = {
value: 'bar'
};

const options = {
cwd: path.join(process.cwd(), '../bar')
};
requireJson = requireJsonFactory(options);

const actual = requireJson('./config.json');

expect(actual).toEqual(expected);
});
});

0 comments on commit 5dd46e8

Please sign in to comment.