Skip to content

Commit

Permalink
add different folders support
Browse files Browse the repository at this point in the history
  • Loading branch information
killmenot committed Sep 28, 2018
1 parent e50860c commit ae17da7
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 32 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ node_js:
- '8'
- '7'
- '6'
- '5'
- '4'
script:
- npm run test
after_success:
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# qewd-require-json

Load configuration JSON file based on environment value
Load QEWD configuration JSON files based on environment

[![Build Status](https://travis-ci.org/killmenot/qewd-require-json.svg?branch=master)](https://travis-ci.org/killmenot/qewd-require-json) [![Coverage Status](https://coveralls.io/repos/github/killmenot/qewd-require-json/badge.svg?branch=master)](https://coveralls.io/github/killmenot/qewd-require-json?branch=master) [![Dependency Status](https://david-dm.org/killmenot/qewd-require-json.svg)](https://david-dm.org/killmenot/qewd-require-json) [![npm version](https://img.shields.io/npm/v/qewd-require-json.svg)](https://www.npmjs.com/package/qewd-require-json)

[![Build Status](https://travis-ci.org/killmenot/qewd-require-json.svg?branch=master)](https://travis-ci.org/killmenot/qewd-require-json) [![Coverage Status](https://coveralls.io/repos/github/killmenot/qewd-require-json/badge.svg?branch=master)](https://coveralls.io/github/killmenot/qewd-require-json?branch=master)

## Usage

Expand Down
54 changes: 33 additions & 21 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,25 @@
const path = require('path');
const fs = require('fs');

function spread(absolutePath) {
const nodeEnv = (process.env.NODE_ENV || '').toLowerCase();
const testEnv = (process.env.TEST_ENV || '').toLowerCase();
const paths = [];

const dirname = path.dirname(absolutePath);
const basename = path.basename(absolutePath, '.json');

if (testEnv) {
paths.push(`${dirname}/${basename}.${testEnv}.json`);
} else if (nodeEnv) {
paths.push(`${dirname}/${basename}.${nodeEnv}.json`);
}

paths.push(absolutePath);

return paths;
}

module.exports = (options) => {
if (typeof options === 'string') {
options = {
Expand All @@ -17,27 +36,20 @@ module.exports = (options) => {
const cwd = options.cwd || '';

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

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

let newAbsoluteModulePath;

if (testEnv) {
newAbsoluteModulePath = `${dirname}/${basename}.${testEnv}.json`;
} else if (nodeEnv) {
newAbsoluteModulePath = `${dirname}/${basename}.${nodeEnv}.json`;
const paths = [
path.join('spec/support', modulePath),
path.join('spec', modulePath),
modulePath
]
.map(p => path.join(cwd || process.cwd(), p))
.map(spread)
.reduce((x, y) => x.concat(y), []);

for (let i = 0; i < paths.length; i++) {
try {
fs.accessSync(paths[i]);
return require(paths[i]);
} catch (err) {}
}

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

return require(newAbsoluteModulePath);
};
};
9 changes: 2 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "qewd-require-json",
"version": "1.1.1",
"description": "Load configuration JSON file based on environment value",
"description": "Load QEWD configuration JSON files based on environment value",
"author": "Alexey Kucherenko",
"main": "index.js",
"license": "MIT",
Expand All @@ -27,19 +27,14 @@
"all": true,
"include": [
"lib/**/*.js"
],
"exclude": [
"coverage",
"node_modules",
"spec/**/*.js"
]
},
"devDependencies": {
"coveralls": "^3.0.2",
"jasmine": "^3.2.0",
"jasmine-spec-reporter": "^4.1.1",
"jshint": "^2.9.6",
"nyc": "^13.0.1",
"nyc": "^13.1.0",
"pre-commit": "^1.2.2"
}
}
3 changes: 3 additions & 0 deletions spec/fixtures/baz/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"value": "baz"
}
3 changes: 3 additions & 0 deletions spec/fixtures/baz/spec/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"value": "baz.spec"
}
3 changes: 3 additions & 0 deletions spec/fixtures/quux/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"value": "quux"
}
3 changes: 3 additions & 0 deletions spec/fixtures/quux/spec/support/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"value": "quux.spec"
}
26 changes: 26 additions & 0 deletions spec/unit/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,30 @@ describe('qewd-require-json', () => {

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

it('should return config from "spec" folder', () => {
const expected = {
value: 'baz.spec'
};

const options = path.join(process.cwd(), '../baz');
requireJson = requireJsonFactory(options);

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

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

it('should return config from "spec/support" folder', () => {
const expected = {
value: 'quux.spec'
};

const options = path.join(process.cwd(), '../quux');
requireJson = requireJsonFactory(options);

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

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

0 comments on commit ae17da7

Please sign in to comment.