@@ -4,6 +4,14 @@ const path = require('path');
4
4
const os = require ( 'os' ) ;
5
5
const { readJson, writeJson } = require ( './file' ) ;
6
6
7
+ const GLOBAL_CONFIG = Symbol ( 'globalConfig' ) ;
8
+ const PROJECT_CONFIG = Symbol ( 'projectConfig' ) ;
9
+ const LOCAL_CONFIG = Symbol ( 'localConfig' ) ;
10
+
11
+ exports . GLOBAL_CONFIG = GLOBAL_CONFIG ;
12
+ exports . PROJECT_CONFIG = PROJECT_CONFIG ;
13
+ exports . LOCAL_CONFIG = LOCAL_CONFIG ;
14
+
7
15
function getNcurcPath ( ) {
8
16
if ( process . env . XDG_CONFIG_HOME !== 'undefined' &&
9
17
process . env . XDG_CONFIG_HOME !== undefined ) {
@@ -15,33 +23,40 @@ function getNcurcPath() {
15
23
exports . getNcurcPath = getNcurcPath ;
16
24
17
25
exports . getMergedConfig = function ( dir , home ) {
18
- const globalConfig = exports . getConfig ( true , home ) ;
19
- const localConfig = exports . getConfig ( false , dir ) ;
20
- return Object . assign ( globalConfig , localConfig ) ;
26
+ const globalConfig = exports . getConfig ( GLOBAL_CONFIG , home ) ;
27
+ const projectConfig = exports . getConfig ( PROJECT_CONFIG , dir ) ;
28
+ const localConfig = exports . getConfig ( LOCAL_CONFIG , dir ) ;
29
+ return Object . assign ( globalConfig , projectConfig , localConfig ) ;
21
30
} ;
22
31
23
- exports . getConfig = function ( isGlobal , dir ) {
24
- const configPath = exports . getConfigPath ( isGlobal , dir ) ;
32
+ exports . getConfig = function ( configType , dir ) {
33
+ const configPath = exports . getConfigPath ( configType , dir ) ;
25
34
return readJson ( configPath ) ;
26
35
} ;
27
36
28
- exports . getConfigPath = function ( isGlobal , dir ) {
29
- if ( ! isGlobal ) {
30
- const ncuDir = exports . getNcuDir ( dir ) ;
31
- const configPath = path . join ( ncuDir , 'config' ) ;
32
- return configPath ;
37
+ exports . getConfigPath = function ( configType , dir ) {
38
+ switch ( configType ) {
39
+ case GLOBAL_CONFIG :
40
+ return getNcurcPath ( ) ;
41
+ case PROJECT_CONFIG :
42
+ const projectRcPath = path . join ( dir || process . cwd ( ) , '.ncurc' ) ;
43
+ return projectRcPath ;
44
+ case LOCAL_CONFIG :
45
+ const ncuDir = exports . getNcuDir ( dir ) ;
46
+ const configPath = path . join ( ncuDir , 'config' ) ;
47
+ return configPath ;
48
+ default :
49
+ throw Error ( 'Invalid configType' ) ;
33
50
}
34
-
35
- return getNcurcPath ( ) ;
36
51
} ;
37
52
38
- exports . writeConfig = function ( isGlobal , obj , dir ) {
39
- writeJson ( exports . getConfigPath ( isGlobal , dir ) , obj ) ;
53
+ exports . writeConfig = function ( configType , obj , dir ) {
54
+ writeJson ( exports . getConfigPath ( configType , dir ) , obj ) ;
40
55
} ;
41
56
42
- exports . updateConfig = function ( isGlobal , obj , dir ) {
43
- const config = exports . getConfig ( isGlobal , dir ) ;
44
- const configPath = exports . getConfigPath ( isGlobal , dir ) ;
57
+ exports . updateConfig = function ( configType , obj , dir ) {
58
+ const config = exports . getConfig ( configType , dir ) ;
59
+ const configPath = exports . getConfigPath ( configType , dir ) ;
45
60
writeJson ( configPath , Object . assign ( config , obj ) ) ;
46
61
} ;
47
62
0 commit comments