|
1 | 1 | const path = require("path"); |
2 | 2 | const fs = require("fs"); |
3 | 3 | const chalk = require("chalk"); |
| 4 | +const { fg } = require("./colours"); |
| 5 | +const exec = require("child_process").exec; |
4 | 6 | const { WrappedPlugin, clear } = require("./WrappedPlugin"); |
5 | 7 | const { |
6 | 8 | getModuleName, |
@@ -30,6 +32,8 @@ module.exports = class SpeedMeasurePlugin { |
30 | 32 | this.addTimeEvent = this.addTimeEvent.bind(this); |
31 | 33 | this.apply = this.apply.bind(this); |
32 | 34 | this.provideLoaderTiming = this.provideLoaderTiming.bind(this); |
| 35 | + this.getLoadersBuildComparison = this.getLoadersBuildComparison.bind(this); |
| 36 | + this.commitBuidlComparison = this.commitBuidlComparison.bind(this); |
33 | 37 | } |
34 | 38 |
|
35 | 39 | wrap(config) { |
@@ -68,6 +72,104 @@ module.exports = class SpeedMeasurePlugin { |
68 | 72 | return config; |
69 | 73 | } |
70 | 74 |
|
| 75 | + commitBuidlComparison() { |
| 76 | + let loaderFile = this.options.compareLoadersBuild.filePath || ""; |
| 77 | + let gitCmd = `git add ${loaderFile} && git commit -m 'Updating file with new build info' && git push origin`; |
| 78 | + exec(gitCmd, function(err, stdout, stderr) { |
| 79 | + if (err) { |
| 80 | + console.log(fg("There was error while committing your changes."), err); |
| 81 | + return; |
| 82 | + } |
| 83 | + console.log("--------------------------------------------"); |
| 84 | + console.log(fg("Succefully Committed Build Info."), stdout); |
| 85 | + console.log("--------------------------------------------"); |
| 86 | + }); |
| 87 | + } |
| 88 | + getLoadersBuildComparison() { |
| 89 | + let objBuildData = { loaderInfo: [] }; |
| 90 | + let loaderFile = this.options.compareLoadersBuild.filePath || ""; |
| 91 | + const outputObj = getLoadersOutput(this.timeEventData.loaders); |
| 92 | + |
| 93 | + if (outputObj && loaderFile && fs.existsSync(loaderFile)) { |
| 94 | + let buildDetails = fs.readFileSync(loaderFile); |
| 95 | + buildDetails = |
| 96 | + buildDetails.toString() !== "" ? JSON.parse(buildDetails) : []; |
| 97 | + const buildCount = buildDetails.length; |
| 98 | + const buildNo = |
| 99 | + buildCount > 0 ? buildDetails[buildCount - 1]["buildNo"] + 1 : 1; |
| 100 | + |
| 101 | + /*********************** Code to create object format of current loader and write in the file. ************************/ |
| 102 | + outputObj.build.forEach((loaderObj, loaderIndex) => { |
| 103 | + const loaderInfo = {}; |
| 104 | + loaderInfo["Name"] = loaderObj.loaders.join(",") || ""; |
| 105 | + loaderInfo["Time"] = loaderObj.activeTime || ""; |
| 106 | + loaderInfo["Count"] = |
| 107 | + this.options.outputFormat === "humanVerbose" |
| 108 | + ? loaderObj.averages.dataPoints |
| 109 | + : ""; |
| 110 | + loaderInfo[`Comparison`] = ""; |
| 111 | + |
| 112 | + // Getting the comparison from the previous build by default only in case if build data is more then one. |
| 113 | + if (buildCount > 0) { |
| 114 | + const prevBuildIndex = buildCount - 1; |
| 115 | + for ( |
| 116 | + var y = 0; |
| 117 | + y < buildDetails[prevBuildIndex]["loaderInfo"].length; |
| 118 | + y++ |
| 119 | + ) { |
| 120 | + const prevloaderDetails = |
| 121 | + buildDetails[prevBuildIndex]["loaderInfo"][y]; |
| 122 | + if ( |
| 123 | + loaderInfo["Name"] == prevloaderDetails["Name"] && |
| 124 | + prevloaderDetails["Time"] |
| 125 | + ) { |
| 126 | + let previousBuildTime = |
| 127 | + buildDetails[prevBuildIndex]["loaderInfo"][y]["Time"]; |
| 128 | + loaderInfo[`Comparison`] = `buildDiff--> ${Math.abs( |
| 129 | + previousBuildTime - loaderObj.activeTime |
| 130 | + )}|${previousBuildTime > loaderObj.activeTime ? "Good" : "Bad"}`; |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + objBuildData["loaderInfo"].push(loaderInfo); |
| 136 | + }); |
| 137 | + |
| 138 | + buildDetails.push({ buildNo, loaderInfo: objBuildData["loaderInfo"] }); |
| 139 | + fs.writeFileSync(loaderFile, JSON.stringify(buildDetails)); |
| 140 | + /****************************************************************************************/ |
| 141 | + |
| 142 | + let outputTable = []; |
| 143 | + let objCurrentBuild = {}; |
| 144 | + |
| 145 | + for (let i = 0; i < buildDetails.length; i++) { |
| 146 | + outputTable = []; |
| 147 | + console.log("--------------------------------------------"); |
| 148 | + console.log("Build No ", buildDetails[i]["buildNo"]); |
| 149 | + console.log("--------------------------------------------"); |
| 150 | + |
| 151 | + if (buildDetails[i]["loaderInfo"]) { |
| 152 | + buildDetails[i]["loaderInfo"].forEach( |
| 153 | + (buildIndex, buildInfoIndex) => { |
| 154 | + const buildInfo = buildDetails[i]["loaderInfo"][buildInfoIndex]; |
| 155 | + objCurrentBuild = {}; |
| 156 | + objCurrentBuild["Name"] = buildInfo["Name"] || ""; |
| 157 | + objCurrentBuild["Time"] = buildInfo["Time"] || ""; |
| 158 | + if (this.options.outputFormat === "humanVerbose") |
| 159 | + objCurrentBuild["Count"] = buildInfo["Count"] || 0; |
| 160 | + objCurrentBuild["Comparison"] = buildInfo["Comparison"] || ""; |
| 161 | + outputTable.push(objCurrentBuild); |
| 162 | + } |
| 163 | + ); |
| 164 | + } |
| 165 | + console.table(outputTable); |
| 166 | + } |
| 167 | + |
| 168 | + // Commiting the build info file to git remote repo. |
| 169 | + this.options.compareLoadersBuild.commitBuildInfo && |
| 170 | + this.commitBuidlComparison(); |
| 171 | + } |
| 172 | + } |
71 | 173 | getOutput() { |
72 | 174 | const outputObj = {}; |
73 | 175 | if (this.timeEventData.misc) |
@@ -150,7 +252,8 @@ module.exports = class SpeedMeasurePlugin { |
150 | 252 | const outputFunc = this.options.outputTarget || console.log; |
151 | 253 | outputFunc(output); |
152 | 254 | } |
153 | | - |
| 255 | + // Build Comparison functionality. |
| 256 | + if (this.options.compareLoadersBuild) this.getLoadersBuildComparison(); |
154 | 257 | this.timeEventData = {}; |
155 | 258 | }); |
156 | 259 |
|
|
0 commit comments