UNPKG

fast-extract

Version:

Extract contents from various archive types (tar, tar.bz2, tar.gz, tar.xz, tgz, zip)

38 lines (37 loc) 1.18 kB
import fs from 'fs'; import Queue from 'queue-cb'; import rimraf2 from 'rimraf2'; import tempSuffix from 'temp-suffix'; import writer from '../../compat/flush-write-stream.js'; export default function createWriteEntriesStream(dest, options = {}) { options = { now: new Date(), ...options }; const tempDest = tempSuffix(dest); const links = []; return writer({ objectMode: true }, function write(entry, _encoding, callback) { if (entry.type === 'link') { links.unshift(entry); return callback(); } if (entry.type === 'symlink') { links.push(entry); return callback(); } entry.create(tempDest, options, callback); }, function flush(callback) { const queue = new Queue(1); queue.defer(rimraf2.bind(null, dest, { disableGlob: true })); queue.defer(fs.rename.bind(fs, tempDest, dest)); for(let index = 0; index < links.length; index++){ const entry = links[index]; queue.defer(entry.create.bind(entry, dest, options)); } queue.await(callback); }); }