@netlify/cache-utils 5.0.2 → 5.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.
Files changed (2) hide show
  1. package/lib/fs.js +32 -15
  2. package/package.json +2 -2
package/lib/fs.js CHANGED
@@ -4,44 +4,61 @@ import cpy from 'cpy';
4
4
  import { globby } from 'globby';
5
5
  import { isNotJunk } from 'junk';
6
6
  import { moveFile } from 'move-file';
7
- // Move or copy a cached file/directory from/to a local one
8
- export const moveCacheFile = async function (src, dest, move) {
7
+ /**
8
+ * Move or copy a cached file/directory from/to a local one
9
+ * @param src The src directory or file to cache
10
+ * @param dest The destination location
11
+ * @param move If the file should be moved, moving is faster but removes the source files locally
12
+ */
13
+ export const moveCacheFile = async function (src, dest, move = false) {
9
14
  // Moving is faster but removes the source files locally
10
15
  if (move) {
11
16
  return moveFile(src, dest, { overwrite: false });
12
17
  }
13
- const { srcGlob, cwd } = await getSrcGlob(src);
14
- return cpy(srcGlob, dirname(dest), { cwd, parents: true, overwrite: false });
18
+ const { srcGlob, ...options } = await getSrcGlob(src);
19
+ if (srcGlob) {
20
+ return cpy(srcGlob, dirname(dest), { ...options, parents: true, overwrite: false });
21
+ }
15
22
  };
16
- // Non-existing files and empty directories are always skipped
23
+ /**
24
+ * Non-existing files and empty directories are always skipped
25
+ */
17
26
  export const hasFiles = async function (src) {
18
- const { srcGlob, cwd, isDir } = await getSrcGlob(src);
19
- return srcGlob !== undefined && !(await isEmptyDir({ srcGlob, cwd, isDir }));
27
+ const { srcGlob, isDir, ...options } = await getSrcGlob(src);
28
+ return srcGlob !== undefined && !(await isEmptyDir(srcGlob, isDir, options));
20
29
  };
21
- // Replicates what `cpy` is doing under the hood.
22
- const isEmptyDir = async function ({ srcGlob, cwd, isDir }) {
30
+ /** Replicates what `cpy` is doing under the hood. */
31
+ const isEmptyDir = async function (globPattern, isDir, options) {
23
32
  if (!isDir) {
24
33
  return false;
25
34
  }
26
- const files = await globby(srcGlob, { cwd });
35
+ const files = await globby(globPattern, options);
27
36
  const filteredFiles = files.filter((file) => isNotJunk(basename(file)));
28
37
  return filteredFiles.length === 0;
29
38
  };
30
- // Get globbing pattern with files to move/copy
39
+ /**
40
+ * Get globbing pattern with files to move/copy
41
+ */
31
42
  const getSrcGlob = async function (src) {
32
43
  const srcStat = await getStat(src);
33
44
  if (srcStat === undefined) {
34
- return {};
45
+ return { srcGlob: undefined, isDir: false, cwd: '' };
35
46
  }
36
47
  const isDir = srcStat.isDirectory();
37
48
  const srcBasename = basename(src);
38
49
  const cwd = dirname(src);
50
+ const baseOptions = {
51
+ srcGlob: srcBasename,
52
+ isDir,
53
+ cwd,
54
+ dot: true, // collect .dot directories as well
55
+ };
39
56
  if (isDir) {
40
- return { srcGlob: `${srcBasename}/**`, cwd, isDir };
57
+ return { ...baseOptions, srcGlob: `${srcBasename}/**` };
41
58
  }
42
- return { srcGlob: srcBasename, cwd, isDir };
59
+ return baseOptions;
43
60
  };
44
- const getStat = async function (src) {
61
+ const getStat = async (src) => {
45
62
  try {
46
63
  return await fs.stat(src);
47
64
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netlify/cache-utils",
3
- "version": "5.0.2",
3
+ "version": "5.1.1",
4
4
  "description": "Utility for caching files in Netlify Build",
5
5
  "type": "module",
6
6
  "exports": "./lib/main.js",
@@ -69,5 +69,5 @@
69
69
  "engines": {
70
70
  "node": "^14.16.0 || >=16.0.0"
71
71
  },
72
- "gitHead": "be9da6e85afa0223bdcd6d228c929f4372515538"
72
+ "gitHead": "356667c6173362b294f9037efdb7b5ef1c726d48"
73
73
  }