UNPKG

@minecraft/creator-tools

Version:

Minecraft Creator Tools command line and libraries.

320 lines (319 loc) 11.1 kB
"use strict"; // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const Log_1 = __importDefault(require("../core/Log")); const Utilities_1 = __importDefault(require("../core/Utilities")); const MinecraftUtilities_1 = __importDefault(require("../minecraft/MinecraftUtilities")); const IInfoItemData_1 = require("./IInfoItemData"); class ProjectInfoItem { #data; #projectItem; get dataObject() { return this.#data; } get itemType() { return this.#data.iTp; } get message() { return this.#data.m; } set message(newMessage) { this.#data.m = newMessage; } get generatorId() { return this.#data.gId; } get generatorIndex() { return this.#data.gIx; } get projectItem() { return this.#projectItem; } get data() { return this.#data.d; } set data(data) { this.#data.d = data; } get content() { return this.#data.c; } get featureSets() { return this.#data.fs; } set featureSets(value) { this.#data.fs = value; } get contentSummary() { let errorContent = this.#data.c; if (errorContent) { errorContent = errorContent.replace(/\n/gi, " "); errorContent = errorContent.replace(/\r/gi, " "); // Strip JSON object/array values, keep only the path portion (e.g., 'In "dependencies[0]"') errorContent = errorContent.replace(/:\s*[\{\[].*$/g, ""); if (errorContent.length > 80) { errorContent = errorContent.substring(0, 77) + "..."; } } return errorContent; } disconnect() { this.#projectItem = undefined; } get typeSummary() { switch (this.#data.iTp) { case IInfoItemData_1.InfoItemType.info: return "Info"; case IInfoItemData_1.InfoItemType.warning: return "Warning"; case IInfoItemData_1.InfoItemType.recommendation: return "Recommendation"; case IInfoItemData_1.InfoItemType.featureAggregate: return "Feature aggregation"; case IInfoItemData_1.InfoItemType.testCompleteFail: return "Test fail"; case IInfoItemData_1.InfoItemType.testCompleteSuccess: return "Test success"; case IInfoItemData_1.InfoItemType.error: return "Error"; default: return "Unknown"; } } get projectItemPath() { if (this.projectItem) { return this.projectItem.projectPath; } else if (this.#data.p) { return this.#data.p; } return undefined; } get shortProjectItemPath() { if (this.projectItem && this.projectItem.projectPath) { return MinecraftUtilities_1.default.getAfterPackPath(this.projectItem.projectPath); } else if (this.#data.p) { return MinecraftUtilities_1.default.getAfterPackPath(this.#data.p); } return undefined; } get typeSummaryShort() { let short = this.typeSummary.toUpperCase(); short = short.replace(/\s/g, ""); return short; } toString() { let summaryString = this.typeSummaryShort + ": "; summaryString += "[" + this.generatorId + Utilities_1.default.frontPadToLength(this.generatorIndex, 3, "0") + "] "; if (this.projectItem) { summaryString += "(" + this.projectItem.projectPath + ") "; } else if (this.#data.p) { summaryString += "(" + this.#data.p + ") "; } if (this.message) { summaryString += this.message; } if (this.data) { summaryString += ": " + this.data; } const errorContent = this.contentSummary; if (errorContent) { summaryString += " [in " + errorContent + "]"; } return summaryString; } minFeature(setName, measureName, newValue) { if (this.#data.fs === undefined) { this.#data.fs = {}; } setName = Utilities_1.default.convertToJsonKey(setName); measureName = Utilities_1.default.convertToJsonKey(measureName); if (!Utilities_1.default.isUsableAsObjectKey(setName)) { Log_1.default.unsupportedToken(setName); throw new Error(); } let setVal = this.#data.fs[setName]; if (setVal === undefined) { setVal = {}; this.#data.fs[setName] = setVal; } let curVal = setVal[measureName]; if (curVal === undefined) { curVal = newValue; } else { curVal = Math.min(curVal, newValue); } setVal[measureName] = curVal; } maxFeature(setName, measureName, newValue) { if (this.#data.fs === undefined) { this.#data.fs = {}; } setName = Utilities_1.default.convertToJsonKey(setName); measureName = Utilities_1.default.convertToJsonKey(measureName); if (!Utilities_1.default.isUsableAsObjectKey(setName)) { Log_1.default.unsupportedToken(setName); throw new Error(); } let setVal = this.#data.fs[setName]; if (setVal === undefined) { setVal = {}; this.#data.fs[setName] = setVal; } let curVal = setVal[measureName]; if (curVal === undefined) { curVal = newValue; } else { curVal = Math.max(curVal, newValue); } setVal[measureName] = curVal; } getFeatureContaining(token) { token = token.toLowerCase(); for (const setName in this.#data.fs) { const featureSet = this.#data.fs[setName]; if (featureSet) { for (const measureName in featureSet) { if (measureName.toLowerCase().indexOf(token) >= 0) { return featureSet[measureName]; } } } } return undefined; } getFeatureMeasureNumber(setName, measure) { measure = measure.toLowerCase(); if (!this.#data.fs) { return 0; } setName = Utilities_1.default.convertToJsonKey(setName); const featureSet = this.#data.fs[setName]; if (featureSet) { for (const measureName in featureSet) { if (measureName.toLowerCase() === measure) { return featureSet[measureName]; } } } return 0; } getNonZeroFeatureMeasures() { const results = []; if (!this.#data.fs) { return results; } for (const setName in this.#data.fs) { const featureSet = this.#data.fs[setName]; if (featureSet) { for (const measureName in featureSet) { const val = featureSet[measureName]; if (val !== undefined && typeof val === "number" && val > 0 && (measureName === "count" || measureName === "instanceCount")) { results.push(setName); } } } } return results; } spectrumFeature(setName, newValue) { if (this.#data.fs === undefined) { this.#data.fs = {}; } setName = Utilities_1.default.convertToJsonKey(setName); if (!Utilities_1.default.isUsableAsObjectKey(setName)) { Log_1.default.unsupportedToken(setName); throw new Error(); } this.incrementFeature(setName, "instanceCount", 1); this.incrementFeature(setName, "total", newValue); this.maxFeature(setName, "max", newValue); this.minFeature(setName, "min", newValue); let setVal = this.#data.fs[setName]; if (setVal === undefined) { setVal = {}; this.#data.fs[setName] = setVal; } const curTotal = setVal["total"]; const curCount = setVal["instanceCount"]; if (curCount && curTotal !== undefined) { setVal["average"] = curTotal / curCount; } } spectrumIntFeature(setName, newValue) { if (this.#data.fs === undefined) { this.#data.fs = {}; } setName = Utilities_1.default.convertToJsonKey(setName); let setVal = this.#data.fs[setName]; if (setVal === undefined) { setVal = {}; this.#data.fs[setName] = setVal; } this.incrementFeature(setName, "instanceCount", 1); this.incrementFeature(setName, "total", newValue); this.maxFeature(setName, "max", newValue); this.minFeature(setName, "min", newValue); const curTotal = setVal["total"]; const curCount = setVal["instanceCount"]; if (curCount && curTotal !== undefined) { setVal["average"] = Math.round(curTotal / curCount); } } incrementFeature(setName, measureName = "count", incrementalValue = 1) { if (this.#data.fs === undefined) { this.#data.fs = {}; } setName = Utilities_1.default.convertToJsonKey(setName); measureName = Utilities_1.default.convertToJsonKey(measureName); if (!Utilities_1.default.isUsableAsObjectKey(setName)) { return; } let setVal = this.#data.fs[setName]; if (setVal === undefined) { setVal = this.#data.fs[setName] = {}; } setVal[measureName] = (setVal[measureName] ?? 0) + incrementalValue; } setFeature(setName, measureName, value) { if (this.#data.fs === undefined) { this.#data.fs = {}; } setName = Utilities_1.default.convertToJsonKey(setName); measureName = Utilities_1.default.convertToJsonKey(measureName); if (!Utilities_1.default.isUsableAsObjectKey(setName)) { return; } let setVal = this.#data.fs[setName]; if (setVal === undefined) { setVal = this.#data.fs[setName] = {}; } setVal[measureName] = value; } constructor(itemType, generatorId, generatorIndex, message, projectItem, data, itemId, content, projectItemPath) { this.#data = { iTp: itemType, gId: generatorId, gIx: generatorIndex, m: message, p: projectItem ? projectItem.projectPath : projectItemPath, d: data, iId: itemId, c: content, fs: undefined, }; this.#projectItem = projectItem; } } exports.default = ProjectInfoItem;