UNPKG

@egodigital/egoose

Version:

Helper classes and functions for Node.js 10 or later.

164 lines 5.11 kB
"use strict"; /** * This file is part of the @egodigital/egoose distribution. * Copyright (c) e.GO Digital GmbH, Aachen, Germany (https://www.e-go-digital.com/) * * @egodigital/egoose is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation, version 3. * * @egodigital/egoose is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ Object.defineProperty(exports, "__esModule", { value: true }); const fsExtra = require("fs-extra"); const path = require("path"); const yazl = require("yazl"); const fs_1 = require("../fs"); const index_1 = require("../index"); /** * Builds a ZIP file. */ class ZipBuilder { constructor() { this._buffer = false; this._STEPS = []; } /** * Adds a buffer. * * @param {string} p The path in the zip file. * @param {Buffer} data The data to write. * * @return this */ addBuffer(p, data) { this._STEPS.push((zip) => { zip.addBuffer(data, normalizeZipPath(p)); }); return this; } /** * Adds an empty directory. * * @param {string} p The path in the zip file. * * @return this */ addDir(p) { this._STEPS.push((zip) => { zip.addEmptyDirectory(normalizeZipPath(p)); }); return this; } /** * Adds all files of a local directory. * * @param {string} dir The path of the local directory. * @param {string} [basePath] The custom base (zip) path. */ addFiles(dir, basePath) { dir = index_1.toStringSafe(dir); if (index_1.isEmptyString(dir)) { dir = process.cwd(); } if (!path.isAbsolute(dir)) { dir = path.join(process.cwd(), dir); } dir = path.resolve(dir); basePath = normalizeZipPath(basePath) + '/'; this._STEPS.push(async (zip) => { const FILES = await fs_1.glob('**/**', { absolute: true, cwd: dir, dot: true, onlyFiles: true, unique: true, }); for (const F of FILES) { const ZIP_PATH = normalizeZipPath(basePath + normalizeZipPath(path.relative(dir, F))); zip.addFile(F, ZIP_PATH); } }); return this; } async createZipInstance() { const NEW_FILE = new yazl.ZipFile(); for (const S of this._STEPS) { await Promise.resolve(S(NEW_FILE)); } return NEW_FILE; } /** * Creates a new ZIP file as buffer. * * @return {Promise<Buffer>} The promise with the buffer. */ async toBuffer() { if (Buffer.isBuffer(this._buffer)) { return this._buffer; } const NEW_FILE = await this.createZipInstance(); // first output to temp file ... return await fs_1.tempFile(async (tf) => { return await (() => { return new Promise((resolve, reject) => { try { const PIPE = NEW_FILE.outputStream.pipe(fsExtra.createWriteStream(tf)); PIPE.once('error', (err) => { reject(err); }); PIPE.once('close', () => { // now read the data and return (async () => { return await fsExtra.readFile(tf); })().then((data) => { // and save data before // delete temp this._buffer = data; resolve(data); }).catch(e => { reject(e); }); }); NEW_FILE.end(); } catch (e) { reject(e); } }); })(); }); } } exports.ZipBuilder = ZipBuilder; /** * Starts building a ZIP file. * * @return {ZipBuilder} The new builder instance. */ function buildZip() { return new ZipBuilder(); } exports.buildZip = buildZip; function normalizeZipPath(p) { p = index_1.toStringSafe(p) .replace(path.sep, '/') .trim(); // remove leading and ending / while (p.startsWith('/')) { p = p.substr(1) .trim(); } while (p.endsWith('/')) { p = p.substr(0, p.length - 1) .trim(); } return p; } //# sourceMappingURL=builder.js.map