UNPKG

fast-extract

Version:

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

56 lines (55 loc) 1.52 kB
import fs from 'fs'; import mkdirp from 'mkdirp-classic'; import path from 'path'; import tempSuffix from 'temp-suffix'; import { Transform } from '../../compat/stream.js'; let WriteFileTransform = class WriteFileTransform extends Transform { _transform(chunk, encoding, callback) { if (this.stream) { this.stream.write(chunk, encoding, ()=>{ callback(); }); return; } mkdirp(path.dirname(this.tempPath), (err)=>{ if (err) return callback(err); this.stream = fs.createWriteStream(this.tempPath, { flags: 'w' }); this.stream.write(chunk, encoding, ()=>{ callback(); }); }); } _flush(callback) { if (!this.stream) { callback(); return; } this.stream.end(()=>{ this.stream = null; this.push(this.tempPath); this.push(null); callback(); }); } destroy(_error) { if (this.stream) { this.stream.end(); this.stream = null; } return this; } constructor(dest, options){ options = options ? { ...options, objectMode: true } : { objectMode: true }; super(options); this.tempPath = tempSuffix(dest); options._tempPaths.push(this.tempPath); } }; export { WriteFileTransform as default };