@@ -33,11 +33,6 @@ export interface SgcConfig {
33
33
} ;
34
34
}
35
35
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
-
41
36
class Config {
42
37
altPath : string | null ;
43
38
@@ -50,19 +45,29 @@ class Config {
50
45
this . setConfig ( ) ;
51
46
}
52
47
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 ;
66
71
67
72
// priority order (1. highest priority):
68
73
// 1. local config
@@ -73,13 +78,64 @@ class Config {
73
78
// 3. default config
74
79
// - 1. from ../.sgcrc
75
80
// - 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
81
86
|| sgcrcDefault ;
82
87
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
+
83
139
// set defaults which are necessary
84
140
const modifiedConfig = merge ( { } , sgcrcDefault , config ) ;
85
141
0 commit comments