-
Notifications
You must be signed in to change notification settings - Fork 23
/
generateCachebusterInfo.js
60 lines (52 loc) · 2.08 KB
/
generateCachebusterInfo.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
const resourceFactory = require("@ui5/fs").resourceFactory;
const crypto = require("crypto");
async function signByTime(resource) {
return resource.getStatInfo().mtime.getTime();
}
async function signByHash(resource) {
const hasher = crypto.createHash("sha1");
const buffer = await resource.getBuffer();
hasher.update(buffer.toString("binary"));
return hasher.digest("hex");
}
function getSigner(type) {
type = type || "time";
switch (type) {
case "time":
return signByTime;
case "hash":
return signByHash;
default:
throw new Error(`Invalid signature type: '${type}'. Valid ones are: 'time' or 'hash'`);
}
}
/**
* Task to generate the application cachebuster info file.
*
* @public
* @alias module:@ui5/builder.tasks.generateCachebusterInfo
* @param {object} parameters Parameters
* @param {module:@ui5/fs.DuplexCollection} parameters.workspace DuplexCollection to read and write files
* @param {module:@ui5/fs.AbstractReader} parameters.dependencies Reader or Collection to read dependency files
* @param {object} parameters.options Options
* @param {string} parameters.options.namespace Namespace of the application
* @param {string} [parameters.options.signatureType='time'] Type of signature to be used ('time' or 'hash')
* @returns {Promise<undefined>} Promise resolving with <code>undefined</code> once data has been written
*/
module.exports = function({workspace, dependencies, options: {namespace, signatureType}}) {
return workspace.byGlob(`/resources/${namespace}/**/*`)
.then(async (resources) => {
const cachebusterInfo = {};
const regex = new RegExp(`^/resources/${namespace}/`);
const signer = getSigner(signatureType);
await Promise.all(resources.map(async (resource) => {
const normalizedPath = resource.getPath().replace(regex, "");
cachebusterInfo[normalizedPath] = await signer(resource);
}));
const cachebusterInfoResource = resourceFactory.createResource({
path: `/resources/${namespace}/sap-ui-cachebuster-info.json`,
string: JSON.stringify(cachebusterInfo, null, 2)
});
return workspace.write(cachebusterInfoResource);
});
};