@netlify/cache-utils 4.0.0 → 4.1.3

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 CHANGED
@@ -8,20 +8,19 @@ Utility for caching files in Netlify Build.
8
8
  ## Simple
9
9
 
10
10
  ```js
11
- module.exports = {
12
- // Restore file/directory cached in previous builds.
13
- // Does not do anything if:
14
- // - the file/directory already exists locally
15
- // - the file/directory has not been cached yet
16
- async onPreBuild({ utils }) {
17
- await utils.cache.restore('./path/to/file')
18
- },
19
- // Cache file/directory for future builds.
20
- // Does not do anything if:
21
- // - the file/directory does not exist locally
22
- async onPostBuild({ utils }) {
23
- await utils.cache.save('./path/to/file')
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
- module.exports = {
33
- async onPreBuild({ utils }) {
34
- await utils.cache.restore(['./path/to/file', './path/to/other'])
35
- },
36
- async onPostBuild({ utils }) {
37
- await utils.cache.save(['./path/to/file', './path/to/other'])
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
- module.exports = {
68
- async onPreBuild({ utils }) {
69
- await utils.cache.restore('./path/to/file')
70
- },
71
- async onPostBuild({ utils }) {
72
- const ttl = 3600
73
- await utils.cache.save('./path/to/file', { ttl })
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
- module.exports = {
92
- async onPreBuild({ utils }) {
93
- await utils.cache.restore('node_modules')
94
- },
95
- async onPostBuild({ utils }) {
96
- await utils.cache.save('node_modules', {
97
- digests: ['package-lock.json', 'yarn.lock'],
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
- module.exports = {
143
- async onPostBuild({ utils }) {
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
- module.exports = {
171
- async onPreBuild({ utils }) {
172
- if (!(await utils.cache.has(path))) {
173
- console.log(`File ${path} not cached`)
174
- return
175
- }
176
-
177
- console.log(`About to restore cached file ${path}...`)
178
- if (await utils.cache.restore('./path/to/file')) {
179
- console.log(`Restored cached file ${path}`)
180
- }
181
- },
182
- async onPostBuild({ utils }) {
183
- if (await utils.cache.save('./path/to/file')) {
184
- console.log(`Saved cached file ${path}`)
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
- module.exports = {
208
- async onPreBuild({ utils }) {
209
- const files = await utils.cache.list()
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.0.0",
3
+ "version": "4.1.3",
4
4
  "description": "Utility for caching files in Netlify Build",
5
5
  "type": "module",
6
6
  "exports": "./src/main.js",
@@ -46,14 +46,13 @@
46
46
  "license": "MIT",
47
47
  "dependencies": {
48
48
  "cpy": "^8.1.0",
49
- "del": "^5.1.0",
49
+ "del": "^6.0.0",
50
50
  "get-stream": "^6.0.0",
51
51
  "globby": "^11.0.0",
52
52
  "junk": "^3.1.0",
53
- "locate-path": "^6.0.0",
54
- "make-dir": "^3.1.0",
53
+ "locate-path": "^7.0.0",
55
54
  "move-file": "^2.0.0",
56
- "path-exists": "^4.0.0",
55
+ "path-exists": "^5.0.0",
57
56
  "readdirp": "^3.4.0"
58
57
  },
59
58
  "devDependencies": {
package/src/fs.js CHANGED
@@ -1,14 +1,11 @@
1
- import { stat } from 'fs'
1
+ import { promises as fs } from 'fs'
2
2
  import { basename, dirname } from 'path'
3
- import { promisify } from 'util'
4
3
 
5
4
  import cpy from 'cpy'
6
5
  import globby from 'globby'
7
6
  import junk from 'junk'
8
7
  import moveFile from 'move-file'
9
8
 
10
- const pStat = promisify(stat)
11
-
12
9
  // Move or copy a cached file/directory from/to a local one
13
10
  export const moveCacheFile = async function (src, dest, move) {
14
11
  // Moving is faster but removes the source files locally
@@ -58,6 +55,6 @@ const getSrcGlob = async function (src) {
58
55
 
59
56
  const getStat = async function (src) {
60
57
  try {
61
- return await pStat(src)
62
- } catch (error) {}
58
+ return await fs.stat(src)
59
+ } catch {}
63
60
  }
package/src/hash.js CHANGED
@@ -2,7 +2,7 @@ import { createHash } from 'crypto'
2
2
  import { createReadStream } from 'fs'
3
3
 
4
4
  import getStream from 'get-stream'
5
- import locatePath from 'locate-path'
5
+ import { locatePath } from 'locate-path'
6
6
 
7
7
  // Caching a big directory like `node_modules` is slow. However those can
8
8
  // sometime be represented by a digest file such as `package-lock.json`. If this
package/src/manifest.js CHANGED
@@ -1,17 +1,12 @@
1
- import { writeFile, readFile } from 'fs'
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
- import pathExists from 'path-exists'
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 pReadFile(manifestPath, 'utf8')
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 makeDir(dirname(manifestPath))
40
- await pWriteFile(manifestPath, manifestString)
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 pReadFile(manifestPath, 'utf8')
68
+ const manifestString = await fs.readFile(manifestPath)
74
69
  const manifest = JSON.parse(manifestString)
75
70
  return manifest
76
71
  }
package/src/utils/cwd.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { normalize } from 'path'
2
2
  import process from 'process'
3
3
 
4
- import pathExists from 'path-exists'
4
+ import { pathExists } from 'path-exists'
5
5
 
6
6
  // Like `process.cwd()` but safer when current directory is wrong
7
7
  export const safeGetCwd = async function (cwdOpt) {
@@ -13,7 +13,7 @@ export const safeGetCwd = async function (cwdOpt) {
13
13
  }
14
14
 
15
15
  return cwd
16
- } catch (error) {
16
+ } catch {
17
17
  return ''
18
18
  }
19
19
  }