UNPKG

@ts-common/azure-js-dev-tools

Version:

Developer dependencies for TypeScript related projects

134 lines 5.33 kB
"use strict"; /** * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for * license information. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.ArchiverCompressor = exports.FakeCompressor = void 0; var tslib_1 = require("tslib"); var archiver = tslib_1.__importStar(require("archiver")); var fs = tslib_1.__importStar(require("fs")); var fileSystem2_1 = require("./fileSystem2"); var arrays_1 = require("./arrays"); /** * A fake Compressor. */ var FakeCompressor = /** @class */ (function () { function FakeCompressor() { this.errors = []; this.warnings = []; } FakeCompressor.prototype.zipFiles = function (filePaths, outputFilePath) { return tslib_1.__awaiter(this, void 0, void 0, function () { var zipFileContents, _a, _b, filePath, result; var e_1, _c; return tslib_1.__generator(this, function (_d) { switch (_d.label) { case 0: zipFileContents = "files:\n"; try { for (_a = tslib_1.__values(arrays_1.toArray(filePaths)), _b = _a.next(); !_b.done; _b = _a.next()) { filePath = _b.value; zipFileContents += filePath + "\n"; } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_b && !_b.done && (_c = _a.return)) _c.call(_a); } finally { if (e_1) throw e_1.error; } } return [4 /*yield*/, fileSystem2_1.writeFileContents(outputFilePath, zipFileContents)]; case 1: _d.sent(); result = { errors: this.errors, warnings: this.warnings, }; return [2 /*return*/, result]; } }); }); }; FakeCompressor.prototype.zipFolder = function (folderPath, outputFilePath) { return tslib_1.__awaiter(this, void 0, void 0, function () { var zipFileContents, result; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: zipFileContents = "folder: " + folderPath; return [4 /*yield*/, fileSystem2_1.writeFileContents(outputFilePath, zipFileContents)]; case 1: _a.sent(); result = { errors: this.errors, warnings: this.warnings, }; return [2 /*return*/, result]; } }); }); }; return FakeCompressor; }()); exports.FakeCompressor = FakeCompressor; /** * A Compressor that uses the archiver NPM package to do compression. */ var ArchiverCompressor = /** @class */ (function () { function ArchiverCompressor() { } ArchiverCompressor.prototype.zip = function (outputFilePath, addContents) { return new Promise(function (resolve, reject) { try { var arc = archiver.create("zip"); addContents(arc); var output = fs.createWriteStream(outputFilePath); arc.pipe(output); var errors_1 = []; arc.on("error", function (error) { return errors_1.push(error); }); var warnings_1 = []; arc.on("warning", function (warning) { return warnings_1.push(warning); }); arc.on("finish", function () { var result = { errors: errors_1, warnings: warnings_1 }; resolve(result); }); arc.finalize(); } catch (error) { reject(error); } }); }; ArchiverCompressor.prototype.zipFiles = function (filePaths, outputFilePath) { return this.zip(outputFilePath, function (arc) { var e_2, _a; try { for (var _b = tslib_1.__values(arrays_1.toArray(filePaths)), _c = _b.next(); !_c.done; _c = _b.next()) { var filePath = _c.value; arc.file(filePath, { name: filePath }); } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_2) throw e_2.error; } } }); }; ArchiverCompressor.prototype.zipFolder = function (folderPath, outputFilePath) { return this.zip(outputFilePath, function (arc) { arc.directory(folderPath, false); }); }; return ArchiverCompressor; }()); exports.ArchiverCompressor = ArchiverCompressor; //# sourceMappingURL=compressor.js.map