/
gulpfile.js
69 lines (63 loc) · 1.75 KB
/
gulpfile.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 { src, dest, lastRun } = require("gulp");
const cloudinaryUpload = require("gulp-cloudinary-upload");
const changed = require("gulp-changed");
const path = require("path");
const paths = {
src: path.resolve(__dirname, "src"),
dest: path.resolve(__dirname, "src"),
manifest: path.resolve(__dirname, "src", "cloudinary.json"),
cloudinaryDest: "korona-ceska.cz"
};
function readManifest(path) {
return fs.promises
.readFile(path, "utf-8")
.then(x => JSON.parse(x))
.catch(() => null);
}
function getRelativePath(filePath) {
return path.relative(
path.resolve("./src"),
path.resolve(__dirname, path.dirname(filePath))
);
}
function getRelativeFilePath(filePath) {
return path.relative(
path.resolve("./src"),
path.resolve(__dirname, filePath)
);
}
const cloudinaryTask = () =>
src("./src/illustrations/*.png", {
since: lastRun(cloudinaryTask)
})
.pipe(
changed("./build", {
async hasChanged(stream, sourceFile) {
const manifest = await readManifest(paths.manifest);
const imagePath = getRelativeFilePath(sourceFile.path);
if (!(manifest && manifest[imagePath])) {
stream.push(sourceFile);
}
}
})
)
.pipe(
cloudinaryUpload({
folderResolver(filePath) {
const relativePath = getRelativePath(filePath);
return path.join(paths.cloudinaryDest, relativePath);
},
keyResolver(filePath) {
return path.relative(paths.src, path.resolve(__dirname, filePath));
}
})
)
.pipe(
cloudinaryUpload.manifest({
path: paths.manifest,
merge: true
})
)
.pipe(dest(paths.dest));
module.exports.default = cloudinaryTask;