@tririga/tri-pull
Version:
A tool for pulling the UX view source files from the TRIRIGA server.
115 lines (103 loc) • 2.87 kB
JavaScript
;
const fs = require("fs");
const log = require("loglevel");
const path = require("path");
const TriWebComponent = require("../server/tri-web-component.js");
class TriPullService {
constructor(url, maxConcurrentOperations = 1) {
this.webComponentAPI = new TriWebComponent(url);
this.maxConcurrentOperations = maxConcurrentOperations;
}
async pullView(viewName, outputDir, polymerVersion, webapp) {
try {
const viewFileInfoList = await this.webComponentAPI.getViewData(
viewName,
polymerVersion,
webapp
);
const pullOperations = viewFileInfoList.map(
(viewFileInfo) =>
new PullOperation(
this.webComponentAPI,
outputDir,
viewName,
viewFileInfo,
polymerVersion,
webapp
)
);
return this.processPullOperations(pullOperations);
} catch (error) {
throw new Error(
`Pull failed for view: ${viewName}, Error: ${error.message}`
);
}
}
processPullOperations(pullOperations) {
const { maxConcurrentOperations } = this;
let index = 0;
const pullResults = [];
async function next(result) {
if (result != null) {
pullResults.push(result);
}
const batch = [];
for (
let j = 0;
j < maxConcurrentOperations && index < pullOperations.length;
index++, j++
) {
batch.push(pullOperations[index].run());
}
if (batch.length > 0) {
await Promise.all(batch);
return next();
} else {
return pullResults;
}
}
return next();
}
}
class PullOperation {
constructor(
webComponentAPI,
outputDir,
viewName,
viewFileInfo,
polymerVersion,
webapp
) {
this.webComponentAPI = webComponentAPI;
this.outputDir = outputDir;
this.viewName = viewName;
this.viewFileInfo = viewFileInfo;
this.polymerVersion = polymerVersion;
this.webapp = webapp;
}
async run() {
const componentSource = await this.webComponentAPI.getViewFile(
this.viewFileInfo.path,
this.viewName,
this.polymerVersion,
this.webapp
);
const filepath = this.viewFileInfo.path;
// Make sure all directories are created.
const viewDir = path.normalize(this.outputDir);
const dirs = filepath.split("/");
dirs[0] = viewDir + dirs[0];
for (let i = 1; i < dirs.length; i++) {
dirs[i] = dirs[i - 1] + path.sep + dirs[i];
}
for (let i = 0; i < dirs.length - 1; i++) {
if (!fs.existsSync(dirs[i])) {
fs.mkdirSync(dirs[i]);
}
}
// Write the component
fs.writeFileSync(viewDir + path.sep + filepath, componentSource);
log.info(" File pulled: " + this.viewFileInfo.path.magenta);
}
}
module.exports = TriPullService;