1
1
const path = require ( 'path' )
2
2
const fs = require ( 'fs' )
3
3
4
- const getCacheDirs = ( constants , inputs ) => {
5
- const getCacheDirs__ = ( constants , input ) => {
6
- let cacheDirs = [ ]
4
+ const getCacheDirs__ = ( base , input ) => {
5
+ let cacheDirs = [ ]
7
6
8
- if ( typeof input === 'string' ) {
9
- cacheDirs . push ( path . normalize ( constants . PUBLISH_DIR + '/' + input ) )
10
- } else if ( Array . isArray ( input ) ) {
11
- input . map ( ( x ) => {
12
- cacheDirs . push ( path . normalize ( constants . PUBLISH_DIR + '/' + x ) )
13
- } )
14
- } else {
15
- console . log ( `Warning: Unsupported value in inputs. Ignoring ${ input } ` )
16
- }
17
-
18
- return cacheDirs
7
+ if ( typeof input === 'string' ) {
8
+ cacheDirs . push ( path . normalize ( base + '/' + input ) )
9
+ } else if ( Array . isArray ( input ) ) {
10
+ input . map ( ( x ) => {
11
+ cacheDirs . push ( path . normalize ( base + '/' + x ) )
12
+ } )
13
+ } else {
14
+ console . log ( `Warning: Unsupported value in inputs. Ignoring ${ input } ` )
19
15
}
20
16
17
+ return cacheDirs
18
+ }
19
+
20
+ const getCacheDirs = ( base , inputs ) => {
21
21
return [
22
- ...getCacheDirs__ ( constants , inputs . img_cache_local_dir ) ,
23
- ...getCacheDirs__ ( constants , inputs . img_cache_remote_dir ) ,
24
- ...getCacheDirs__ ( constants , inputs . other_cache_dir ) ,
22
+ ...getCacheDirs__ ( base , inputs . img_cache_local_dir ) ,
23
+ ...getCacheDirs__ ( base , inputs . img_cache_remote_dir ) ,
24
+ ...getCacheDirs__ ( base , inputs . other_cache_dir ) ,
25
25
]
26
26
}
27
27
28
28
const getHttpHeaders = ( inputs ) => {
29
29
let httpHeader = ''
30
30
31
- inputs . img_cache_local_dir . map ( ( x ) => {
31
+ getCacheDirs__ ( '.' , inputs . img_cache_local_dir ) . map ( ( x ) => {
32
32
httpHeader += `
33
33
${ x } /*
34
34
cache-control: public
@@ -49,11 +49,11 @@ module.exports = {
49
49
)
50
50
}
51
51
52
- const cacheDirs = getCacheDirs ( constants , inputs )
52
+ const cacheDirs = getCacheDirs ( constants . PUBLISH_DIR , inputs )
53
53
cacheDirs . map ( ( x ) => {
54
54
if ( fs . existsSync ( x ) ) {
55
55
console . log (
56
- `Warning: directory ${ x } already exists before restoring caches. It will be replaced.` ,
56
+ `Warning: directory ${ x } already exists before restoring caches. It will be replaced if it exists in the cache .` ,
57
57
)
58
58
}
59
59
} )
@@ -69,27 +69,27 @@ module.exports = {
69
69
} ,
70
70
71
71
async onPostBuild ( { constants, inputs, utils } ) {
72
- if ( inputs . img_cache_httpHeader ) {
73
- fs . appendFile (
74
- `${ constants . PUBLISH_DIR } /_headers` ,
75
- getHttpHeaders ( inputs ) ,
76
- ( err ) => {
77
- if ( err ) throw err
78
- console . log ( 'Saved http header' )
79
- } ,
80
- )
81
- }
82
-
83
- const cacheDirs = getCacheDirs ( constants , inputs )
72
+ const cacheDirs = getCacheDirs ( constants . PUBLISH_DIR , inputs )
84
73
85
74
if ( await utils . cache . save ( cacheDirs ) ) {
86
75
console . log ( 'Saving 11ty directories to cache:' )
87
76
cacheDirs . map ( ( x ) => {
88
77
console . log ( '- ' + x )
89
78
} )
79
+
80
+ if ( inputs . img_cache_httpHeader ) {
81
+ fs . appendFile (
82
+ `${ constants . PUBLISH_DIR } /_headers` ,
83
+ getHttpHeaders ( inputs ) ,
84
+ ( err ) => {
85
+ if ( err ) throw err
86
+ console . log ( 'Saved http header' )
87
+ } ,
88
+ )
89
+ }
90
90
} else {
91
91
console . log (
92
- `Warning: 11ty build not found. Is you publish directory set correctly? “${ constants . PUBLISH_DIR } ”` ,
92
+ `Warning: Unable to save 11ty cache. Build not found or is empty . Is you publish directory set correctly? “${ constants . PUBLISH_DIR } ”` ,
93
93
)
94
94
}
95
95
} ,
0 commit comments