@minecraft/creator-tools
Version:
Minecraft Creator Tools command line and libraries.
96 lines (95 loc) • 5.01 kB
JavaScript
;
// 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 });
exports.CommunitySchemaItemInfoGeneratorTest = void 0;
const ProjectInfoItem_1 = __importDefault(require("./ProjectInfoItem"));
const IInfoItemData_1 = require("./IInfoItemData");
const Database_1 = __importDefault(require("../minecraft/Database"));
const CreatorToolsHost_1 = __importDefault(require("../app/CreatorToolsHost"));
const axios_1 = __importDefault(require("axios"));
const Utilities_1 = __importDefault(require("../core/Utilities"));
const MinecraftDefinitions_1 = __importDefault(require("../minecraft/MinecraftDefinitions"));
const ProjectItemUtilities_1 = __importDefault(require("../app/ProjectItemUtilities"));
const json_schema_1 = require("json-schema");
const JsonSchemaErrorBase = 100;
const NotCurrentFormatVersionBase = 1100;
var CommunitySchemaItemInfoGeneratorTest;
(function (CommunitySchemaItemInfoGeneratorTest) {
CommunitySchemaItemInfoGeneratorTest[CommunitySchemaItemInfoGeneratorTest["couldNotParseJson"] = 1] = "couldNotParseJson";
})(CommunitySchemaItemInfoGeneratorTest || (exports.CommunitySchemaItemInfoGeneratorTest = CommunitySchemaItemInfoGeneratorTest = {}));
/**
* Validates JSON files against JSON schema definitions.
*
* @see {@link ../../public/data/forms/mctoolsval/comjson.form.json} for topic definitions
*/
class CommunitySchemaItemInfoGenerator {
id = "COMJSON";
title = "Community JSON Schema Validation";
canAlwaysProcess = true;
_schemaContentByPath = {};
constructor() {
this.loadSchema = this.loadSchema.bind(this);
}
summarize(info, infoSet) { }
async loadSchema(uri) {
const res = await axios_1.default.get(Utilities_1.default.ensureEndsWithSlash(CreatorToolsHost_1.default.contentWebRoot) + uri);
return res.data;
}
async generate(projectItem, contentIndex) {
const items = [];
if (projectItem.primaryFile &&
projectItem.primaryFile.content &&
typeof projectItem.primaryFile.content === "string") {
const schemaPath = projectItem.getCommunitySchemaPath();
if (schemaPath) {
let verIsCurrent = await MinecraftDefinitions_1.default.formatVersionIsCurrent(projectItem);
if (verIsCurrent) {
let schemaContents = this._schemaContentByPath[schemaPath];
if (!schemaContents) {
schemaContents = await Database_1.default.getCommunitySchema(schemaPath);
if (schemaContents) {
this._schemaContentByPath[schemaPath] = schemaContents;
}
}
if (schemaContents) {
let content = projectItem.primaryFile.content;
let contentObj = undefined;
content = Utilities_1.default.fixJsonContent(content);
try {
contentObj = JSON.parse(content);
const results = (0, json_schema_1.validate)(contentObj, schemaContents);
for (const err of results.errors) {
items.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.warning, this.id, JsonSchemaErrorBase + projectItem.itemType, `JSON structure error`, projectItem, `(${err.property}) ${err.message}`));
}
}
catch (e) {
let errorMess = e;
if (e.message) {
errorMess = e.message;
}
items.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.error, this.id, CommunitySchemaItemInfoGeneratorTest.couldNotParseJson, "Could not parse JSON - " + errorMess, projectItem));
}
if (contentObj) {
}
}
}
else {
let fvStr = "";
const fv = await MinecraftDefinitions_1.default.getFormatVersion(projectItem);
if (fv) {
fvStr = " (is at " + fv.join(".") + ")";
}
items.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.info, this.id, NotCurrentFormatVersionBase + projectItem.itemType, ProjectItemUtilities_1.default.getDescriptionForType(projectItem.itemType) +
" is not at a current format version" +
fvStr, projectItem));
}
}
}
return items;
}
}
exports.default = CommunitySchemaItemInfoGenerator;