@minecraft/creator-tools
Version:
Minecraft Creator Tools command line and libraries.
249 lines (248 loc) • 7.66 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 });
const StorageBase_1 = __importDefault(require("./StorageBase"));
const FolderBase_1 = __importDefault(require("./FolderBase"));
/**
* A virtual folder that delegates to real folders based on a mapping.
* This creates a unified view of multiple storage locations.
*/
class VirtualFolderStorage extends StorageBase_1.default {
rootFolder;
_mappings = [];
constructor(mappings) {
super();
this.rootFolder = new VirtualRootFolder(this);
this.readOnly = true; // Virtual storage is read-only by default
if (mappings) {
for (const mapping of mappings) {
this.addMapping(mapping);
}
}
}
/**
* Add a folder mapping to the virtual storage.
* @param mapping The mapping to add
*/
addMapping(mapping) {
this._mappings.push(mapping);
this.rootFolder.addVirtualSubfolder(mapping.virtualName, mapping.realFolder);
}
/**
* Remove a folder mapping from the virtual storage.
* @param virtualName The virtual name to remove
*/
removeMapping(virtualName) {
this._mappings = this._mappings.filter((m) => m.virtualName !== virtualName);
this.rootFolder.removeVirtualSubfolder(virtualName);
}
/**
* Get all current mappings.
*/
getMappings() {
return [...this._mappings];
}
async getAvailable() {
this.available = true;
return this.available;
}
getUsesPollingBasedUpdating() {
return false;
}
async incrementalScanForChanges() {
// Scan each mapped folder for changes
for (const mapping of this._mappings) {
if (mapping.realFolder.storage) {
await mapping.realFolder.storage.incrementalScanForChanges();
}
}
}
}
exports.default = VirtualFolderStorage;
/**
* The root folder of a VirtualFolderStorage.
* Maps virtual folder names to real folder implementations.
*/
class VirtualRootFolder extends FolderBase_1.default {
_storage;
_virtualFolders = new Map();
_name = "";
folders = {};
files = {}; // Root folder has no files
get name() {
return this._name;
}
get parentFolder() {
return null;
}
get storage() {
return this._storage;
}
get fullPath() {
return "/";
}
get extendedPath() {
return "/";
}
constructor(storage) {
super();
this._storage = storage;
}
addVirtualSubfolder(name, realFolder) {
this._virtualFolders.set(name, realFolder);
this.folders[name] = realFolder;
}
removeVirtualSubfolder(name) {
this._virtualFolders.delete(name);
delete this.folders[name];
}
ensureFolder(name) {
const existing = this._virtualFolders.get(name);
if (existing) {
return existing;
}
throw new Error(`Virtual folder '${name}' does not exist`);
}
ensureFile(_name) {
throw new Error("Cannot create files in virtual root folder");
}
async exists() {
return true;
}
async ensureExists() {
return true;
}
async load(_force) {
// Load all virtual subfolders
for (const folder of this._virtualFolders.values()) {
await folder.load(_force);
}
return new Date();
}
async saveAll(_force) {
// Save all virtual subfolders
for (const folder of this._virtualFolders.values()) {
await folder.saveAll(_force);
}
return true;
}
async moveTo(_newStorageRelativePath) {
throw new Error("Cannot move virtual root folder");
}
async rename(_newName) {
throw new Error("Cannot rename virtual root folder");
}
async deleteThisFolder() {
throw new Error("Cannot delete virtual root folder");
}
async deleteFile(_name) {
throw new Error("No files in virtual root folder");
}
async deleteFileFromRelativePath(_path) {
throw new Error("Cannot delete files from virtual root folder");
}
async createFile(_name) {
throw new Error("Cannot create files in virtual root folder");
}
async deleteAllFolderContents() {
throw new Error("Cannot delete virtual root folder contents");
}
removeFolder(_name) {
return false;
}
getFolderFromRelativePathLocal(relativePath) {
const parts = relativePath.split("/").filter((p) => p.length > 0);
if (parts.length === 0) {
return this;
}
const firstPart = parts[0];
const folder = this._virtualFolders.get(firstPart);
if (!folder) {
return undefined;
}
if (parts.length === 1) {
return folder;
}
const remainingPath = parts.slice(1).join("/");
return folder.getFolderFromRelativePathLocal(remainingPath);
}
getFolderByIndex(index) {
const keys = this.getSortedFolderKeys();
if (index < 0 || index >= keys.length) {
return undefined;
}
return this.folders[keys[index]];
}
getSortedFolderKeys() {
return Array.from(this._virtualFolders.keys()).sort();
}
getSortedFileKeys() {
return [];
}
async ensureFolderFromRelativePath(relativePath) {
const result = await this.getFolderFromRelativePath(relativePath);
if (!result) {
throw new Error(`Folder not found: ${relativePath}`);
}
return result;
}
async ensureFileFromRelativePath(_relativePath) {
throw new Error("Cannot create files in virtual root folder");
}
async setStructureFromFileList(_fileList) {
throw new Error("Cannot set structure on virtual root folder");
}
clearAllManagers() {
// No-op for virtual folder
}
getFolderRelativePath(_toFolder) {
return undefined;
}
folderExists(name) {
return this._virtualFolders.has(name);
}
fileExists(_name) {
return false;
}
async getFileFromRelativePath(relativePath) {
const parts = relativePath.split("/").filter((p) => p.length > 0);
if (parts.length === 0) {
return undefined;
}
const firstPart = parts[0];
const folder = this._virtualFolders.get(firstPart);
if (!folder) {
return undefined;
}
const remainingPath = parts.slice(1).join("/");
if (remainingPath.length === 0) {
return undefined;
}
return folder.getFileFromRelativePath(remainingPath);
}
async getFolderFromRelativePath(relativePath) {
const parts = relativePath.split("/").filter((p) => p.length > 0);
if (parts.length === 0) {
return this;
}
const firstPart = parts[0];
const folder = this._virtualFolders.get(firstPart);
if (!folder) {
return undefined;
}
if (parts.length === 1) {
return folder;
}
const remainingPath = parts.slice(1).join("/");
return folder.getFolderFromRelativePath(remainingPath);
}
async scanForChanges() {
for (const folder of this._virtualFolders.values()) {
await folder.scanForChanges();
}
}
}