@netlify/cache-utils 3.0.0 → 4.1.1

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,8 +1,10 @@
1
1
  {
2
2
  "name": "@netlify/cache-utils",
3
- "version": "3.0.0",
3
+ "version": "4.1.1",
4
4
  "description": "Utility for caching files in Netlify Build",
5
- "main": "src/main.js",
5
+ "type": "module",
6
+ "exports": "./src/main.js",
7
+ "main": "./src/main.js",
6
8
  "files": [
7
9
  "src/**/*.js"
8
10
  ],
@@ -43,14 +45,12 @@
43
45
  },
44
46
  "license": "MIT",
45
47
  "dependencies": {
46
- "array-flat-polyfill": "^1.0.1",
47
48
  "cpy": "^8.1.0",
48
49
  "del": "^5.1.0",
49
50
  "get-stream": "^6.0.0",
50
51
  "globby": "^11.0.0",
51
52
  "junk": "^3.1.0",
52
53
  "locate-path": "^6.0.0",
53
- "make-dir": "^3.1.0",
54
54
  "move-file": "^2.0.0",
55
55
  "path-exists": "^4.0.0",
56
56
  "readdirp": "^3.4.0"
package/src/dir.js CHANGED
@@ -1,12 +1,8 @@
1
- 'use strict'
2
-
3
- const { resolve } = require('path')
1
+ import { resolve } from 'path'
4
2
 
5
3
  // Retrieve the cache directory location
6
- const getCacheDir = function ({ cacheDir = DEFAULT_CACHE_DIR, cwd = '.' } = {}) {
4
+ export const getCacheDir = function ({ cacheDir = DEFAULT_CACHE_DIR, cwd = '.' } = {}) {
7
5
  return resolve(cwd, cacheDir)
8
6
  }
9
7
 
10
8
  const DEFAULT_CACHE_DIR = '.netlify/cache/'
11
-
12
- module.exports = { getCacheDir }
package/src/expire.js CHANGED
@@ -1,7 +1,5 @@
1
- 'use strict'
2
-
3
1
  // Retrieve the expiration date when caching a file
4
- const getExpires = function (ttl) {
2
+ export const getExpires = function (ttl) {
5
3
  if (!Number.isInteger(ttl) || ttl < 1) {
6
4
  return
7
5
  }
@@ -12,8 +10,6 @@ const getExpires = function (ttl) {
12
10
  const SECS_TO_MSECS = 1e3
13
11
 
14
12
  // Check if a file about to be restored is expired
15
- const checkExpires = function (expires) {
13
+ export const checkExpires = function (expires) {
16
14
  return expires !== undefined && Date.now() > expires
17
15
  }
18
-
19
- module.exports = { getExpires, checkExpires }
package/src/fs.js CHANGED
@@ -1,18 +1,13 @@
1
- 'use strict'
1
+ import { promises as fs } from 'fs'
2
+ import { basename, dirname } from 'path'
2
3
 
3
- const { stat } = require('fs')
4
- const { basename, dirname } = require('path')
5
- const { promisify } = require('util')
6
-
7
- const cpy = require('cpy')
8
- const globby = require('globby')
9
- const junk = require('junk')
10
- const moveFile = require('move-file')
11
-
12
- const pStat = promisify(stat)
4
+ import cpy from 'cpy'
5
+ import globby from 'globby'
6
+ import junk from 'junk'
7
+ import moveFile from 'move-file'
13
8
 
14
9
  // Move or copy a cached file/directory from/to a local one
15
- const moveCacheFile = async function (src, dest, move) {
10
+ export const moveCacheFile = async function (src, dest, move) {
16
11
  // Moving is faster but removes the source files locally
17
12
  if (move) {
18
13
  return moveFile(src, dest, { overwrite: false })
@@ -23,7 +18,7 @@ const moveCacheFile = async function (src, dest, move) {
23
18
  }
24
19
 
25
20
  // Non-existing files and empty directories are always skipped
26
- const hasFiles = async function (src) {
21
+ export const hasFiles = async function (src) {
27
22
  const { srcGlob, cwd, isDir } = await getSrcGlob(src)
28
23
  return srcGlob !== undefined && !(await isEmptyDir({ srcGlob, cwd, isDir }))
29
24
  }
@@ -60,8 +55,6 @@ const getSrcGlob = async function (src) {
60
55
 
61
56
  const getStat = async function (src) {
62
57
  try {
63
- return await pStat(src)
64
- } catch (error) {}
58
+ return await fs.stat(src)
59
+ } catch {}
65
60
  }
66
-
67
- module.exports = { moveCacheFile, hasFiles }
package/src/hash.js CHANGED
@@ -1,15 +1,13 @@
1
- 'use strict'
1
+ import { createHash } from 'crypto'
2
+ import { createReadStream } from 'fs'
2
3
 
3
- const { createHash } = require('crypto')
4
- const { createReadStream } = require('fs')
5
-
6
- const getStream = require('get-stream')
7
- const locatePath = require('locate-path')
4
+ import getStream from 'get-stream'
5
+ import locatePath from 'locate-path'
8
6
 
9
7
  // Caching a big directory like `node_modules` is slow. However those can
10
8
  // sometime be represented by a digest file such as `package-lock.json`. If this
11
9
  // has not changed, we don't need to save cache again.
12
- const getHash = async function (digests, move) {
10
+ export const getHash = async function (digests, move) {
13
11
  // Moving files is faster than computing hashes
14
12
  if (move || digests.length === 0) {
15
13
  return
@@ -36,5 +34,3 @@ const hashFile = async function (path) {
36
34
  // We need a hashing algoritm that's as fast as possible.
37
35
  // Userland CRC32 implementations are actually slower than Node.js SHA1.
38
36
  const HASH_ALGO = 'sha1'
39
-
40
- module.exports = { getHash }
package/src/list.js CHANGED
@@ -1,15 +1,13 @@
1
- 'use strict'
1
+ import { join } from 'path'
2
2
 
3
- const { join } = require('path')
3
+ import readdirp from 'readdirp'
4
4
 
5
- const readdirp = require('readdirp')
6
-
7
- const { getCacheDir } = require('./dir')
8
- const { isManifest } = require('./manifest')
9
- const { getBases } = require('./path')
5
+ import { getCacheDir } from './dir.js'
6
+ import { isManifest } from './manifest.js'
7
+ import { getBases } from './path.js'
10
8
 
11
9
  // List all cached files/directories, at the top-level
12
- const list = async function ({ cacheDir, cwd: cwdOpt, depth = DEFAULT_DEPTH } = {}) {
10
+ export const list = async function ({ cacheDir, cwd: cwdOpt, depth = DEFAULT_DEPTH } = {}) {
13
11
  const bases = await getBases(cwdOpt)
14
12
  const cacheDirA = getCacheDir({ cacheDir, cwd: cwdOpt })
15
13
  const files = await Promise.all(bases.map(({ name, base }) => listBase({ name, base, cacheDir: cacheDirA, depth })))
@@ -29,5 +27,3 @@ const listBase = async function ({ name, base, cacheDir, depth }) {
29
27
  const fileFilter = function ({ basename }) {
30
28
  return !isManifest(basename)
31
29
  }
32
-
33
- module.exports = { list }
package/src/main.js CHANGED
@@ -1,14 +1,13 @@
1
- 'use strict'
1
+ import del from 'del'
2
2
 
3
- require('./utils/polyfills')
3
+ import { getCacheDir } from './dir.js'
4
+ import { moveCacheFile, hasFiles } from './fs.js'
5
+ import { list } from './list.js'
6
+ import { getManifestInfo, writeManifest, removeManifest, isExpired } from './manifest.js'
7
+ import { parsePath } from './path.js'
4
8
 
5
- const del = require('del')
6
-
7
- const { getCacheDir } = require('./dir')
8
- const { moveCacheFile, hasFiles } = require('./fs')
9
- const { list } = require('./list')
10
- const { getManifestInfo, writeManifest, removeManifest, isExpired } = require('./manifest')
11
- const { parsePath } = require('./path')
9
+ export { getCacheDir } from './dir.js'
10
+ export { list } from './list.js'
12
11
 
13
12
  // Cache a file
14
13
  const saveOne = async function (
@@ -86,13 +85,13 @@ const allowMany = async function (func, paths, ...args) {
86
85
  return results.some(Boolean)
87
86
  }
88
87
 
89
- const save = allowMany.bind(null, saveOne)
90
- const restore = allowMany.bind(null, restoreOne)
91
- const remove = allowMany.bind(null, removeOne)
92
- const has = allowMany.bind(null, hasOne)
88
+ export const save = allowMany.bind(null, saveOne)
89
+ export const restore = allowMany.bind(null, restoreOne)
90
+ export const remove = allowMany.bind(null, removeOne)
91
+ export const has = allowMany.bind(null, hasOne)
93
92
 
94
93
  // Change `opts` default values
95
- const bindOpts = function (opts) {
94
+ export const bindOpts = function (opts) {
96
95
  return {
97
96
  save: (paths, optsA) => save(paths, { ...opts, ...optsA }),
98
97
  restore: (paths, optsA) => restore(paths, { ...opts, ...optsA }),
@@ -102,5 +101,3 @@ const bindOpts = function (opts) {
102
101
  getCacheDir: (optsA) => getCacheDir({ ...opts, ...optsA }),
103
102
  }
104
103
  }
105
-
106
- module.exports = { save, restore, remove, has, list, getCacheDir, bindOpts }
package/src/manifest.js CHANGED
@@ -1,22 +1,15 @@
1
- 'use strict'
1
+ import { promises as fs } from 'fs'
2
+ import { dirname } from 'path'
2
3
 
3
- const { writeFile, readFile } = require('fs')
4
- const { dirname } = require('path')
5
- const { promisify } = require('util')
4
+ import del from 'del'
5
+ import pathExists from 'path-exists'
6
6
 
7
- const del = require('del')
8
- const makeDir = require('make-dir')
9
- const pathExists = require('path-exists')
10
-
11
- const { getExpires, checkExpires } = require('./expire')
12
- const { getHash } = require('./hash')
13
-
14
- const pWriteFile = promisify(writeFile)
15
- const pReadFile = promisify(readFile)
7
+ import { getExpires, checkExpires } from './expire.js'
8
+ import { getHash } from './hash.js'
16
9
 
17
10
  // Retrieve cache manifest of a file to cache, which contains the file/directory
18
11
  // contents hash and the `expires` date.
19
- const getManifestInfo = async function ({ cachePath, move, ttl, digests }) {
12
+ export const getManifestInfo = async function ({ cachePath, move, ttl, digests }) {
20
13
  const manifestPath = getManifestPath(cachePath)
21
14
  const expires = getExpires(ttl)
22
15
  const hash = await getHash(digests, move)
@@ -32,18 +25,18 @@ const isIdentical = async function ({ hash, manifestPath, manifestString }) {
32
25
  return false
33
26
  }
34
27
 
35
- const oldManifestString = await pReadFile(manifestPath, 'utf8')
28
+ const oldManifestString = await fs.readFile(manifestPath, 'utf8')
36
29
  return oldManifestString === manifestString
37
30
  }
38
31
 
39
32
  // Persist the cache manifest to filesystem
40
- const writeManifest = async function ({ manifestPath, manifestString }) {
41
- await makeDir(dirname(manifestPath))
42
- await pWriteFile(manifestPath, manifestString)
33
+ export const writeManifest = async function ({ manifestPath, manifestString }) {
34
+ await fs.mkdir(dirname(manifestPath), { recursive: true })
35
+ await fs.writeFile(manifestPath, manifestString)
43
36
  }
44
37
 
45
38
  // Remove the cache manifest from filesystem
46
- const removeManifest = async function (cachePath) {
39
+ export const removeManifest = async function (cachePath) {
47
40
  const manifestPath = getManifestPath(cachePath)
48
41
  await del(manifestPath, { force: true })
49
42
  }
@@ -53,14 +46,14 @@ const getManifestPath = function (cachePath) {
53
46
  return `${cachePath}${CACHE_EXTENSION}`
54
47
  }
55
48
 
56
- const isManifest = function (filePath) {
49
+ export const isManifest = function (filePath) {
57
50
  return filePath.endsWith(CACHE_EXTENSION)
58
51
  }
59
52
 
60
53
  const CACHE_EXTENSION = '.netlify.cache.json'
61
54
 
62
55
  // Check whether a file/directory is expired by checking its cache manifest
63
- const isExpired = async function (cachePath) {
56
+ export const isExpired = async function (cachePath) {
64
57
  const manifestPath = getManifestPath(cachePath)
65
58
  if (!(await pathExists(manifestPath))) {
66
59
  return false
@@ -72,9 +65,7 @@ const isExpired = async function (cachePath) {
72
65
 
73
66
  const readManifest = async function (cachePath) {
74
67
  const manifestPath = getManifestPath(cachePath)
75
- const manifestString = await pReadFile(manifestPath, 'utf8')
68
+ const manifestString = await fs.readFile(manifestPath)
76
69
  const manifest = JSON.parse(manifestString)
77
70
  return manifest
78
71
  }
79
-
80
- module.exports = { getManifestInfo, writeManifest, removeManifest, isManifest, isExpired }
package/src/path.js CHANGED
@@ -1,13 +1,11 @@
1
- 'use strict'
1
+ import { homedir } from 'os'
2
+ import { resolve, isAbsolute, join, sep } from 'path'
2
3
 
3
- const { homedir } = require('os')
4
- const { resolve, isAbsolute, join, sep } = require('path')
5
-
6
- const { getCacheDir } = require('./dir')
7
- const { safeGetCwd } = require('./utils/cwd')
4
+ import { getCacheDir } from './dir.js'
5
+ import { safeGetCwd } from './utils/cwd.js'
8
6
 
9
7
  // Find the paths of the file before/after caching
10
- const parsePath = async function ({ path, cacheDir, cwdOpt }) {
8
+ export const parsePath = async function ({ path, cacheDir, cwdOpt }) {
11
9
  const srcPath = await getSrcPath(path, cwdOpt)
12
10
  const cachePath = await getCachePath({ srcPath, cacheDir, cwdOpt })
13
11
  return { srcPath, cachePath }
@@ -94,7 +92,7 @@ const parseBase = function (name, base, srcPath) {
94
92
  return { name, relPath }
95
93
  }
96
94
 
97
- const getBases = async function (cwdOpt) {
95
+ export const getBases = async function (cwdOpt) {
98
96
  const cwdBase = await getCwdBase(cwdOpt)
99
97
  return [...cwdBase, { name: 'home', base: homedir() }, { name: 'root', base: sep }]
100
98
  }
@@ -107,5 +105,3 @@ const getCwdBase = async function (cwdOpt) {
107
105
 
108
106
  return [{ name: 'cwd', base: cwd }]
109
107
  }
110
-
111
- module.exports = { parsePath, getBases }
package/src/utils/cwd.js CHANGED
@@ -1,12 +1,10 @@
1
- 'use strict'
1
+ import { normalize } from 'path'
2
+ import process from 'process'
2
3
 
3
- const { normalize } = require('path')
4
- const process = require('process')
5
-
6
- const pathExists = require('path-exists')
4
+ import pathExists from 'path-exists'
7
5
 
8
6
  // Like `process.cwd()` but safer when current directory is wrong
9
- const safeGetCwd = async function (cwdOpt) {
7
+ export const safeGetCwd = async function (cwdOpt) {
10
8
  try {
11
9
  const cwd = getCwdValue(cwdOpt)
12
10
 
@@ -15,7 +13,7 @@ const safeGetCwd = async function (cwdOpt) {
15
13
  }
16
14
 
17
15
  return cwd
18
- } catch (error) {
16
+ } catch {
19
17
  return ''
20
18
  }
21
19
  }
@@ -23,5 +21,3 @@ const safeGetCwd = async function (cwdOpt) {
23
21
  const getCwdValue = function (cwdOpt = process.cwd()) {
24
22
  return normalize(cwdOpt)
25
23
  }
26
-
27
- module.exports = { safeGetCwd }
@@ -1,5 +0,0 @@
1
- 'use strict'
2
-
3
- // Patches `Array.flat()` and `Array.flatMap()`
4
- // TODO: remove after dropping Node <12 support
5
- require('array-flat-polyfill')