@@ -4,35 +4,56 @@ import { join } from 'path';
44
55import Config from '../../config' ;
66
7- const getConfig = ( path : string , env : string ) : any => {
8- const configPath = join ( path , env ) ;
9- let config : any ;
10- try {
11- config = JSON . parse ( JSON . stringify ( require ( configPath ) ) ) ;
12- } catch ( e ) {
13- config = null ;
14- util . log ( util . colors . red ( e . message ) ) ;
7+ /**
8+ * Builds an object consisting of the base configuration provided by confg/seed.config.ts, the additional
9+ * project specific overrides as defined in config/project.config.ts and including the base environment config as defined in env/base.ts
10+ * and the environment specific overrides (for instance if env=dev then as defined in env/dev.ts).
11+ */
12+ export class TemplateLocalsBuilder {
13+ private stringifySystemConfigDev = false ;
14+ private stringifyEnvConfig = true ;
15+
16+ withStringifiedSystemConfigDev ( ) {
17+ this . stringifySystemConfigDev = true ;
18+ return this ;
19+ }
20+ wihtoutStringifiedEnvConfig ( ) {
21+ this . stringifyEnvConfig = false ;
22+ return this ;
1523 }
1624
17- return config ;
18- } ;
1925
20- /**
21- * Returns the project configuration (consisting of the base configuration provided by seed.config.ts and the additional
22- * project specific overrides as defined in project.config.ts)
23- */
24- export function templateLocals ( ) {
25- const configEnvName = argv [ 'env-config' ] || argv [ 'config-env' ] || 'dev' ;
26- const configPath = Config . getPluginConfig ( 'environment-config' ) ;
27- const baseConfig = getConfig ( configPath , 'base' ) ;
28- const config = getConfig ( configPath , configEnvName ) ;
29-
30- if ( ! config ) {
31- throw new Error ( configEnvName + ' is an invalid configuration name' ) ;
26+ build ( ) {
27+ const configEnvName = argv [ 'env-config' ] || argv [ 'config-env' ] || 'dev' ;
28+ const configPath = Config . getPluginConfig ( 'environment-config' ) ;
29+ const envOnlyConfig = this . getConfig ( configPath , configEnvName ) ;
30+ const baseConfig = this . getConfig ( configPath , 'base' ) ;
31+
32+ if ( ! envOnlyConfig ) {
33+ throw new Error ( configEnvName + ' is an invalid configuration name' ) ;
34+ }
35+
36+ const envConfig = Object . assign ( { } , baseConfig , envOnlyConfig ) ;
37+ let locals = Object . assign ( { } ,
38+ Config ,
39+ { ENV_CONFIG : this . stringifyEnvConfig ? JSON . stringify ( envConfig ) : envConfig }
40+ ) ;
41+ if ( this . stringifySystemConfigDev ) {
42+ Object . assign ( locals , { SYSTEM_CONFIG_DEV : JSON . stringify ( Config . SYSTEM_CONFIG_DEV ) } ) ;
43+ }
44+ return locals ;
3245 }
3346
34- return Object . assign ( Config , {
35- ENV_CONFIG : JSON . stringify ( Object . assign ( baseConfig , config ) )
36- } ) ;
37- }
47+ private getConfig ( path : string , env : string ) {
48+ const configPath = join ( path , env ) ;
49+ let config : any ;
50+ try {
51+ config = JSON . parse ( JSON . stringify ( require ( configPath ) ) ) ;
52+ } catch ( e ) {
53+ config = null ;
54+ util . log ( util . colors . red ( e . message ) ) ;
55+ }
3856
57+ return config ;
58+ } ;
59+ }
0 commit comments