@netlify/cache-utils 4.0.0 → 4.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +55 -64
- package/package.json +1 -2
- package/src/fs.js +1 -1
- package/src/manifest.js +5 -10
- package/src/utils/cwd.js +1 -1
package/README.md
CHANGED
|
@@ -8,20 +8,19 @@ Utility for caching files in Netlify Build.
|
|
|
8
8
|
## Simple
|
|
9
9
|
|
|
10
10
|
```js
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
},
|
|
11
|
+
// Restore file/directory cached in previous builds.
|
|
12
|
+
// Does not do anything if:
|
|
13
|
+
// - the file/directory already exists locally
|
|
14
|
+
// - the file/directory has not been cached yet
|
|
15
|
+
export const onPreBuild = async function ({ utils }) {
|
|
16
|
+
await utils.cache.restore('./path/to/file')
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// Cache file/directory for future builds.
|
|
20
|
+
// Does not do anything if:
|
|
21
|
+
// - the file/directory does not exist locally
|
|
22
|
+
export const onPostBuild = async function ({ utils }) {
|
|
23
|
+
await utils.cache.save('./path/to/file')
|
|
25
24
|
}
|
|
26
25
|
```
|
|
27
26
|
|
|
@@ -29,13 +28,12 @@ module.exports = {
|
|
|
29
28
|
|
|
30
29
|
```js
|
|
31
30
|
// Restore/cache several files/directories
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
},
|
|
31
|
+
export const onPreBuild = async function ({ utils }) {
|
|
32
|
+
await utils.cache.restore(['./path/to/file', './path/to/other'])
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export const onPostBuild = async function ({ utils }) {
|
|
36
|
+
await utils.cache.save(['./path/to/file', './path/to/other'])
|
|
39
37
|
}
|
|
40
38
|
```
|
|
41
39
|
|
|
@@ -64,14 +62,13 @@ Only cache the file/directory for a specific amount of time.
|
|
|
64
62
|
|
|
65
63
|
```js
|
|
66
64
|
// Only cache the following file/directory for 1 hour
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
},
|
|
65
|
+
export const onPreBuild = async function ({ utils }) {
|
|
66
|
+
await utils.cache.restore('./path/to/file')
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export const onPostBuild = async function ({ utils }) {
|
|
70
|
+
const ttl = 3600
|
|
71
|
+
await utils.cache.save('./path/to/file', { ttl })
|
|
75
72
|
}
|
|
76
73
|
```
|
|
77
74
|
|
|
@@ -88,15 +85,14 @@ speeds up caching.
|
|
|
88
85
|
// contents has changed. This will speed up cache saving.
|
|
89
86
|
// For example, `package-lock.json` and `yarn.lock` are digest files for the
|
|
90
87
|
// `node_modules` directory.
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
},
|
|
88
|
+
export const onPreBuild = async function ({ utils }) {
|
|
89
|
+
await utils.cache.restore('node_modules')
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export const onPostBuild = async function ({ utils }) {
|
|
93
|
+
await utils.cache.save('node_modules', {
|
|
94
|
+
digests: ['package-lock.json', 'yarn.lock'],
|
|
95
|
+
})
|
|
100
96
|
}
|
|
101
97
|
```
|
|
102
98
|
|
|
@@ -139,10 +135,8 @@ Remove a file/directory from the cache. Useful for cache invalidation.
|
|
|
139
135
|
Returns `false` if the file/directory was not cached yet. Returns `true` otherwise.
|
|
140
136
|
|
|
141
137
|
```js
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
await utils.cache.remove('./path/to/file')
|
|
145
|
-
},
|
|
138
|
+
export const onPostBuild = async function ({ utils }) {
|
|
139
|
+
await utils.cache.remove('./path/to/file')
|
|
146
140
|
}
|
|
147
141
|
```
|
|
148
142
|
|
|
@@ -167,23 +161,22 @@ Returns whether a file/directory is currently cached.
|
|
|
167
161
|
// previously cached or not
|
|
168
162
|
const path = './path/to/file'
|
|
169
163
|
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
},
|
|
164
|
+
export const onPreBuild = async function ({ utils }) {
|
|
165
|
+
if (!(await utils.cache.has(path))) {
|
|
166
|
+
console.log(`File ${path} not cached`)
|
|
167
|
+
return
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
console.log(`About to restore cached file ${path}...`)
|
|
171
|
+
if (await utils.cache.restore('./path/to/file')) {
|
|
172
|
+
console.log(`Restored cached file ${path}`)
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export const onPostBuild = async function ({ utils }) {
|
|
177
|
+
if (await utils.cache.save('./path/to/file')) {
|
|
178
|
+
console.log(`Saved cached file ${path}`)
|
|
179
|
+
}
|
|
187
180
|
}
|
|
188
181
|
```
|
|
189
182
|
|
|
@@ -204,11 +197,9 @@ Returns the absolute paths of the files currently cached. Those are the paths of
|
|
|
204
197
|
being restored), not while being cached.
|
|
205
198
|
|
|
206
199
|
```js
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
console.log('Cached files', files)
|
|
211
|
-
},
|
|
200
|
+
export const onPreBuild = async function ({ utils }) {
|
|
201
|
+
const files = await utils.cache.list()
|
|
202
|
+
console.log('Cached files', files)
|
|
212
203
|
}
|
|
213
204
|
```
|
|
214
205
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@netlify/cache-utils",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.1.0",
|
|
4
4
|
"description": "Utility for caching files in Netlify Build",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": "./src/main.js",
|
|
@@ -51,7 +51,6 @@
|
|
|
51
51
|
"globby": "^11.0.0",
|
|
52
52
|
"junk": "^3.1.0",
|
|
53
53
|
"locate-path": "^6.0.0",
|
|
54
|
-
"make-dir": "^3.1.0",
|
|
55
54
|
"move-file": "^2.0.0",
|
|
56
55
|
"path-exists": "^4.0.0",
|
|
57
56
|
"readdirp": "^3.4.0"
|
package/src/fs.js
CHANGED
package/src/manifest.js
CHANGED
|
@@ -1,17 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { promises as fs } from 'fs'
|
|
2
2
|
import { dirname } from 'path'
|
|
3
|
-
import { promisify } from 'util'
|
|
4
3
|
|
|
5
4
|
import del from 'del'
|
|
6
|
-
import makeDir from 'make-dir'
|
|
7
5
|
import pathExists from 'path-exists'
|
|
8
6
|
|
|
9
7
|
import { getExpires, checkExpires } from './expire.js'
|
|
10
8
|
import { getHash } from './hash.js'
|
|
11
9
|
|
|
12
|
-
const pWriteFile = promisify(writeFile)
|
|
13
|
-
const pReadFile = promisify(readFile)
|
|
14
|
-
|
|
15
10
|
// Retrieve cache manifest of a file to cache, which contains the file/directory
|
|
16
11
|
// contents hash and the `expires` date.
|
|
17
12
|
export const getManifestInfo = async function ({ cachePath, move, ttl, digests }) {
|
|
@@ -30,14 +25,14 @@ const isIdentical = async function ({ hash, manifestPath, manifestString }) {
|
|
|
30
25
|
return false
|
|
31
26
|
}
|
|
32
27
|
|
|
33
|
-
const oldManifestString = await
|
|
28
|
+
const oldManifestString = await fs.readFile(manifestPath, 'utf8')
|
|
34
29
|
return oldManifestString === manifestString
|
|
35
30
|
}
|
|
36
31
|
|
|
37
32
|
// Persist the cache manifest to filesystem
|
|
38
33
|
export const writeManifest = async function ({ manifestPath, manifestString }) {
|
|
39
|
-
await
|
|
40
|
-
await
|
|
34
|
+
await fs.mkdir(dirname(manifestPath), { recursive: true })
|
|
35
|
+
await fs.writeFile(manifestPath, manifestString)
|
|
41
36
|
}
|
|
42
37
|
|
|
43
38
|
// Remove the cache manifest from filesystem
|
|
@@ -70,7 +65,7 @@ export const isExpired = async function (cachePath) {
|
|
|
70
65
|
|
|
71
66
|
const readManifest = async function (cachePath) {
|
|
72
67
|
const manifestPath = getManifestPath(cachePath)
|
|
73
|
-
const manifestString = await
|
|
68
|
+
const manifestString = await fs.readFile(manifestPath)
|
|
74
69
|
const manifest = JSON.parse(manifestString)
|
|
75
70
|
return manifest
|
|
76
71
|
}
|