Skip to content

Commit 2992ed9

Browse files
committed
Feat: separate getConfigPath and setConfig for future which-config
1 parent 7d1bcd7 commit 2992ed9

File tree

2 files changed

+94
-26
lines changed

2 files changed

+94
-26
lines changed

__tests__/Config.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,24 +69,36 @@ it('read global config', () => {
6969
const sgcrc = json.readToObjSync(path.join(fixtures, '.sgcrc'));
7070

7171
fs.writeFileSync(path.join(homedir, '.sgcrc'), JSON.stringify(sgcrc));
72-
expect(new Config().config).toEqual(sgcrc);
72+
73+
const { config } = new Config();
74+
7375
fs.removeSync(path.join(homedir, '.sgcrc'));
76+
77+
expect(config).toEqual(sgcrc);
7478
});
7579

7680
it('read local config from `sgc.config.js`', () => {
7781
const sgcrc = json.readToObjSync(path.join(fixtures, '.sgcrc'));
7882

7983
fs.writeFileSync(path.join(cwd, 'sgc.config.js'), `module.exports = (${JSON.stringify(sgcrc)})`);
80-
expect(new Config().config).toEqual(sgcrc);
84+
85+
const { config } = new Config();
86+
8187
fs.removeSync(path.join(cwd, 'sgc.config.js'));
88+
89+
expect(config).toEqual(sgcrc);
8290
});
8391

8492
it('read global config from `sgc.config.js`', () => {
8593
const sgcrc = json.readToObjSync(path.join(fixtures, '.sgcrc'));
8694

8795
fs.writeFileSync(path.join(homedir, 'sgc.config.js'), `module.exports = (${JSON.stringify(sgcrc)})`);
88-
expect(new Config().config).toEqual(sgcrc);
96+
97+
const { config } = new Config();
98+
8999
fs.removeSync(path.join(homedir, 'sgc.config.js'));
100+
101+
expect(config).toEqual(sgcrc);
90102
});
91103

92104
it('read a .sgcrc_default from a deep nested cwd', () => {

lib/Config.ts

Lines changed: 79 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@ export interface SgcConfig {
3333
};
3434
}
3535

36-
const safeRequire = (jsPath: string | null): SgcConfig | false => (
37-
// eslint-disable-next-line global-require, import/no-dynamic-require
38-
jsPath && fs.existsSync(jsPath) && require(jsPath)
39-
);
40-
4136
class Config {
4237
altPath: string | null;
4338

@@ -50,19 +45,29 @@ class Config {
5045
this.setConfig();
5146
}
5247

53-
private setConfig(): SgcConfig {
54-
const pathString = findup(this.fileName, { cwd: this.altPath || cwd });
55-
const localeConfigJS = safeRequire(findup('sgc.config.js', { cwd }));
56-
const localeConfig = pathString ? json.readToObjSync<SgcConfig>(pathString) : false;
57-
const globalConfigJS = safeRequire(path.join(homedir, 'sgc.config.js'));
58-
const globalConfig = json.readToObjSync<SgcConfig>(path.join(homedir, '.sgcrc'));
59-
const packageJson = findup('package.json', { cwd });
60-
const packageConfig = packageJson
61-
? (json.readToObjSync<{ sgc?: SgcConfig }>(packageJson) || {}).sgc
62-
: false;
63-
const sgcrcDefaultConfig = json.readToObjSync<SgcConfig>(path.join(__dirname, '..', '.sgcrc')) as SgcConfig;
64-
const sgcrcTestDefaultConfig = json.readToObjSync<SgcConfig>(path.join(__dirname, '..', '.sgcrc_default')) as SgcConfig;
65-
const sgcrcDefault = sgcrcDefaultConfig || sgcrcTestDefaultConfig;
48+
static safeRequire = (jsPath: string | null): SgcConfig | false => (
49+
// eslint-disable-next-line global-require, import/no-dynamic-require
50+
jsPath && require(jsPath)
51+
)
52+
53+
static safeRead = (configPath: string | null): SgcConfig | false => (
54+
!!configPath && json.readToObjSync<SgcConfig>(configPath)
55+
)
56+
57+
static getPath = (configPath: string | null): string | null => (
58+
!!configPath && fs.existsSync(configPath) ? configPath : null
59+
)
60+
61+
private getConfigPath(): { path: string; defaultPath: string; type: 'rc' | 'js' | 'pkg' } {
62+
// paths
63+
const localPath = Config.getPath(findup(this.fileName, { cwd: this.altPath || cwd }));
64+
const localJsPath = Config.getPath(findup('sgc.config.js', { cwd }));
65+
const globalPath = Config.getPath(path.join(homedir, this.fileName));
66+
const globalJsPath = Config.getPath(path.join(homedir, 'sgc.config.js'));
67+
const packageJson = Config.getPath(findup('package.json', { cwd }));
68+
const defaultPath = Config.getPath(path.join(__dirname, '..', '.sgcrc')) as string;
69+
const testDefaultPath = Config.getPath(path.join(__dirname, '..', '.sgcrc_default')) as string;
70+
const sgcrcDefault = defaultPath || testDefaultPath;
6671

6772
// priority order (1. highest priority):
6873
// 1. local config
@@ -73,13 +78,64 @@ class Config {
7378
// 3. default config
7479
// - 1. from ../.sgcrc
7580
// - 2. test case ../.sgcrc is renamed to ../.sgcrc_default
76-
const config = localeConfigJS
77-
|| localeConfig
78-
|| packageConfig
79-
|| globalConfigJS
80-
|| globalConfig
81+
const configPath = localJsPath
82+
|| localPath
83+
|| packageJson
84+
|| globalJsPath
85+
|| globalPath
8186
|| sgcrcDefault;
8287

88+
let type: 'rc' | 'js' | 'pkg';
89+
90+
switch (path.extname(configPath)) {
91+
case '.json':
92+
type = 'pkg';
93+
break;
94+
95+
case '.js':
96+
type = 'js';
97+
break;
98+
99+
default:
100+
type = 'rc';
101+
break;
102+
}
103+
104+
return {
105+
path: configPath,
106+
defaultPath: sgcrcDefault,
107+
type,
108+
};
109+
}
110+
111+
private setConfig(): SgcConfig {
112+
const configPath = this.getConfigPath();
113+
const sgcrcDefault: SgcConfig = Config.safeRead(configPath.defaultPath) as SgcConfig;
114+
115+
let config: SgcConfig = sgcrcDefault;
116+
let readConfig: SgcConfig | false;
117+
118+
switch (configPath.type) {
119+
case 'js':
120+
readConfig = Config.safeRequire(configPath.path);
121+
break;
122+
123+
case 'pkg':
124+
readConfig = (
125+
json.readToObjSync<{ sgc: SgcConfig }>(configPath.path)
126+
|| { sgc: false as false }
127+
).sgc;
128+
break;
129+
130+
default:
131+
case 'rc':
132+
readConfig = Config.safeRead(configPath.path);
133+
}
134+
135+
if (readConfig) {
136+
config = readConfig;
137+
}
138+
83139
// set defaults which are necessary
84140
const modifiedConfig = merge({}, sgcrcDefault, config);
85141

0 commit comments

Comments
 (0)