@minecraft/creator-tools
Version:
Minecraft Creator Tools command line and libraries.
211 lines (205 loc) • 7.99 kB
JavaScript
"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const NodeStorage_1 = __importDefault(require("./NodeStorage"));
const FileBase_1 = __importDefault(require("../storage/FileBase"));
const StorageUtilities_1 = __importStar(require("../storage/StorageUtilities"));
const fs = __importStar(require("fs"));
class NodeFile extends FileBase_1.default {
_name;
_parentFolder;
get name() {
return this._name;
}
get fullPath() {
let path = this._parentFolder.fullPath;
if (!path.endsWith(NodeStorage_1.default.platformFolderDelimiter)) {
path += NodeStorage_1.default.platformFolderDelimiter;
}
return path + this.name;
}
get parentFolder() {
return this._parentFolder;
}
get isContentLoaded() {
return this.lastLoadedOrSaved != null || this.modified != null;
}
constructor(parentFolder, folderName) {
super();
this._parentFolder = parentFolder;
this._name = folderName;
}
async scanForChanges() {
// No-op for node storage
}
async exists() {
return fs.existsSync(this.fullPath);
}
loadContentSync(force) {
if (force || this.lastLoadedOrSaved == null) {
const encoding = StorageUtilities_1.default.getEncodingByFileName(this._name);
if (!fs.existsSync(this.fullPath)) {
this._content = null;
}
else if (encoding === StorageUtilities_1.EncodingType.ByteBuffer) {
// Log.debug(`NodeFS loading '${this.fullPath}' as binary.`);
const byteResult = fs.readFileSync(this.fullPath);
if (byteResult instanceof ArrayBuffer) {
this._content = new Uint8Array(byteResult);
}
else {
this._content = byteResult;
}
}
else {
// Log.debug(`NodeFS loading '${this.fullPath}' as text.`);
this._content = fs.readFileSync(this.fullPath, { encoding: "utf8" });
}
// this._content += "";
this.lastLoadedOrSaved = new Date();
}
return this.lastLoadedOrSaved;
}
async loadContent(force) {
return this.loadContentSync(force);
}
setContent(newContent, updateType) {
const areEqual = StorageUtilities_1.default.contentsAreEqual(this._content, newContent);
if (areEqual) {
return false;
}
if (!this.lastLoadedOrSaved) {
this.lastLoadedOrSaved = new Date();
this.lastLoadedOrSaved = new Date(this.lastLoadedOrSaved.getTime() - 1);
// Log.debugAlert("Setting a file without loading it first.");
}
let oldContent = this._content;
this._content = newContent;
this.contentWasModified(oldContent, updateType);
return true;
}
/* update: rely consistenly on getHash() implementation in FileBase which uses the js-md5 library
async getHash(): Promise<string | undefined> {
await this.loadContent(false);
if (this._content === undefined || this._content === null) {
return undefined;
}
const hash = crypto.createHash("MD5");
hash.update(this._content);
return hash.digest("base64");
}
*/
async saveContent() {
if (this.parentFolder.storage.readOnly) {
throw new Error("Can't save read-only file.");
}
if (this.needsSave) {
this.lastLoadedOrSaved = new Date();
if (this.content != null) {
this.parentFolder.ensureExists();
const encoding = StorageUtilities_1.default.getEncodingByFileName(this._name);
if (encoding === StorageUtilities_1.EncodingType.ByteBuffer) {
// Log.verbose("Saving '" + this.fullPath + "' as binary. size: " + this.content.length);
fs.writeFileSync(this.fullPath, this.content);
}
else {
// Log.verbose("Saving '" + this.fullPath + "' as text. size: " + this.content.length);
fs.writeFileSync(this.fullPath, this.content, { encoding: "utf8" });
}
}
}
if (this.lastLoadedOrSaved === null) {
this.lastLoadedOrSaved = new Date();
}
return this.lastLoadedOrSaved;
}
async writeContent(content) {
this.lastLoadedOrSaved = new Date();
await this._parentFolder.ensureExists();
return new Promise((resolve, reject) => {
const writer = fs.createWriteStream(this.fullPath);
writer.on("finish", resolve);
writer.on("error", reject);
for (const str of content) {
writer.write(str + "\n");
}
writer.end();
});
}
async deleteThisFile(skipRemoveFromParent) {
if (this.parentFolder.storage.readOnly) {
throw new Error("Can't save read-only file.");
}
if (skipRemoveFromParent !== true) {
this._parentFolder._removeFile(this);
}
return this._deleteItem(this.fullPath);
}
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.");
}
await this.loadContent(false);
const originalPath = this.fullPath;
this._name = newFileName;
this._parentFolder = newParentFolder;
this.modified = new Date();
newParentFolder._addExistingFile(this);
this._deleteItem(originalPath);
return true;
}
async _deleteItem(path) {
let isSuccess = true;
try {
fs.rmSync(path);
}
catch (e) {
isSuccess = false;
}
return isSuccess;
}
}
exports.default = NodeFile;