Skip to content

Commit 4d825cc

Browse files
committed
Feat: export configPath from Config
1 parent 2992ed9 commit 4d825cc

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

__tests__/Config.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,21 @@ it('read a .sgcrc_default from a deep nested cwd', () => {
107107

108108
expect(new Config(deepCwd).config).toEqual(fixturesConfig);
109109
});
110+
111+
it('should get the right config path', () => {
112+
const sgcrc = json.readToObjSync(path.join(fixtures, '.sgcrc'));
113+
114+
fs.writeFileSync(path.join(homedir, 'sgc.config.js'), `module.exports = (${JSON.stringify(sgcrc)})`);
115+
116+
const { configPath } = new Config();
117+
118+
fs.removeSync(path.join(homedir, 'sgc.config.js'));
119+
120+
expect(path.basename(configPath)).toBe('sgc.config.js');
121+
});
122+
123+
it('should get the right config path', () => {
124+
const { configPath } = new Config();
125+
126+
expect(path.basename(configPath)).toBe('package.json');
127+
});

lib/Config.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ export interface SgcConfig {
3434
}
3535

3636
class Config {
37-
altPath: string | null;
37+
alternativeCwd: string | null;
3838

39-
fileName: string;
39+
sgcConfigName: string;
4040

41-
constructor(altPath: string | null = null, fileName = '.sgcrc') {
42-
this.altPath = altPath;
43-
this.fileName = fileName;
41+
constructor(alternativeCwd: string | null = null, sgcConfigName = '.sgcrc') {
42+
this.alternativeCwd = alternativeCwd;
43+
this.sgcConfigName = sgcConfigName;
4444

4545
this.setConfig();
4646
}
@@ -60,9 +60,11 @@ class Config {
6060

6161
private getConfigPath(): { path: string; defaultPath: string; type: 'rc' | 'js' | 'pkg' } {
6262
// paths
63-
const localPath = Config.getPath(findup(this.fileName, { cwd: this.altPath || cwd }));
63+
const localPath = Config.getPath((
64+
findup(this.sgcConfigName, { cwd: this.alternativeCwd || cwd })
65+
));
6466
const localJsPath = Config.getPath(findup('sgc.config.js', { cwd }));
65-
const globalPath = Config.getPath(path.join(homedir, this.fileName));
67+
const globalPath = Config.getPath(path.join(homedir, this.sgcConfigName));
6668
const globalJsPath = Config.getPath(path.join(homedir, 'sgc.config.js'));
6769
const packageJson = Config.getPath(findup('package.json', { cwd }));
6870
const defaultPath = Config.getPath(path.join(__dirname, '..', '.sgcrc')) as string;
@@ -155,6 +157,10 @@ class Config {
155157
public get config(): SgcConfig {
156158
return this.setConfig();
157159
}
160+
161+
public get configPath(): string {
162+
return this.getConfigPath().path;
163+
}
158164
}
159165

160166
export default Config;

0 commit comments

Comments
 (0)