UNPKG

just-scripts

Version:
70 lines 2.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.extractTarTask = exports.createTarTask = void 0; const just_task_1 = require("just-task"); const fs_1 = require("fs"); const zlib_1 = require("zlib"); /** * Creates an tar (optionally gzipped) archive * @param options */ function createTarTask(options = { file: 'archive.tar.gz' }) { const resolvedTar = (0, just_task_1.resolve)('tar-fs'); // Inject default options options = { cwd: process.cwd(), gzip: true, ...options }; // Validate whether tar-fs is installed if (!resolvedTar) { just_task_1.logger.error('Please make sure to have "tar-fs" as a dependency in your package.json'); throw new Error('Required dependency "tar-fs" is not installed!'); } const tar = require(resolvedTar); const { entries, file, cwd, ...restOptions } = options; return function archive(done) { let tarStream = tar.pack(cwd, { entries, finalize: true, finish: () => { done(); }, ...restOptions, }); if (options.gzip) { const gzip = typeof options.gzip === 'boolean' ? (0, zlib_1.createGzip)() : (0, zlib_1.createGzip)(options.gzip); tarStream = tarStream.pipe(gzip); } tarStream.pipe((0, fs_1.createWriteStream)(options.file)); }; } exports.createTarTask = createTarTask; /** * Creates an tar (optionally gzipped) archive * @param options */ function extractTarTask(options = { file: 'archive.tar.gz' }) { const resolvedTar = (0, just_task_1.resolve)('tar-fs'); // Inject default options options = { cwd: process.cwd(), gzip: true, ...options }; // Validate whether tar-fs is installed if (!resolvedTar) { just_task_1.logger.error('Please make sure to have "tar-fs" as a dependency in your package.json'); throw new Error('Required dependency "tar-fs" is not installed!'); } const tar = require(resolvedTar); const { cwd, file, ...restOptions } = options; return function extract(done) { let tarStream = (0, fs_1.createReadStream)(file); if (options.gzip) { const gunzip = (0, zlib_1.createGunzip)(); tarStream = tarStream.pipe(gunzip); } tarStream = tarStream.pipe(tar.extract(cwd, { finalize: true, finish: () => { done(); }, ...restOptions, })); }; } exports.extractTarTask = extractTarTask; //# sourceMappingURL=tarTask.js.map