-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.js
More file actions
50 lines (40 loc) · 1.08 KB
/
index.js
File metadata and controls
50 lines (40 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const crypto = require('crypto')
const fs = require('fs')
const getDigest = (string) => {
const hash = crypto.createHash('md5')
const data = hash.update(string, 'utf-8')
return data.digest('hex')
}
const getFileDigest = (path) => {
if (!fs.existsSync(path)) {
return null
}
if (fs.statSync(path).isDirectory()) {
return null
}
return getDigest(fs.readFileSync(path))
}
function filter(src, dest) {
if (!fs.existsSync(dest)) {
return true
}
if (fs.statSync(dest).isDirectory()) {
return true
}
return getFileDigest(src) !== getFileDigest(dest)
}
module.exports = (options = {}) => ({
name: 'copy-static-files',
setup(build) {
let src = options.src || './static'
let dest = options.dest || '../public'
build.onEnd(() => fs.cpSync(src, dest, {
dereference: options.dereference || true,
errorOnExist: options.errorOnExist || false,
filter: options.filter || filter,
force: options.force || true,
preserveTimestamps: options.preserveTimestamps || true,
recursive: options.recursive || true,
}))
},
})