1 | var fs = require("fs");
|
2 | var path = require("path");
|
3 | var Promise = require('promise');
|
4 | var SlJsInfra = require("sl-js-infra").SlJsInfra;
|
5 | var ValidationUtils = SlJsInfra.Utils.ValidationUtils;
|
6 | var mkdirp = require('mkdirp');
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | var BLOB_ID = "sl-mapping";
|
14 | function SlMapper(outputPath, backendProxy, buildSessionId, logger) {
|
15 | ValidationUtils.verifyNotNullOrEmpty(backendProxy, "backendProxy");
|
16 | ValidationUtils.verifyNotNullOrEmpty(buildSessionId, "buildSessionId");
|
17 | ValidationUtils.verifyNotNullOrEmpty(logger, "logger");
|
18 | this.mapping = {};
|
19 | this.outputPath = outputPath;
|
20 | this.backendProxy = backendProxy;
|
21 | this.buildSessionId = buildSessionId;
|
22 | this.logger = logger;
|
23 |
|
24 | this.mapMethod = function(method) {
|
25 | var key = this.createUniqueIdKey(method, false);
|
26 | if(key) {
|
27 | this.mapping[key] = method.uniqueId;
|
28 | }
|
29 | }
|
30 | this.mapBranch = function(branch) {
|
31 | var key = this.createUniqueIdKey(branch, true);
|
32 | if(key) {
|
33 | this.mapping[key] = branch.uniqueId;
|
34 | }
|
35 | }
|
36 |
|
37 |
|
38 | this.getFilePath = function() {
|
39 | return path.resolve(this.outputPath);
|
40 | }
|
41 |
|
42 | this.writeToFile = function () {
|
43 | var filePath = this.getFilePath();
|
44 | var outputFolder = path.dirname(filePath);
|
45 | mkdirp.sync(outputFolder);
|
46 | var fileContent = this.createFileContent();
|
47 | try {
|
48 | fs.writeFileSync(filePath, fileContent);
|
49 | }catch (e) {
|
50 | this.logger.error("Failed to write sl-mapping file Error: ", e)
|
51 | }
|
52 | }
|
53 |
|
54 | this.postToServer = function () {
|
55 | var context = this;
|
56 | return new Promise(function (resolve , reject) {
|
57 | context.backendProxy.submitBlob(context.mapping, context.buildSessionId, BLOB_ID, function (err) {
|
58 | if (err) {
|
59 | context.logger.error("Error while submitting sl-mapping, '%s'", err);
|
60 | reject(err)
|
61 | } else {
|
62 |
|
63 | context.logger.info("sl-mapping submitted successfully");
|
64 | resolve();
|
65 | }
|
66 | });
|
67 | });
|
68 |
|
69 |
|
70 | }
|
71 |
|
72 | this.createUniqueIdKey = function(element, isBranch){
|
73 | var delimiter = isBranch ? "|" : "@";
|
74 |
|
75 | var position = isBranch ? element.generatedData.parentPosition : element.generatedData.position;
|
76 | var key = [element.generatedData.absoluteFilename, position.join(",")].join(delimiter);
|
77 | if(isBranch){
|
78 | key += "|" + element.index;
|
79 | }
|
80 | return key;
|
81 | }
|
82 |
|
83 |
|
84 | this.createFileContent = function () {
|
85 | var content ="var slMapping =" + JSON.stringify(this.mapping, null, "\t") + "\n" +
|
86 | " if(window && (!window.$Sealights || !window.$Sealights.slMapping)){\n" +
|
87 | " window.$Sealights = window.$Sealights || {};\n" +
|
88 | " window.$Sealights.slMapping = slMapping;\n" +
|
89 | " }";
|
90 | return content;
|
91 | }
|
92 |
|
93 | function validateParams() {
|
94 | for(var i = 0; i< arguments.length; i++){
|
95 | if(!arguments[i]){
|
96 | return false;
|
97 | }
|
98 | return true;
|
99 | }
|
100 | }
|
101 | }
|
102 | module.exports.SlMapper = SlMapper;
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|