@@ -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,68 @@ 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
+ const a = fs ;
89
+
90
+ console . log ( configPath ) ;
91
+
92
+ let type : 'rc' | 'js' | 'pkg' ;
93
+
94
+ switch ( path . extname ( configPath ) ) {
95
+ case '.json' :
96
+ type = 'pkg' ;
97
+ break ;
98
+
99
+ case '.js' :
100
+ type = 'js' ;
101
+ break ;
102
+
103
+ default :
104
+ type = 'rc' ;
105
+ break ;
106
+ }
107
+
108
+ return {
109
+ path : configPath ,
110
+ defaultPath : sgcrcDefault ,
111
+ type,
112
+ } ;
113
+ }
114
+
115
+ private setConfig ( ) : SgcConfig {
116
+ const configPath = this . getConfigPath ( ) ;
117
+ const sgcrcDefault : SgcConfig = Config . safeRead ( configPath . defaultPath ) as SgcConfig ;
118
+
119
+ let config : SgcConfig = sgcrcDefault ;
120
+ let readConfig : SgcConfig | false ;
121
+
122
+ switch ( configPath . type ) {
123
+ case 'js' :
124
+ readConfig = Config . safeRequire ( configPath . path ) ;
125
+ break ;
126
+
127
+ case 'pkg' :
128
+ readConfig = (
129
+ json . readToObjSync < { sgc : SgcConfig } > ( configPath . path )
130
+ || { sgc : false as false }
131
+ ) . sgc ;
132
+ break ;
133
+
134
+ default :
135
+ case 'rc' :
136
+ readConfig = Config . safeRead ( configPath . path ) ;
137
+ }
138
+
139
+ if ( readConfig ) {
140
+ config = readConfig ;
141
+ }
142
+
83
143
// set defaults which are necessary
84
144
const modifiedConfig = merge ( { } , sgcrcDefault , config ) ;
85
145
0 commit comments