Skip to main content
Glama
archive-fs.js•7.11 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const archive_1 = __importDefault(require("./archive")); const fs_1 = require("fs"); const jszip_1 = __importDefault(require("jszip")); const archive_jszip_1 = __importDefault(require("./archive-jszip")); const file_helper_1 = require("../file-helper"); const extract_zip_1 = __importDefault(require("extract-zip")); const jszip_helper_1 = require("../jszip-helper"); class ArchiveFs extends archive_1.default { constructor(filename, params) { super(filename); this.dir = undefined; this.params = params; } initialize() { return __awaiter(this, void 0, void 0, function* () { this.setPaths(); yield this.assertDirs(); yield this.extractFile(this.filename); if (!this.params.name) { yield this.prepareWorkDir(this.filename); this.isRoot = true; } this.archive = true; return this; }); } setPaths() { this.dir = this.params.baseDir + '/'; this.templatesDir = this.dir + 'templates' + '/'; this.outputDir = this.dir + 'output' + '/'; this.templateDir = undefined; this.workDir = this.outputDir + this.params.workDir + '/'; } assertDirs() { return __awaiter(this, void 0, void 0, function* () { (0, file_helper_1.makeDirIfNotExists)(this.dir); (0, file_helper_1.makeDirIfNotExists)(this.templatesDir); (0, file_helper_1.makeDirIfNotExists)(this.outputDir); (0, file_helper_1.makeDirIfNotExists)(this.workDir); }); } extractFile(file) { return __awaiter(this, void 0, void 0, function* () { const targetDir = this.getTemplateDir(file); if ((0, file_helper_1.exists)(targetDir)) { return; } yield (0, extract_zip_1.default)(file, { dir: targetDir }).catch((err) => { throw err; }); }); } getTemplateDir(file) { const info = file_helper_1.FileHelper.getFileInfo(file); this.templateDir = this.templatesDir + info.base + '/'; return this.templateDir; } prepareWorkDir(templateDir) { return __awaiter(this, void 0, void 0, function* () { yield this.cleanupWorkDir(); const fromTemplate = this.getTemplateDir(templateDir); yield (0, file_helper_1.copyDir)(fromTemplate, this.workDir); }); } fileExists(file) { return (0, file_helper_1.exists)(this.getPath(file)); } folder(dir) { return __awaiter(this, void 0, void 0, function* () { const path = this.getPath(dir); const files = []; if (!(0, file_helper_1.exists)(path)) { return files; } let entries = yield fs_1.promises.readdir(path, { withFileTypes: true }); for (let entry of entries) { if (!entry.isDirectory()) { files.push({ name: dir + '/' + entry.name, relativePath: entry.name, }); } } return files; }); } read(file) { return __awaiter(this, void 0, void 0, function* () { if (!this.archive) { yield this.initialize(); } const path = this.getPath(file); return yield fs_1.promises.readFile(path); }); } getPath(file) { if (this.isRoot) { return this.workDir + file; } return this.templateDir + file; } write(file, data) { return __awaiter(this, void 0, void 0, function* () { const filename = this.workDir + file; (0, file_helper_1.ensureDirectoryExistence)(filename); yield fs_1.promises.writeFile(filename, data); return this; }); } remove(file) { return __awaiter(this, void 0, void 0, function* () { const path = this.getPath(file); if ((0, file_helper_1.exists)(path)) { yield fs_1.promises.unlink(path); } }); } output(location, params) { return __awaiter(this, void 0, void 0, function* () { yield this.writeBuffer(this); this.setOptions(params); if ((0, file_helper_1.exists)(location)) { yield fs_1.promises.rm(location); } yield (0, jszip_helper_1.compressFolder)(this.workDir, location, this.options); if (this.params.cleanupWorkDir === true) { yield this.cleanupWorkDir(); } }); } cleanupWorkDir() { return __awaiter(this, void 0, void 0, function* () { if (!(0, file_helper_1.exists)(this.workDir)) { return; } yield fs_1.promises.rm(this.workDir, { recursive: true, force: true }); }); } readXml(file) { return __awaiter(this, void 0, void 0, function* () { const isBuffered = this.fromBuffer(file); if (!isBuffered) { const buffer = yield this.read(file); if (!buffer) { throw 'no buffer: ' + file; } const xmlString = buffer.toString(); const XmlDocument = this.parseXml(xmlString); this.toBuffer(file, XmlDocument); return XmlDocument; } else { return isBuffered.content; } }); } writeXml(file, XmlDocument) { this.toBuffer(file, XmlDocument); } /** * Used for worksheets only **/ extract(file) { return __awaiter(this, void 0, void 0, function* () { const contents = (yield this.read(file)); const zip = new jszip_1.default(); const newArchive = new archive_jszip_1.default(file); newArchive.archive = yield zip.loadAsync(contents); return newArchive; }); } } exports.default = ArchiveFs; //# sourceMappingURL=archive-fs.js.map

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/guangxiangdebizi/PPT-MCP'

If you have feedback or need assistance with the MCP directory API, please join our Discord server