@netlify/cache-utils 2.0.3 → 4.0.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/package.json CHANGED
@@ -1,8 +1,10 @@
1
1
  {
2
2
  "name": "@netlify/cache-utils",
3
- "version": "2.0.3",
3
+ "version": "4.0.0",
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,11 +45,10 @@
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
- "globby": "^10.0.2",
51
+ "globby": "^11.0.0",
51
52
  "junk": "^3.1.0",
52
53
  "locate-path": "^6.0.0",
53
54
  "make-dir": "^3.1.0",
@@ -60,6 +61,6 @@
60
61
  "tmp-promise": "^3.0.0"
61
62
  },
62
63
  "engines": {
63
- "node": ">=10.18.0"
64
+ "node": "^12.20.0 || ^14.14.0 || >=16.0.0"
64
65
  }
65
66
  }
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,16 @@
1
- 'use strict'
1
+ import { stat } from 'fs'
2
+ import { basename, dirname } from 'path'
3
+ import { promisify } from 'util'
2
4
 
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')
5
+ import cpy from 'cpy'
6
+ import globby from 'globby'
7
+ import junk from 'junk'
8
+ import moveFile from 'move-file'
11
9
 
12
10
  const pStat = promisify(stat)
13
11
 
14
12
  // Move or copy a cached file/directory from/to a local one
15
- const moveCacheFile = async function (src, dest, move) {
13
+ export const moveCacheFile = async function (src, dest, move) {
16
14
  // Moving is faster but removes the source files locally
17
15
  if (move) {
18
16
  return moveFile(src, dest, { overwrite: false })
@@ -23,7 +21,7 @@ const moveCacheFile = async function (src, dest, move) {
23
21
  }
24
22
 
25
23
  // Non-existing files and empty directories are always skipped
26
- const hasFiles = async function (src) {
24
+ export const hasFiles = async function (src) {
27
25
  const { srcGlob, cwd, isDir } = await getSrcGlob(src)
28
26
  return srcGlob !== undefined && !(await isEmptyDir({ srcGlob, cwd, isDir }))
29
27
  }
@@ -63,5 +61,3 @@ const getStat = async function (src) {
63
61
  return await pStat(src)
64
62
  } catch (error) {}
65
63
  }
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,20 @@
1
- 'use strict'
1
+ import { writeFile, readFile } from 'fs'
2
+ import { dirname } from 'path'
3
+ import { promisify } from 'util'
2
4
 
3
- const { writeFile, readFile } = require('fs')
4
- const { dirname } = require('path')
5
- const { promisify } = require('util')
5
+ import del from 'del'
6
+ import makeDir from 'make-dir'
7
+ import pathExists from 'path-exists'
6
8
 
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')
9
+ import { getExpires, checkExpires } from './expire.js'
10
+ import { getHash } from './hash.js'
13
11
 
14
12
  const pWriteFile = promisify(writeFile)
15
13
  const pReadFile = promisify(readFile)
16
14
 
17
15
  // Retrieve cache manifest of a file to cache, which contains the file/directory
18
16
  // contents hash and the `expires` date.
19
- const getManifestInfo = async function ({ cachePath, move, ttl, digests }) {
17
+ export const getManifestInfo = async function ({ cachePath, move, ttl, digests }) {
20
18
  const manifestPath = getManifestPath(cachePath)
21
19
  const expires = getExpires(ttl)
22
20
  const hash = await getHash(digests, move)
@@ -37,13 +35,13 @@ const isIdentical = async function ({ hash, manifestPath, manifestString }) {
37
35
  }
38
36
 
39
37
  // Persist the cache manifest to filesystem
40
- const writeManifest = async function ({ manifestPath, manifestString }) {
38
+ export const writeManifest = async function ({ manifestPath, manifestString }) {
41
39
  await makeDir(dirname(manifestPath))
42
40
  await pWriteFile(manifestPath, manifestString)
43
41
  }
44
42
 
45
43
  // Remove the cache manifest from filesystem
46
- const removeManifest = async function (cachePath) {
44
+ export const removeManifest = async function (cachePath) {
47
45
  const manifestPath = getManifestPath(cachePath)
48
46
  await del(manifestPath, { force: true })
49
47
  }
@@ -53,14 +51,14 @@ const getManifestPath = function (cachePath) {
53
51
  return `${cachePath}${CACHE_EXTENSION}`
54
52
  }
55
53
 
56
- const isManifest = function (filePath) {
54
+ export const isManifest = function (filePath) {
57
55
  return filePath.endsWith(CACHE_EXTENSION)
58
56
  }
59
57
 
60
58
  const CACHE_EXTENSION = '.netlify.cache.json'
61
59
 
62
60
  // Check whether a file/directory is expired by checking its cache manifest
63
- const isExpired = async function (cachePath) {
61
+ export const isExpired = async function (cachePath) {
64
62
  const manifestPath = getManifestPath(cachePath)
65
63
  if (!(await pathExists(manifestPath))) {
66
64
  return false
@@ -76,5 +74,3 @@ const readManifest = async function (cachePath) {
76
74
  const manifest = JSON.parse(manifestString)
77
75
  return manifest
78
76
  }
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
 
@@ -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')