Skip to content

Commit acaea85

Browse files
author
suraj.patel
committed
Removing unwanted modules and checking for valid json input.
1 parent 3d31a6d commit acaea85

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

index.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
const path = require("path");
22
const fs = require("fs");
33
const chalk = require("chalk");
4-
const { fg } = require("./colours");
5-
const exec = require("child_process").exec;
64
const { WrappedPlugin, clear } = require("./WrappedPlugin");
75
const {
86
getModuleName,
@@ -33,6 +31,7 @@ module.exports = class SpeedMeasurePlugin {
3331
this.apply = this.apply.bind(this);
3432
this.provideLoaderTiming = this.provideLoaderTiming.bind(this);
3533
this.getLoadersBuildComparison = this.getLoadersBuildComparison.bind(this);
34+
this.isValidJson = this.isValidJson.bind(this);
3635
}
3736

3837
wrap(config) {
@@ -71,15 +70,24 @@ module.exports = class SpeedMeasurePlugin {
7170
return config;
7271
}
7372

73+
isValidJson(strJson) {
74+
try {
75+
return JSON.parse(strJson) && !!strJson;
76+
} catch (e) {
77+
return false;
78+
}
79+
}
80+
7481
getLoadersBuildComparison() {
7582
let objBuildData = { loaderInfo: [] };
7683
let loaderFile = this.options.compareLoadersBuild.filePath || "";
7784
const outputObj = getLoadersOutput(this.timeEventData.loaders);
7885

7986
if (outputObj && loaderFile && fs.existsSync(loaderFile)) {
8087
let buildDetails = fs.readFileSync(loaderFile);
81-
buildDetails =
82-
buildDetails.toString() !== "" ? JSON.parse(buildDetails) : [];
88+
buildDetails = this.isValidJson(buildDetails.toString())
89+
? JSON.parse(buildDetails)
90+
: [];
8391
const buildCount = buildDetails.length;
8492
const buildNo =
8593
buildCount > 0 ? buildDetails[buildCount - 1]["buildNo"] + 1 : 1;

0 commit comments

Comments
 (0)