1
+ import { CliConfig as CliConfigBase } from './config/config' ;
2
+ import * as chalk from 'chalk' ;
1
3
import * as fs from 'fs' ;
2
4
import * as path from 'path' ;
3
- import * as chalk from 'chalk' ;
4
5
5
6
const schemaPath = path . resolve ( process . env . CLI_ROOT , 'lib/config/schema.json' ) ;
6
7
const schema = require ( schemaPath ) ;
7
8
8
9
export const CLI_CONFIG_FILE_NAME = 'angular-cli.json' ;
9
- export const ARRAY_METHODS = [ 'push' , 'splice' , 'sort' , 'reverse' , 'pop' , 'shift' ] ;
10
+
10
11
11
12
function _findUp ( name : string , from : string ) {
12
13
let currentDir = from ;
@@ -16,175 +17,53 @@ function _findUp(name: string, from: string) {
16
17
return p ;
17
18
}
18
19
19
- currentDir = path . resolve ( currentDir , '..' ) ;
20
+ currentDir = path . dirname ( currentDir ) ;
20
21
}
21
22
22
23
return null ;
23
24
}
24
25
25
26
26
- export class CliConfig {
27
- private _config : any ;
27
+ function getUserHome ( ) {
28
+ return process . env [ ( process . platform == 'win32' ) ? 'USERPROFILE' : 'HOME' ] ;
29
+ }
30
+
28
31
32
+ export class CliConfig extends CliConfigBase {
29
33
constructor ( path ?: string ) {
30
34
if ( path ) {
31
- try {
32
- fs . accessSync ( path ) ;
33
- this . _config = require ( path ) ;
34
- } catch ( e ) {
35
- throw new Error ( `Config file does not exits.` ) ;
36
- }
35
+ return CliConfigBase . fromConfigPath ( path ) as CliConfig ;
37
36
} else {
38
- this . _config = CliConfig . fromProject ( ) ;
39
- }
40
- }
41
-
42
- save ( path : string = CliConfig . _configFilePath ( ) ) {
43
- if ( ! path ) {
44
- throw new Error ( 'Could not find config path.' ) ;
37
+ return CliConfig . fromProject ( ) ;
45
38
}
46
-
47
- fs . writeFileSync ( path , JSON . stringify ( this . _config , null , 2 ) , { encoding : 'utf-8' } ) ;
48
- }
49
-
50
- set ( jsonPath : string , value : any , force : boolean = false ) : boolean {
51
- let method : any = null ;
52
- let splittedPath = jsonPath . split ( '.' ) ;
53
- if ( ARRAY_METHODS . indexOf ( splittedPath [ splittedPath . length - 1 ] ) != - 1 ) {
54
- method = splittedPath [ splittedPath . length - 1 ] ;
55
- splittedPath . splice ( splittedPath . length - 1 , 1 ) ;
56
- jsonPath = splittedPath . join ( '.' ) ;
57
- }
58
-
59
- let { parent, name, remaining } = this . _findParent ( jsonPath ) ;
60
- let properties : any ;
61
- let additionalProperties : boolean ;
62
-
63
- const checkPath = jsonPath . split ( '.' ) . reduce ( ( o , i ) => {
64
- if ( ! o || ! o . properties ) {
65
- throw new Error ( `Invalid config path.` ) ;
66
- }
67
- properties = o . properties ;
68
- additionalProperties = o . additionalProperties ;
69
-
70
- return o . properties [ i ] ;
71
- } , schema ) ;
72
- const configPath = jsonPath . split ( '.' ) . reduce ( ( o , i ) => o [ i ] , this . _config ) ;
73
-
74
- if ( ! properties [ name ] && ! additionalProperties ) {
75
- throw new Error ( `${ name } is not a known property.` ) ;
76
- }
77
-
78
- if ( method ) {
79
- if ( Array . isArray ( configPath ) && checkPath . type === 'array' ) {
80
- [ ] [ method ] . call ( configPath , value ) ;
81
- return true ;
82
- } else {
83
- throw new Error ( `Trying to use array method on non-array property type.` ) ;
84
- }
85
- }
86
-
87
- if ( typeof checkPath . type === 'string' && isNaN ( value ) ) {
88
- parent [ name ] = value ;
89
- return true ;
90
- }
91
-
92
- if ( typeof checkPath . type === 'number' && ! isNaN ( value ) ) {
93
- parent [ name ] = value ;
94
- return true ;
95
- }
96
-
97
- if ( typeof value != checkPath . type ) {
98
- throw new Error ( `Invalid value type. Trying to set ${ typeof value } to ${ path . type } ` ) ;
99
- }
100
- }
101
-
102
- get ( jsonPath : string ) : any {
103
- let { parent, name, remaining } = this . _findParent ( jsonPath ) ;
104
- if ( remaining || ! ( name in parent ) ) {
105
- return null ;
106
- } else {
107
- return parent [ name ] ;
108
- }
109
- }
110
-
111
- private _validatePath ( jsonPath : string ) {
112
- if ( ! jsonPath . match ( / ^ (?: [ - _ \w \d ] + (?: \[ \d + \] ) * \. ) * (?: [ - _ \w \d ] + (?: \[ \d + \] ) * ) $ / ) ) {
113
- throw `Invalid JSON path: "${ jsonPath } "` ;
114
- }
115
- }
116
-
117
- private _findParent ( jsonPath : string ) : { parent : any , name : string | number , remaining ?: string } {
118
- this . _validatePath ( jsonPath ) ;
119
-
120
- let parent : any = null ;
121
- let current : any = this . _config ;
122
-
123
- const splitPath = jsonPath . split ( '.' ) ;
124
- let name : string | number = '' ;
125
-
126
- while ( splitPath . length > 0 ) {
127
- const m = splitPath . shift ( ) . match ( / ^ ( .* ?) (?: \[ ( \d + ) \] ) * $ / ) ;
128
-
129
- name = m [ 1 ] ;
130
- const index : string = m [ 2 ] ;
131
- parent = current ;
132
- current = current [ name ] ;
133
-
134
- if ( current === null || current === undefined ) {
135
- return {
136
- parent,
137
- name,
138
- remaining : ( ! isNaN ( index ) ? `[${ index } ]` : '' ) + splitPath . join ( '.' )
139
- } ;
140
- }
141
-
142
- if ( ! isNaN ( index ) ) {
143
- name = index ;
144
- parent = current ;
145
- current = current [ index ] ;
146
-
147
- if ( current === null || current === undefined ) {
148
- return {
149
- parent,
150
- name,
151
- remaining : splitPath . join ( '.' )
152
- } ;
153
- }
154
- }
155
- }
156
-
157
- return { parent, name } ;
158
39
}
159
40
160
41
private static _configFilePath ( projectPath ?: string ) : string {
161
42
// Find the configuration, either where specified, in the angular-cli project
162
43
// (if it's in node_modules) or from the current process.
163
44
return ( projectPath && _findUp ( CLI_CONFIG_FILE_NAME , projectPath ) )
164
- || _findUp ( CLI_CONFIG_FILE_NAME , __dirname )
165
- || _findUp ( CLI_CONFIG_FILE_NAME , process . cwd ( ) ) ;
45
+ || _findUp ( CLI_CONFIG_FILE_NAME , process . cwd ( ) )
46
+ || _findUp ( CLI_CONFIG_FILE_NAME , __dirname ) ;
166
47
}
167
48
168
- public static fromProject ( ) : any {
169
- const configPath = CliConfig . _configFilePath ( ) ;
49
+ static fromProject ( ) : CliConfig {
50
+ const configPath = this . _configFilePath ( ) ;
51
+ const globalConfigPath = path . join ( getUserHome ( ) , CLI_CONFIG_FILE_NAME ) ;
170
52
171
53
if ( ! configPath ) {
172
54
return { } ;
173
55
}
174
56
175
- let config = require ( configPath ) ;
176
-
177
- if ( config . defaults . sourceDir || config . defaults . prefix ) {
178
- config . apps [ 0 ] . root = config . apps [ 0 ] . root || config . defaults . sourceDir ;
179
- config . apps [ 0 ] . prefix = config . apps [ 0 ] . prefix || config . defaults . prefix ;
180
-
57
+ const cliConfig = CliConfigBase . fromConfigPath ( CliConfig . _configFilePath ( ) , [ globalConfigPath ] ) ;
58
+ if ( cliConfig . alias ( 'apps.0.root' , 'defaults.sourceDir' )
59
+ + cliConfig . alias ( 'apps.0.prefix' , 'defaults.prefix' ) ) {
181
60
console . error ( chalk . yellow (
182
- 'The "defaults.prefix" and "defaults.sourceDir" properties of angular-cli.json '
61
+ 'The "defaults.prefix" and "defaults.sourceDir" properties of angular-cli.json\n '
183
62
+ 'are deprecated in favor of "apps[0].root" and "apps[0].prefix".\n'
184
63
+ 'Please update in order to avoid errors in future versions of angular-cli.'
185
64
) ) ;
186
65
}
187
-
188
- return config ;
66
+
67
+ return cliConfig as CliConfig ;
189
68
}
190
69
}
0 commit comments