@minecraft/creator-tools
Version:
Minecraft Creator Tools command line and libraries.
91 lines (90 loc) • 3.33 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 });
const Log_1 = __importDefault(require("../core/Log"));
const FileBase_1 = __importDefault(require("./FileBase"));
const Storage_1 = __importDefault(require("./Storage"));
const StorageUtilities_1 = __importDefault(require("./StorageUtilities"));
class File extends FileBase_1.default {
_name;
_parentFolder;
get name() {
return this._name;
}
get isContentLoaded() {
return true;
}
get parentFolder() {
return this._parentFolder;
}
get fullPath() {
return this._parentFolder.fullPath + Storage_1.default.folderDelimiter + this.name;
}
constructor(parentFolder, folderName) {
super();
this._parentFolder = parentFolder;
this._name = folderName;
}
async scanForChanges() {
// No-op for in-memory storage
}
async exists() {
return true;
}
async loadContent(force) {
this.lastLoadedOrSaved = new Date();
return this.lastLoadedOrSaved;
}
async deleteThisFile(skipRemoveFromParent) {
if (this.parentFolder.storage.readOnly) {
throw new Error("Can't save read-only file.");
}
Log_1.default.verbose("Deleting file '" + this.storageRelativePath + "'");
if (skipRemoveFromParent !== true) {
this._parentFolder._removeFile(this);
}
return true;
}
async moveTo(newStorageRelativePath) {
const newFolderPath = StorageUtilities_1.default.getFolderPath(newStorageRelativePath);
const newFileName = StorageUtilities_1.default.getLeafName(newStorageRelativePath);
if (newFileName.length < 2) {
throw new Error("New path is not correct.");
}
const newParentFolder = await this._parentFolder.storage.ensureFolderFromStorageRelativePath(newFolderPath);
if (newParentFolder.files[newFileName] !== undefined) {
throw new Error("File exists at specified path.");
}
this._name = newFileName;
this._parentFolder = newParentFolder;
newParentFolder._addExistingFile(this);
return true;
}
setContent(newContent, updateType, sourceId) {
if (this._content === newContent) {
return false;
}
if (!this.lastLoadedOrSaved) {
this.lastLoadedOrSaved = new Date();
this.lastLoadedOrSaved = new Date(this.lastLoadedOrSaved.getTime() - 1);
// This is expected in serialized storage mode where files are created with initial content
// Log.debugAlert("Setting a file without loading it first.");
}
let oldContent = this._content;
this._content = newContent;
this.contentWasModified(oldContent, updateType, sourceId);
return true;
}
async saveContent() {
if (this.parentFolder.storage.readOnly) {
throw new Error("Can't save read-only file.");
}
this.lastLoadedOrSaved = new Date();
return this.lastLoadedOrSaved;
}
}
exports.default = File;