@@ -45,6 +45,8 @@ class BuildConfigTask {
4545 * @param done Function to call when done.
4646 */
4747 run ( path , done ) {
48+ const self = this ;
49+
4850 // Get the last commit.
4951 exec ( 'git log -1 --pretty=format:"%H"' , ( err , commit , stderr ) => {
5052 if ( err ) {
@@ -61,28 +63,9 @@ class BuildConfigTask {
6163 let contents = LICENSE + '// tslint:disable: variable-name\n' + 'export class CoreConfigConstants {\n' ;
6264
6365 for ( let key in config ) {
64- let value = config [ key ] ;
65-
66- if ( typeof value == 'string' ) {
67- // Wrap the string in ' and escape them.
68- value = "'" + value . replace ( / ( [ ^ \\ ] ) ' / g, "$1\\'" ) + "'" ;
69- } else if ( typeof value != 'number' && typeof value != 'boolean' ) {
70- // Stringify with 4 spaces of indentation, and then add 4 more spaces in each line.
71- value = JSON . stringify ( value , null , 4 ) . replace ( / ^ (?: ) / gm, ' ' ) . replace ( / ^ (?: } ) / gm, ' }' ) ;
72- // Replace " by ' in values.
73- value = value . replace ( / : " ( [ ^ " ] * ) " / g, ": '$1'" ) ;
74-
75- // Check if the keys have "-" in it.
76- const matches = value . match ( / " ( [ ^ " ] * \- [ ^ " ] * ) " : / g) ;
77- if ( matches ) {
78- // Replace " by ' in keys. We cannot remove them because keys have chars like '-'.
79- value = value . replace ( / " ( [ ^ " ] * ) " : / g, "'$1':" ) ;
80- } else {
81- // Remove ' in keys.
82- value = value . replace ( / " ( [ ^ " ] * ) " : / g, "$1:" ) ;
83- }
84-
85- // Add type any to the key.
66+ let value = self . transformValue ( config [ key ] ) ;
67+
68+ if ( typeof config [ key ] != 'number' && typeof config [ key ] != 'boolean' && typeof config [ key ] != 'string' ) {
8669 key = key + ': any' ;
8770 }
8871
@@ -108,6 +91,48 @@ class BuildConfigTask {
10891 . on ( 'end' , done ) ;
10992 } ) ;
11093 }
94+
95+
96+ /**
97+ * Recursively transform a config value into personalized TS.
98+ *
99+ * @param value Value to convert
100+ * @return Converted value.
101+ */
102+ transformValue ( value ) {
103+ if ( typeof value == 'string' ) {
104+ // Wrap the string in ' and escape them.
105+ return "'" + value . replace ( / ( [ ^ \\ ] ) ' / g, "$1\\'" ) + "'" ;
106+ }
107+
108+ if ( typeof value != 'number' && typeof value != 'boolean' ) {
109+ const isArray = Array . isArray ( value ) ;
110+ let contents = '' ;
111+
112+ let quoteKeys = false ;
113+ if ( ! isArray ) {
114+ for ( let key in value ) {
115+ if ( key . indexOf ( '-' ) >= 0 ) {
116+ quoteKeys = true ;
117+ break ;
118+ }
119+ }
120+ }
121+
122+ for ( let key in value ) {
123+ value [ key ] = this . transformValue ( value [ key ] ) ;
124+
125+ const quotedKey = quoteKeys ? "'" + key + "'" : key ;
126+ contents += ' ' + ( isArray ? '' : quotedKey + ': ' ) + value [ key ] + ",\n" ;
127+ }
128+
129+ contents += ( isArray ? ']' : '}' ) ;
130+
131+ return ( isArray ? '[' : '{' ) + "\n" + contents . replace ( / ^ / gm, ' ' ) ;
132+ }
133+
134+ return value ;
135+ }
111136}
112137
113138module . exports = BuildConfigTask ;
0 commit comments