@h4ad/node-modules-packer
Version:
<h1 align="center"> 🚀 Node Modules Packer </h1>
68 lines • 3.26 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FasterZip = void 0;
const path_1 = require("path");
const yazl_1 = require("yazl");
const fs_1 = require("./fs");
const string_stream_1 = require("./string-stream");
const string_to_uint_1 = require("./string-to-uint");
class FasterZip {
async run(rootPath, outputPath, zipArtifacts) {
await new Promise((resolve, reject) => {
(async () => {
const zipfile = new yazl_1.ZipFile();
const stream = (0, fs_1.safeCreateWriteStream)(outputPath).once('error', reject);
zipfile.outputStream.pipe(stream);
for (const artifact of zipArtifacts) {
await this.handleArtifact(artifact, zipfile, rootPath, reject).catch(reject);
}
zipfile.end();
stream.once('error', reject).once('close', () => resolve());
})();
});
}
async readdirAndAddToZip(zipFile, rootPath, source, path, onErrorOnStream) {
const filePaths = await (0, fs_1.readdirAsync)(path).then(files => files.map(file => (0, path_1.join)(path, file)));
const filePathsAndStats = await Promise.all(filePaths.map(async (filePath) => [filePath, await (0, fs_1.statAsync)(filePath)]));
await Promise.all(filePathsAndStats.map(async ([filePath, stat]) => {
if (stat.isFile()) {
if (source.shouldIgnore && source.shouldIgnore(filePath))
return;
await this.addFileToZip(zipFile, source, rootPath, filePath, onErrorOnStream);
}
else {
await this.readdirAndAddToZip(zipFile, rootPath, source, filePath, onErrorOnStream);
}
}));
}
async addFileToZip(zipFile, source, rootPath, filePath, onErrorOnStream) {
const metadataPath = (0, path_1.normalize)(source.metadataPath
? filePath.replace(source.path, source.metadataPath)
: (0, path_1.relative)(rootPath, filePath));
const transformer = source.transformer && source.transformer(filePath, metadataPath);
const readStream = (0, fs_1.safeCreateReadStream)(filePath).once('error', err => onErrorOnStream(err));
if (transformer) {
try {
const code = await (0, string_to_uint_1.streamToUInt8Array)(readStream);
const finalContent = await transformer(code);
const fileContentReadable = new string_stream_1.StringStream(finalContent);
zipFile.addReadStream(fileContentReadable, metadataPath);
}
catch (e) {
zipFile.addReadStream(readStream, metadataPath);
}
}
else
zipFile.addReadStream(readStream, metadataPath);
}
async handleArtifact(artifact, zipfile, rootPath, onErrorOnStream) {
if (artifact.type === 'directory') {
await this.readdirAndAddToZip(zipfile, rootPath, artifact, artifact.path, onErrorOnStream);
}
else {
await this.addFileToZip(zipfile, artifact, rootPath, artifact.path, onErrorOnStream);
}
}
}
exports.FasterZip = FasterZip;
//# sourceMappingURL=zip.js.map