1 | ;
|
2 | /**
|
3 | * This file is part of the @egodigital/egoose distribution.
|
4 | * Copyright (c) e.GO Digital GmbH, Aachen, Germany (https://www.e-go-digital.com/)
|
5 | *
|
6 | * @egodigital/egoose is free software: you can redistribute it and/or modify
|
7 | * it under the terms of the GNU Lesser General Public License as
|
8 | * published by the Free Software Foundation, version 3.
|
9 | *
|
10 | * @egodigital/egoose is distributed in the hope that it will be useful, but
|
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
13 | * Lesser General Public License for more details.
|
14 | *
|
15 | * You should have received a copy of the GNU Lesser General Public License
|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
17 | */
|
18 | Object.defineProperty(exports, "__esModule", { value: true });
|
19 | const fsExtra = require("fs-extra");
|
20 | const path = require("path");
|
21 | const yazl = require("yazl");
|
22 | const fs_1 = require("../fs");
|
23 | const index_1 = require("../index");
|
24 | /**
|
25 | * Builds a ZIP file.
|
26 | */
|
27 | class ZipBuilder {
|
28 | constructor() {
|
29 | this._buffer = false;
|
30 | this._STEPS = [];
|
31 | }
|
32 | /**
|
33 | * Adds a buffer.
|
34 | *
|
35 | * @param {string} p The path in the zip file.
|
36 | * @param {Buffer} data The data to write.
|
37 | *
|
38 | * @return this
|
39 | */
|
40 | addBuffer(p, data) {
|
41 | this._STEPS.push((zip) => {
|
42 | zip.addBuffer(data, normalizeZipPath(p));
|
43 | });
|
44 | return this;
|
45 | }
|
46 | /**
|
47 | * Adds an empty directory.
|
48 | *
|
49 | * @param {string} p The path in the zip file.
|
50 | *
|
51 | * @return this
|
52 | */
|
53 | addDir(p) {
|
54 | this._STEPS.push((zip) => {
|
55 | zip.addEmptyDirectory(normalizeZipPath(p));
|
56 | });
|
57 | return this;
|
58 | }
|
59 | /**
|
60 | * Adds all files of a local directory.
|
61 | *
|
62 | * @param {string} dir The path of the local directory.
|
63 | * @param {string} [basePath] The custom base (zip) path.
|
64 | */
|
65 | addFiles(dir, basePath) {
|
66 | dir = index_1.toStringSafe(dir);
|
67 | if (index_1.isEmptyString(dir)) {
|
68 | dir = process.cwd();
|
69 | }
|
70 | if (!path.isAbsolute(dir)) {
|
71 | dir = path.join(process.cwd(), dir);
|
72 | }
|
73 | dir = path.resolve(dir);
|
74 | basePath = normalizeZipPath(basePath) + '/';
|
75 | this._STEPS.push(async (zip) => {
|
76 | const FILES = await fs_1.glob('**/**', {
|
77 | absolute: true,
|
78 | cwd: dir,
|
79 | deep: true,
|
80 | dot: true,
|
81 | onlyFiles: true,
|
82 | unique: true,
|
83 | });
|
84 | for (const F of FILES) {
|
85 | const ZIP_PATH = normalizeZipPath(basePath + normalizeZipPath(path.relative(dir, F)));
|
86 | zip.addFile(F, ZIP_PATH);
|
87 | }
|
88 | });
|
89 | return this;
|
90 | }
|
91 | async createZipInstance() {
|
92 | const NEW_FILE = new yazl.ZipFile();
|
93 | for (const S of this._STEPS) {
|
94 | await Promise.resolve(S(NEW_FILE));
|
95 | }
|
96 | return NEW_FILE;
|
97 | }
|
98 | /**
|
99 | * Creates a new ZIP file as buffer.
|
100 | *
|
101 | * @return {Promise<Buffer>} The promise with the buffer.
|
102 | */
|
103 | async toBuffer() {
|
104 | if (Buffer.isBuffer(this._buffer)) {
|
105 | return this._buffer;
|
106 | }
|
107 | const NEW_FILE = await this.createZipInstance();
|
108 | // first output to temp file ...
|
109 | return await fs_1.tempFile(async (tf) => {
|
110 | return await (() => {
|
111 | return new Promise((resolve, reject) => {
|
112 | try {
|
113 | const PIPE = NEW_FILE.outputStream.pipe(fsExtra.createWriteStream(tf));
|
114 | PIPE.once('error', (err) => {
|
115 | reject(err);
|
116 | });
|
117 | PIPE.once('close', () => {
|
118 | // now read the data and return
|
119 | (async () => {
|
120 | return await fsExtra.readFile(tf);
|
121 | })().then((data) => {
|
122 | // and save data before
|
123 | // delete temp
|
124 | this._buffer = data;
|
125 | resolve(data);
|
126 | }).catch(e => {
|
127 | reject(e);
|
128 | });
|
129 | });
|
130 | NEW_FILE.end();
|
131 | }
|
132 | catch (e) {
|
133 | reject(e);
|
134 | }
|
135 | });
|
136 | })();
|
137 | });
|
138 | }
|
139 | }
|
140 | exports.ZipBuilder = ZipBuilder;
|
141 | /**
|
142 | * Starts building a ZIP file.
|
143 | *
|
144 | * @return {ZipBuilder} The new builder instance.
|
145 | */
|
146 | function buildZip() {
|
147 | return new ZipBuilder();
|
148 | }
|
149 | exports.buildZip = buildZip;
|
150 | function normalizeZipPath(p) {
|
151 | p = index_1.toStringSafe(p)
|
152 | .replace(path.sep, '/')
|
153 | .trim();
|
154 | // remove leading and ending /
|
155 | while (p.startsWith('/')) {
|
156 | p = p.substr(1)
|
157 | .trim();
|
158 | }
|
159 | while (p.endsWith('/')) {
|
160 | p = p.substr(0, p.length - 1)
|
161 | .trim();
|
162 | }
|
163 | return p;
|
164 | }
|
165 | //# sourceMappingURL=builder.js.map |
\ | No newline at end of file |