Skip to main content
Glama
file-helper.js•6.25 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 }); exports.ensureDirectoryExistence = exports.copyDir = exports.makeDir = exports.makeDirIfNotExists = exports.exists = exports.FileHelper = void 0; const fs_1 = __importDefault(require("fs")); const fs_2 = require("fs"); const path_1 = __importDefault(require("path")); const content_tracker_1 = require("./content-tracker"); const archive_jszip_1 = __importDefault(require("./archive/archive-jszip")); const archive_fs_1 = __importDefault(require("./archive/archive-fs")); class FileHelper { static importArchive(location, params) { if (!fs_1.default.existsSync(location)) { throw new Error('File not found: ' + location); } switch (params.mode) { case 'jszip': return new archive_jszip_1.default(location); case 'fs': return new archive_fs_1.default(location, params); } } static removeFromDirectory(archive, dir, cb) { return __awaiter(this, void 0, void 0, function* () { const removed = []; const files = yield archive.folder(dir); for (const file of files) { if (cb(file)) { yield archive.remove(file.relativePath); removed.push(file.name); } } return removed; }); } static getFileExtension(filename) { return path_1.default.extname(filename).replace('.', ''); } static getFileInfo(filename) { return { base: path_1.default.basename(filename), dir: path_1.default.dirname(filename), isDir: filename[filename.length - 1] === '/', extension: path_1.default.extname(filename).replace('.', ''), }; } static check(archive, file) { FileHelper.isArchive(archive); return FileHelper.fileExistsInArchive(archive, file); } static isArchive(archive) { if (archive === undefined) { throw new Error('Archive is invalid or empty.'); } } static fileExistsInArchive(archive, file) { return archive.fileExists(file); } static zipCopyWithRelations(parentClass, type, sourceNumber, targetNumber) { return __awaiter(this, void 0, void 0, function* () { const typePlural = type + 's'; yield FileHelper.zipCopyByIndex(parentClass, `ppt/${typePlural}/${type}`, sourceNumber, targetNumber); yield FileHelper.zipCopyByIndex(parentClass, `ppt/${typePlural}/_rels/${type}`, sourceNumber, targetNumber, '.xml.rels'); }); } static zipCopyByIndex(parentClass, prefix, sourceId, targetId, suffix) { return __awaiter(this, void 0, void 0, function* () { suffix = suffix || '.xml'; return FileHelper.zipCopy(parentClass.sourceArchive, `${prefix}${sourceId}${suffix}`, parentClass.targetArchive, `${prefix}${targetId}${suffix}`); }); } /** * Copies a file from one archive to another. The new file can have a different name to the origin. * @param {IArchive} sourceArchive - Source archive * @param {string} sourceFile - file path and name inside source archive * @param {IArchive} targetArchive - Target archive * @param {string} targetFile - file path and name inside target archive * @return {IArchive} targetArchive as an instance of IArchive */ static zipCopy(sourceArchive, sourceFile, targetArchive, targetFile) { return __awaiter(this, void 0, void 0, function* () { FileHelper.check(sourceArchive, sourceFile); content_tracker_1.contentTracker.trackFile(targetFile); const content = yield sourceArchive .read(sourceFile, 'nodebuffer') .catch((e) => { throw e; }); return targetArchive.write(targetFile || sourceFile, content); }); } } exports.FileHelper = FileHelper; const exists = (dir) => { return fs_1.default.existsSync(dir); }; exports.exists = exists; const makeDirIfNotExists = (dir) => { if (!(0, exports.exists)(dir)) { (0, exports.makeDir)(dir); } }; exports.makeDirIfNotExists = makeDirIfNotExists; const makeDir = (dir) => { try { if (!fs_1.default.existsSync(dir)) { fs_1.default.mkdirSync(dir); } } catch (err) { throw err; } }; exports.makeDir = makeDir; const copyDir = (src, dest) => __awaiter(void 0, void 0, void 0, function* () { yield fs_2.promises.mkdir(dest, { recursive: true }); let entries = yield fs_2.promises.readdir(src, { withFileTypes: true }); for (let entry of entries) { let srcPath = path_1.default.join(src, entry.name); let destPath = path_1.default.join(dest, entry.name); entry.isDirectory() ? yield (0, exports.copyDir)(srcPath, destPath) : yield fs_2.promises.copyFile(srcPath, destPath); } }); exports.copyDir = copyDir; const ensureDirectoryExistence = (filePath) => { const dirname = path_1.default.dirname(filePath); if (fs_1.default.existsSync(dirname)) { return true; } (0, exports.ensureDirectoryExistence)(dirname); fs_1.default.mkdirSync(dirname); }; exports.ensureDirectoryExistence = ensureDirectoryExistence; //# sourceMappingURL=file-helper.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