UNPKG

just-scripts

Version:
70 lines 2.55 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 = 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' ? zlib_1.createGzip() : zlib_1.createGzip(options.gzip); tarStream = tarStream.pipe(gzip); } tarStream.pipe(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 = 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 = fs_1.createReadStream(file); if (options.gzip) { const gunzip = 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