Skip to content

Commit 2e4d6d5

Browse files
authored
Merge pull request #6 from Bardigan/feature/split-config
Split Config into utils
2 parents 77a418f + e105f1b commit 2e4d6d5

File tree

8 files changed

+45
-37
lines changed

8 files changed

+45
-37
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"lint:fix": "npm run lint -- --fix",
1414
"test": "cross-env BABEL_ENV=test npm run test:unit",
1515
"test:watch": "cross-env BABEL_ENV=test npm run test:unit -- -w -R dot",
16-
"test:unit": "mocha --compilers js:babel-core/register './test/**/*.spec.js' --require babel-polyfill",
16+
"test:unit": "mocha --compilers js:babel-core/register test/**/*.spec.js --require babel-polyfill",
1717
"test:live": "npm run test && npm run build && npm pack && npm install -g json-viewer-*.tgz",
1818
"coverage": "cross-env BABEL_ENV=test nyc --reporter=lcov npm run test:unit",
1919
"coverage:html": "cross-env BABEL_ENV=test nyc --reporter=html npm run test:unit"

src/config.js

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { jsonViewer } from '@src/json-viewer';
2-
import { setUserConfig, getUserConfig } from '@src/config';
2+
import { setUserConfig, getUserConfig } from '@utils/config';
33
import { getCliParams, getCliInput } from '@utils/cli';
44
import { prettyPrint } from '@utils/console/pretty-print';
55

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { defaultConfig } from '@utils/config';
2+
3+
export const getDefaultConfig = () => defaultConfig;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { readJsonFile } from '@utils/files';
2+
import { userConfigFile, defaultConfig } from '@utils/config';
3+
4+
export const getUserConfig = (filepath = '') => {
5+
const userConfig = readJsonFile(filepath || userConfigFile);
6+
7+
return {
8+
...defaultConfig,
9+
...userConfig
10+
};
11+
};

src/utils/config/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import path from 'path';
2+
3+
export const defaultConfig = {
4+
color: false
5+
};
6+
7+
export const userConfigFile = path.resolve(process.cwd(), '.user-config.json');
8+
9+
export * from './get-default-config';
10+
export * from './get-user-config';
11+
export * from './set-user-config';
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { writeJsonFile } from '@utils/files';
2+
import { userConfigFile, defaultConfig } from '@utils/config';
3+
4+
export const setUserConfig = (params = {}, userConfig = {}) => {
5+
const paramsConfig = {
6+
color: params.alwaysColor || userConfig.color || defaultConfig.color
7+
};
8+
const newConfig = {
9+
...defaultConfig,
10+
...userConfig,
11+
...paramsConfig
12+
};
13+
14+
writeJsonFile(userConfigFile, newConfig);
15+
16+
return newConfig;
17+
};
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from 'path';
22
import { expect } from 'chai';
3-
import { defaultConfig, userConfigFile, getDefaultConfig, getUserConfig, setUserConfig } from '@src/config';
3+
import { defaultConfig, userConfigFile, getDefaultConfig, getUserConfig, setUserConfig } from '@utils/config';
44

55
describe('config', () => {
66
describe('userConfigFile', () => {

0 commit comments

Comments
 (0)