@ui5/task-adaptation
Version:
Custom task for ui5-builder which allows building UI5 Flexibility Adaptation Projects for SAP BTP, Cloud Foundry environment
75 lines • 2.94 kB
JavaScript
import * as fs from "fs";
import * as resourceFactory from "@ui5/fs/resourceFactory";
import { posix as path } from "path";
const UTF8 = "utf8";
export default class ResourceUtil {
static getRootFolder(projectNamespace) {
const newPath = ["/resources"];
if (projectNamespace) {
newPath.push(projectNamespace);
}
return path.join(...newPath);
}
static relativeToRoot(resourcePath, projectNamespace) {
const rootFolder = ResourceUtil.getRootFolder(projectNamespace);
return path.relative(rootFolder, resourcePath);
}
static getResourcePath(projectNamespace, ...paths) {
return path.join(this.getRootFolder(projectNamespace), ...paths);
}
static write(dir, files) {
const fsTarget = resourceFactory.createAdapter({
fsBasePath: dir,
virBasePath: "/"
});
const promises = [];
files.forEach((string, filename) => {
const resource = resourceFactory.createResource({ path: "/" + filename, string });
promises.push(fsTarget.write(resource));
});
return Promise.all(promises);
}
static _read(rootFolder, folder, files, exclude = []) {
const entries = fs.readdirSync(folder);
for (let entry of entries) {
const entryPath = path.join(folder, entry);
const stats = fs.lstatSync(entryPath);
if (stats.isFile() && !exclude.some(filepath => entryPath.endsWith(filepath))) {
const normalized = entryPath.substring(rootFolder.length + 1);
files.set(normalized, fs.readFileSync(entryPath, { encoding: "utf-8" }));
}
else if (stats.isDirectory()) {
this._read(rootFolder, entryPath, files, exclude);
}
}
}
static read(folder) {
const files = new Map();
this._read(folder, folder, files);
return files;
}
static getString(resource) {
return resource.getBuffer().then((buffer) => buffer.toString(UTF8));
}
static getJson(resource) {
return resource.getBuffer().then((buffer) => JSON.parse(buffer.toString(UTF8)));
}
static setString(resource, str) {
resource.setBuffer(Buffer.from(str, UTF8));
}
static createResource(filename, projectNamespace, content) {
return resourceFactory.createResource({
path: this.getResourcePath(projectNamespace, filename),
string: content
});
}
static async toFileMap(resources, projectNamespace) {
const files = new Map();
const rootFolderLength = ResourceUtil.getRootFolder(projectNamespace).length;
for (const resource of resources) {
files.set(resource.getPath().substring(rootFolderLength + 1), await ResourceUtil.getString(resource));
}
return files;
}
}
//# sourceMappingURL=resourceUtil.js.map