forked from SAP/ui5-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrapHtmlTransformer.js
31 lines (29 loc) · 1.29 KB
/
bootstrapHtmlTransformer.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
const log = require("@ui5/logger").getLogger("builder:processors:bootstrapHtmlTransformer");
const cheerio = require("cheerio");
/**
* Transforms the UI5 bootstrap of a HTML resource files.
*
* @module builder/processors/bootstrapHtmlTransformer
* @param {Object} parameters Parameters
* @param {module:@ui5/fs.Resource[]} parameters.resources List of resources to be processed
* @param {Object} [parameters.options] Options
* @param {string} [parameters.options.src] Bootstrap "src" that should be used
* @returns {Promise<module:@ui5/fs.Resource[]>} Promise resolving with the cloned resources
*/
module.exports = function({resources, options}) {
async function processResource(resource) {
const content = await resource.getString();
const $ = cheerio.load(content);
const bootstrapScript = $("script#sap-ui-bootstrap");
if (bootstrapScript.length === 1) {
bootstrapScript.attr("src", options.src);
resource.setString($.html());
} else if (bootstrapScript.length > 1) {
log.warn("Skipping bootstrap transformation. Found multiple bootstrap script tags with id=sap-ui-bootstrap.");
} else {
log.warn("Skipping bootstrap transformation. Could not find bootstrap script tag with id=sap-ui-bootstrap.");
}
return resource;
}
return Promise.all(resources.map(processResource));
};