@@ -2,67 +2,80 @@ import { promises as fs } from 'fs'
2
2
import { basename , dirname } from 'path'
3
3
4
4
import cpy from 'cpy'
5
- import { globby } from 'globby'
5
+ import { Options , globby } from 'globby'
6
6
import { isNotJunk } from 'junk'
7
7
import { moveFile } from 'move-file'
8
8
9
9
/**
10
10
* Move or copy a cached file/directory from/to a local one
11
+ * @param src The src directory or file to cache
12
+ * @param dest The destination location
13
+ * @param move If the file should be moved, moving is faster but removes the source files locally
11
14
*/
12
- export const moveCacheFile = async function ( src : string , dest : string , move : boolean ) {
15
+ export const moveCacheFile = async function ( src : string , dest : string , move = false ) {
13
16
// Moving is faster but removes the source files locally
14
17
if ( move ) {
15
18
return moveFile ( src , dest , { overwrite : false } )
16
19
}
17
20
18
- const glob = await getSrcGlob ( src )
19
- if ( glob ) {
20
- return cpy ( glob . srcGlob , dirname ( dest ) , { cwd : glob . cwd , parents : true , overwrite : false } )
21
+ const { srcGlob , ... options } = await getSrcGlob ( src )
22
+ if ( srcGlob ) {
23
+ return cpy ( srcGlob , dirname ( dest ) , { ... options , parents : true , overwrite : false } )
21
24
}
22
25
}
23
26
24
27
/**
25
28
* Non-existing files and empty directories are always skipped
26
29
*/
27
30
export const hasFiles = async function ( src : string ) : Promise < boolean > {
28
- const glob = await getSrcGlob ( src )
29
- if ( ! glob ) {
30
- return false
31
- }
32
-
33
- return glob . srcGlob !== undefined && ! ( await isEmptyDir ( { srcGlob : glob . srcGlob , cwd : glob . cwd , isDir : glob . isDir } ) )
31
+ const { srcGlob, isDir, ...options } = await getSrcGlob ( src )
32
+ return srcGlob !== undefined && ! ( await isEmptyDir ( srcGlob , isDir , options ) )
34
33
}
35
34
36
35
/** Replicates what `cpy` is doing under the hood. */
37
- const isEmptyDir = async function ( { srcGlob , cwd , isDir } ) {
36
+ const isEmptyDir = async function ( globPattern : string , isDir : boolean , options : Options ) {
38
37
if ( ! isDir ) {
39
38
return false
40
39
}
41
40
42
- const files = await globby ( srcGlob , { cwd } )
41
+ const files = await globby ( globPattern , options )
43
42
const filteredFiles = files . filter ( ( file ) => isNotJunk ( basename ( file ) ) )
44
43
return filteredFiles . length === 0
45
44
}
46
45
46
+ type GlobOptions = {
47
+ srcGlob ?: string
48
+ isDir : boolean
49
+ cwd : string
50
+ dot ?: boolean
51
+ }
52
+
47
53
/**
48
54
* Get globbing pattern with files to move/copy
49
55
*/
50
- const getSrcGlob = async function ( src : string ) : Promise < null | { srcGlob : string ; cwd : string ; isDir : boolean } > {
56
+ const getSrcGlob = async function ( src : string ) : Promise < GlobOptions > {
51
57
const srcStat = await getStat ( src )
52
58
53
59
if ( srcStat === undefined ) {
54
- return null
60
+ return { srcGlob : undefined , isDir : false , cwd : '' }
55
61
}
56
62
57
63
const isDir = srcStat . isDirectory ( )
58
64
const srcBasename = basename ( src )
59
65
const cwd = dirname ( src )
60
66
67
+ const baseOptions : GlobOptions = {
68
+ srcGlob : srcBasename ,
69
+ isDir,
70
+ cwd,
71
+ dot : true , // collect .dot directories as well
72
+ }
73
+
61
74
if ( isDir ) {
62
- return { srcGlob : `${ srcBasename } /**` , cwd , isDir }
75
+ return { ... baseOptions , srcGlob : `${ srcBasename } /**` }
63
76
}
64
77
65
- return { srcGlob : srcBasename , cwd , isDir }
78
+ return baseOptions
66
79
}
67
80
68
81
const getStat = async ( src : string ) => {
0 commit comments