Skip to content

Commit 5aac51f

Browse files
committed
✨ Feat: load global config
1 parent 8a5a54a commit 5aac51f

File tree

2 files changed

+40
-13
lines changed

2 files changed

+40
-13
lines changed

lib/getConfig.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
1+
import os from 'os';
12
import path from 'path';
23
import json from 'json-extra';
34

45
const cwd = process.cwd();
6+
const homedir = os.homedir();
57

68
const getConfig = (altPath) => {
79
const pathString = altPath || path.join(cwd, '.sgcrc');
8-
let configObject = json.readToObjSync(pathString);
10+
const configObject = json.readToObjSync(pathString);
11+
const globalConfig = json.readToObjSync(path.join(homedir, '.sgcrc'));
12+
const packageConfig = json.readToObjSync(path.join(cwd, 'package.json')).sgc;
13+
const sgcrcDefaultConfig = json.readToObjSync(path.join(__dirname, '..', '.sgcrc_default'));
914

10-
if (!configObject) {
11-
// if package.json .rcs else .sgcrc_default
12-
const packageJson = json.readToObjSync(path.join(cwd, 'package.json')).sgc;
13-
const sgcrcDefault = json.readToObjSync(path.join(__dirname, '..', '.sgcrc_default'));
14-
configObject = typeof packageJson === 'object' ? packageJson : sgcrcDefault;
15-
}
15+
// priority order (1. highest priority):
16+
// 1. local config
17+
// - 1. .sgcrc
18+
// - 2. (package.json).sgc
19+
// 2. global config
20+
// 3. default config from ../.sgcrc_default
21+
const config = configObject || packageConfig || globalConfig || sgcrcDefaultConfig;
1622

17-
return configObject;
23+
return config;
1824
};
1925

2026
export default getConfig;

test/getConfig.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import fs from 'fs-extra';
2+
import os from 'os';
13
import test from 'ava';
24
import path from 'path';
35
import json from 'json-extra';
4-
import fs from 'fs-extra';
56
import getConfig from '../lib/getConfig';
67

78
const cwd = process.cwd();
9+
const homedir = os.homedir();
810
const fixtures = path.join(cwd, 'test', 'fixtures');
911

1012
test('read config from a specific path', (t) => {
@@ -18,12 +20,31 @@ test('read config from a .sgcrc_default', (t) => {
1820
test('read config from package.json', (t) => {
1921
const sgcrc = json.readToObjSync(path.join(fixtures, '.sgcrc'));
2022
const packageJson = json.readToObjSync(path.join(cwd, 'package.json'));
23+
const randomString = Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 10);
2124
packageJson.sgc = sgcrc;
2225

23-
fs.copySync(path.join(cwd, 'package.json'), path.join(cwd, 'package.json.back'));
24-
fs.unlinkSync(path.join(cwd, 'package.json'));
26+
fs.renameSync(path.join(cwd, 'package.json'), path.join(cwd, `package.json.${randomString}.back`));
2527
fs.writeFileSync(path.join(cwd, 'package.json'), JSON.stringify(packageJson));
2628
t.deepEqual(getConfig(), sgcrc);
27-
fs.unlinkSync(path.join(cwd, 'package.json'));
28-
fs.renameSync(path.join(cwd, 'package.json.back'), path.join(cwd, 'package.json'));
29+
fs.removeSync(path.join(cwd, 'package.json'));
30+
fs.renameSync(path.join(cwd, `package.json.${randomString}.back`), path.join(cwd, 'package.json'));
31+
});
32+
33+
test('read global config', (t) => {
34+
let globalExist = false;
35+
const sgcrc = json.readToObjSync(path.join(fixtures, '.sgcrc'));
36+
const randomString = Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 10);
37+
38+
if (fs.existsSync(path.join(homedir, '.sgcrc'))) {
39+
globalExist = true;
40+
fs.renameSync(path.join(homedir, '.sgcrc'), path.join(homedir, `.sgcrc.${randomString}.back`));
41+
}
42+
43+
fs.writeFileSync(path.join(homedir, '.sgcrc'), JSON.stringify(sgcrc));
44+
t.deepEqual(getConfig(), sgcrc);
45+
fs.removeSync(path.join(homedir, '.sgcrc'));
46+
47+
if (globalExist) {
48+
fs.renameSync(path.join(homedir, `.sgcrc.${randomString}.back`), path.join(homedir, '.sgcrc'));
49+
}
2950
});

0 commit comments

Comments
 (0)