@@ -2,34 +2,40 @@ const chalk = require("chalk");
2
2
const fs = require ( "fs-extra" ) ;
3
3
const lodashMerge = require ( "lodash.merge" ) ;
4
4
const TemplatePath = require ( "./TemplatePath" ) ;
5
- const mainRootConfig = require ( "../config.js" ) ;
6
5
const eleventyConfig = require ( "./EleventyConfig" ) ;
7
6
const debug = require ( "debug" ) ( "Eleventy:TemplateConfig" ) ;
8
7
9
8
class TemplateConfig {
10
- constructor ( rootConfig , projectConfigPath ) {
9
+ constructor ( customRootConfig , localProjectConfigPath ) {
11
10
this . overrides = { } ;
12
- this . projectConfigPath = projectConfigPath || ".eleventy.js" ;
13
- this . rootConfig = rootConfig || mainRootConfig ;
14
- if ( rootConfig ) {
11
+ this . localProjectConfigPath = localProjectConfigPath || ".eleventy.js" ;
12
+
13
+ if ( customRootConfig ) {
14
+ this . customRootConfig = customRootConfig ;
15
15
debug ( "Warning: Using custom root config!" ) ;
16
+ } else {
17
+ this . customRootConfig = null ;
16
18
}
19
+ this . initializeRootConfig ( ) ;
20
+ this . config = this . mergeConfig ( this . localProjectConfigPath ) ;
21
+ }
17
22
18
- if ( typeof this . rootConfig === "function" ) {
19
- this . rootConfig = this . rootConfig ( eleventyConfig ) ;
20
- // debug( "rootConfig is a function, after calling, eleventyConfig is %o", eleventyConfig );
21
- }
22
- debug ( "rootConfig %o" , this . rootConfig ) ;
23
+ getLocalProjectConfigFile ( ) {
24
+ return this . localProjectConfigPath ;
25
+ }
23
26
24
- this . config = this . mergeConfig ( this . projectConfigPath ) ;
27
+ reset ( ) {
28
+ eleventyConfig . reset ( ) ;
29
+ this . initializeRootConfig ( ) ;
30
+ this . config = this . mergeConfig ( this . localProjectConfigPath ) ;
25
31
}
26
32
27
33
getConfig ( ) {
28
34
return this . config ;
29
35
}
30
36
31
37
setProjectConfigPath ( path ) {
32
- this . projectConfigPath = path ;
38
+ this . localProjectConfigPath = path ;
33
39
34
40
this . config = this . mergeConfig ( path ) ;
35
41
}
@@ -40,16 +46,31 @@ class TemplateConfig {
40
46
this . config . pathPrefix = pathPrefix ;
41
47
}
42
48
43
- mergeConfig ( projectConfigPath ) {
49
+ initializeRootConfig ( ) {
50
+ this . rootConfig = this . customRootConfig || require ( "../config.js" ) ;
51
+
52
+ if ( typeof this . rootConfig === "function" ) {
53
+ this . rootConfig = this . rootConfig ( eleventyConfig ) ;
54
+ // debug( "rootConfig is a function, after calling, eleventyConfig is %o", eleventyConfig );
55
+ }
56
+ debug ( "rootConfig %o" , this . rootConfig ) ;
57
+ }
58
+
59
+ mergeConfig ( localProjectConfigPath ) {
44
60
let overrides = [ "templateFormats" ] ;
45
61
let localConfig = { } ;
46
62
let path = TemplatePath . normalize (
47
- TemplatePath . getWorkingDir ( ) + "/" + projectConfigPath
63
+ TemplatePath . getWorkingDir ( ) + "/" + localProjectConfigPath
48
64
) ;
49
65
debug ( `Merging config with ${ path } ` ) ;
50
66
51
67
if ( fs . existsSync ( path ) ) {
52
68
try {
69
+ // remove from require cache so it will grab a fresh copy
70
+ if ( path in require . cache ) {
71
+ delete require . cache [ path ] ;
72
+ }
73
+
53
74
localConfig = require ( path ) ;
54
75
// debug( "localConfig require return value: %o", localConfig );
55
76
} catch ( err ) {
0 commit comments