UNPKG

@minecraft/creator-tools

Version:

Minecraft Creator Tools command line and libraries.

136 lines (135 loc) 4.53 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 ste_events_1 = require("ste-events"); const StorageUtilities_1 = __importDefault(require("../../storage/StorageUtilities")); const Database_1 = __importDefault(require("../Database")); class LegacyDocumentationDefinition { _data; _file; _isLoaded = false; _onLoaded = new ste_events_1.EventDispatcher(); get isLoaded() { return this._isLoaded; } get file() { return this._file; } get onLoaded() { return this._onLoaded.asEvent(); } set file(newFile) { this._file = newFile; } static async loadNode(catalog, nodePath, isPreview) { let metadataFolder = undefined; if (isPreview) { metadataFolder = await Database_1.default.loadPreviewMetadataFolder(); } else { metadataFolder = await Database_1.default.loadReleaseMetadataFolder(); } if (!metadataFolder) { return undefined; } const docFile = await metadataFolder.ensureFileFromRelativePath("/doc_modules/" + catalog + ".json"); if (!docFile) { return undefined; } if (!(await docFile.exists())) { return undefined; } const legacyDocDef = await LegacyDocumentationDefinition.ensureOnFile(docFile); if (!legacyDocDef) { return undefined; } return legacyDocDef.getNode(nodePath); } getNode(nodePath) { if (!this._data) { return; } const nodeEntries = nodePath.split("/"); let curNode = this._data; for (let nodeEntry of nodeEntries) { if (nodeEntry.length > 0) { nodeEntry = nodeEntry.toLowerCase(); if (!curNode.nodes || curNode.nodes.length === 0) { return undefined; } let foundNextNode = false; for (const node of curNode.nodes) { if (!foundNextNode && ((node.name && node.name.toLowerCase() === nodeEntry) || (node.examples_title && node.examples_title.toLowerCase() === nodeEntry))) { curNode = node; foundNextNode = true; } } if (!foundNextNode) { return undefined; } } } return curNode; } static async ensureOnFile(file, loadHandler) { let ldd; if (file.manager === undefined) { ldd = new LegacyDocumentationDefinition(); ldd.file = file; file.manager = ldd; } if (file.manager !== undefined && file.manager instanceof LegacyDocumentationDefinition) { ldd = file.manager; if (!ldd.isLoaded) { if (loadHandler) { ldd.onLoaded.subscribe(loadHandler); } await ldd.load(); } } return ldd; } persist() { if (this._file === undefined) { return false; } if (!this._data) { return false; } return this._file.setObjectContentIfSemanticallyDifferent(this._data); } async load() { if (this._isLoaded) { return; } if (this._file === undefined) { Log_1.default.unexpectedUndefined("LDDF"); return; } if (!this._file.isContentLoaded) { await this._file.loadContent(); } if (!this._file.content || this._file.content instanceof Uint8Array) { this._isLoaded = true; this._onLoaded.dispatch(this, this); return; } let data = {}; // Use getJsonObject (not getJsonObjectWithComments) since this is read-only metadata let result = StorageUtilities_1.default.getJsonObject(this._file); if (result) { data = result; } this._data = data; this._isLoaded = true; this._onLoaded.dispatch(this, this); } } exports.default = LegacyDocumentationDefinition;