UNPKG

@rushstack/package-extractor

Version:

A library for bundling selected files and dependencies into a deployable package.

64 lines 2.83 kB
"use strict"; // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. // See LICENSE in the project root for license information. var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ArchiveManager = void 0; const jszip_1 = __importDefault(require("jszip")); const node_core_library_1 = require("@rushstack/node-core-library"); // 755 are default permissions to allow read/write/execute for owner and read/execute for group and others. const DEFAULT_FILE_PERMISSIONS = 0o755; // This value sets the allowed permissions when preserving symbolic links. // 120000 is the symbolic link identifier, and is OR'd with the default file permissions. // See: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/uapi/linux/stat.h#n10 // eslint-disable-next-line no-bitwise const SYMBOLIC_LINK_PERMISSIONS = 0o120000 | DEFAULT_FILE_PERMISSIONS; class ArchiveManager { constructor() { this._zip = new jszip_1.default(); } async addToArchiveAsync(options) { var _a; const { filePath, fileData, archivePath } = options; let data; let permissions; if (filePath) { const stats = (_a = options.stats) !== null && _a !== void 0 ? _a : (await node_core_library_1.FileSystem.getLinkStatisticsAsync(filePath)); if (stats.isSymbolicLink()) { data = await node_core_library_1.FileSystem.readLinkAsync(filePath); permissions = SYMBOLIC_LINK_PERMISSIONS; } else if (stats.isDirectory()) { throw new Error('Directories cannot be added to the archive'); } else { data = await node_core_library_1.FileSystem.readFileToBufferAsync(filePath); permissions = stats.mode; } } else if (fileData) { data = fileData; permissions = DEFAULT_FILE_PERMISSIONS; } else { throw new Error('Either filePath or fileData must be provided'); } // Replace backslashes for Unix compat const addPath = node_core_library_1.Path.convertToSlashes(archivePath); this._zip.file(addPath, data, { unixPermissions: permissions, dir: false }); } async createArchiveAsync(archiveFilePath) { const zipContent = await this._zip.generateAsync({ type: 'nodebuffer', platform: 'UNIX' }); await node_core_library_1.FileSystem.writeFileAsync(archiveFilePath, zipContent); } } exports.ArchiveManager = ArchiveManager; //# sourceMappingURL=ArchiveManager.js.map