@minecraft/creator-tools
Version:
Minecraft Creator Tools command line and libraries.
129 lines (128 loc) • 4.95 kB
JavaScript
"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 });
exports.PackType = void 0;
const ProjectItemUtilities_1 = __importDefault(require("../app/ProjectItemUtilities"));
const BehaviorManifestDefinition_1 = __importDefault(require("./BehaviorManifestDefinition"));
const ResourceManifestDefinition_1 = __importDefault(require("./ResourceManifestDefinition"));
const SkinManifestDefinition_1 = __importDefault(require("./SkinManifestDefinition"));
const PersonaManifestDefinition_1 = __importDefault(require("./PersonaManifestDefinition"));
const DesignManifestDefinition_1 = __importDefault(require("./DesignManifestDefinition"));
const Log_1 = __importDefault(require("../core/Log"));
var PackType;
(function (PackType) {
PackType[PackType["resource"] = 0] = "resource";
PackType[PackType["behavior"] = 1] = "behavior";
PackType[PackType["skin"] = 2] = "skin";
PackType[PackType["persona"] = 3] = "persona";
PackType[PackType["design"] = 4] = "design";
})(PackType || (exports.PackType = PackType = {}));
class Pack {
packType;
manifestFile;
folder;
project;
projectItem;
isInWorld = false;
manifest;
_items;
//stubbing in for use later
get isEDUOffer() {
return false;
}
constructor(folderIn, packTypeIn, project, projectItem) {
this.project = project;
this.projectItem = projectItem;
this.folder = folderIn;
this.packType = packTypeIn;
}
ensureManifestFile() {
if (this.manifestFile === undefined) {
this.manifestFile = this.folder.ensureFile("manifest.json");
}
return this.manifestFile;
}
async ensureManifest() {
if (this.manifest) {
return this.manifest;
}
this.manifestFile = this.ensureManifestFile();
if (this.packType === PackType.behavior) {
this.manifest = await BehaviorManifestDefinition_1.default.ensureOnFile(this.manifestFile);
}
else if (this.packType === PackType.skin) {
this.manifest = await SkinManifestDefinition_1.default.ensureOnFile(this.manifestFile);
}
else if (this.packType === PackType.persona) {
this.manifest = await PersonaManifestDefinition_1.default.ensureOnFile(this.manifestFile);
}
else if (this.packType === PackType.design) {
this.manifest = await DesignManifestDefinition_1.default.ensureOnFile(this.manifestFile);
}
else {
this.manifest = await ResourceManifestDefinition_1.default.ensureOnFile(this.manifestFile);
}
if (this.manifest && !this.manifest.isLoaded) {
this.manifest.load();
}
// If the manifest file was newly created or is empty, populate it with default content
if (this.manifest && !this.manifest.definition) {
this.manifest.ensureHeaderForProject(this.project);
this.manifest.persist();
}
return this.manifest;
}
getManifest() {
const manifest = this.getPackItems().find((item) => item.name === "manifest.json");
Log_1.default.assert(!!manifest, "Pack should always have a manifest item");
return manifest;
}
getPackItems() {
if (!!this._items) {
return this._items;
}
const folderPath = this.projectItem.projectPath;
if (!folderPath) {
throw new Error("Pack.getPackItems called without a project path");
}
this._items = this.project.items.filter((item) => item.projectPath?.startsWith(folderPath));
return this._items;
}
hasVibrantVisualsContent() {
return this.getPackItems().some((item) => ProjectItemUtilities_1.default.isVibrantVisualsRelated(item));
}
static ensureOnFolder(folder, packType, project, projectItem) {
if (folder.manager === undefined) {
const pack = new Pack(folder, packType, project, projectItem);
if (projectItem.isInWorld) {
pack.isInWorld = true;
}
pack.project = project;
pack.packType = packType;
return pack;
}
else {
return folder.manager;
}
}
get name() {
return this.projectItem?.name || "Unnamed pack";
}
async getFiles(predicate) {
const result = [];
for await (const file of this.folder.allFiles) {
if (!file.isContentLoaded) {
await file.loadContent();
}
if (file.content && (!predicate || predicate(file))) {
result.push(file);
}
}
return result;
}
}
exports.default = Pack;