-
Notifications
You must be signed in to change notification settings - Fork 270
/
Copy pathcopy-files.js
69 lines (58 loc) · 2.19 KB
/
copy-files.js
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const fs = require('fs');
const path = require('path');
function metalsmith(options) {
/*
copyFiles({
sourceTopDir: '..',
destTopDir: '../src',
copy: [
{
sourceDir: 'node_modules/particle-api-js/dist',
destDir: 'assets/js',
files: ['particle.min.js', 'particle.min.js.map'],
}
],
})
*/
const run = async function(files, metalsmith, done) {
for(const copyObj of options.copy) {
const sourcePath = metalsmith.path(path.join(options.sourceTopDir, copyObj.sourceDir));
const destPath = metalsmith.path(path.join(options.destTopDir, copyObj.destDir));
for(const filename of copyObj.files) {
const sourceFilePath = path.join(sourcePath, filename);
const destFilePath = path.join(destPath, filename);
const assetPath = path.join(copyObj.destDir, filename);
if (fs.existsSync(sourceFilePath)) {
const fileData = fs.readFileSync(sourceFilePath);
let copyFile = false;
if (fs.existsSync(destFilePath)) {
const destFileData = fs.readFileSync(destFilePath);
copyFile = !fileData.equals(destFileData);
}
else {
copyFile = true;
}
if (copyFile) {
fs.writeFileSync(destFilePath, fileData);
console.log('copy-files copying', {sourceFilePath, destFilePath, assetPath});
files[assetPath] = {
contents: fileData,
mode: '0644',
stats: fs.statSync(destFilePath)
};
}
}
else {
console.log('copy-files: source path does not exist ' + sourceFilePath);
}
}
}
done();
}
return function (files, metalsmith, done) {
run(files, metalsmith, done);
};
}
module.exports = {
metalsmith,
};