├── .editorconfig ├── .gitattributes ├── .gitignore ├── .npmrc ├── .travis.yml ├── fixture.tar.gz ├── index.js ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - '12' 5 | - '10' 6 | -------------------------------------------------------------------------------- /fixture.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kevva/gulp-decompress/d1dbd9f0c8de0bd909f86eb3d27cf709dadd31f5/fixture.tar.gz -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const fs = require('fs'); 3 | const archiveType = require('archive-type'); 4 | const decompress = require('decompress'); 5 | const PluginError = require('plugin-error'); 6 | const {Transform} = require('readable-stream'); 7 | const Vinyl = require('vinyl'); 8 | 9 | module.exports = options => new Transform({ 10 | objectMode: true, 11 | async transform(file, enc, cb) { 12 | if (file.isNull()) { 13 | cb(null, file); 14 | return; 15 | } 16 | 17 | if (file.isStream()) { 18 | cb(new PluginError('gulp-decompress', 'Streaming is not supported')); 19 | return; 20 | } 21 | 22 | if (!archiveType(file.contents)) { 23 | cb(null, file); 24 | return; 25 | } 26 | 27 | try { 28 | const decompressedFiles = await decompress(file.contents, options); 29 | 30 | for (const decompressedFile of decompressedFiles) { 31 | const stats = new fs.Stats(); 32 | 33 | stats.mtime = decompressedFile.mtime; 34 | 35 | if (decompressedFile.type === 'symlink') { 36 | stats.isSymbolicLink = () => true; 37 | } else { 38 | stats.mode = decompressedFile.mode; 39 | stats.isDirectory = () => decompressedFile.type === 'directory'; 40 | } 41 | 42 | const vinylOptions = { 43 | stat: stats, 44 | contents: (stats.isDirectory() || stats.isSymbolicLink()) ? null : decompressedFile.data, 45 | path: decompressedFile.path 46 | }; 47 | 48 | if (decompressedFile.linkname) { 49 | vinylOptions.symlink = decompressedFile.linkname; 50 | } 51 | 52 | this.push(new Vinyl(vinylOptions)); 53 | } 54 | 55 | cb(); 56 | } catch (error) { 57 | cb(new PluginError('gulp-decompress:', error, {fileName: file.path})); 58 | } 59 | } 60 | }); 61 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Kevin Mårtensson (github.com/kevva) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gulp-decompress", 3 | "version": "3.0.0", 4 | "description": "Extract TAR, TAR.BZ2, TAR.GZ and ZIP archives", 5 | "license": "MIT", 6 | "repository": "kevva/gulp-decompress", 7 | "author": { 8 | "name": "Kevin Mårtensson", 9 | "email": "kevinmartensson@gmail.com", 10 | "url": "https://github.com/kevva" 11 | }, 12 | "engines": { 13 | "node": ">=10" 14 | }, 15 | "scripts": { 16 | "test": "xo && ava" 17 | }, 18 | "files": [ 19 | "index.js" 20 | ], 21 | "keywords": [ 22 | "bz2", 23 | "bzip2", 24 | "decompress", 25 | "extract", 26 | "gulpplugin", 27 | "tar", 28 | "tar.bz", 29 | "tar.gz", 30 | "unzip", 31 | "zip" 32 | ], 33 | "dependencies": { 34 | "archive-type": "^4.0.0", 35 | "decompress": "^4.0.0", 36 | "plugin-error": "^1.0.1", 37 | "readable-stream": "^3.4.0", 38 | "vinyl": "^2.1.0" 39 | }, 40 | "devDependencies": { 41 | "ava": "^2.3.0", 42 | "get-stream": "^5.1.0", 43 | "is-jpg": "^2.0.0", 44 | "xo": "^0.24.0" 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # gulp-decompress [![Build Status](https://travis-ci.org/kevva/gulp-decompress.svg?branch=master)](https://travis-ci.org/kevva/gulp-decompress) 2 | 3 | > Extract TAR, TAR.BZ2, TAR.GZ and ZIP archives using [decompress](https://github.com/kevva/decompress) 4 | 5 | 6 | ## Install 7 | 8 | ``` 9 | $ npm install gulp-decompress 10 | ``` 11 | 12 | 13 | ## Usage 14 | 15 | ```js 16 | const decompress = require('gulp-decompress'); 17 | const gulp = require('gulp'); 18 | 19 | gulp.task('default', () => 20 | gulp.src('*.{tar,tar.bz2,tar.gz,zip}') 21 | .pipe(decompress({strip: 1})) 22 | .pipe(gulp.dest('dist')) 23 | ); 24 | ``` 25 | 26 | 27 | ## Options 28 | 29 | See the [decompress options](https://github.com/kevva/decompress#options). 30 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import {promises as fs} from 'fs'; 2 | import path from 'path'; 3 | import Vinyl from 'vinyl'; 4 | import isJpg from 'is-jpg'; 5 | import getStream from 'get-stream'; 6 | import test from 'ava'; 7 | import gulpDecompress from '.'; 8 | 9 | const createStream = async () => { 10 | const buf = await fs.readFile('fixture.tar.gz'); 11 | const stream = gulpDecompress(); 12 | 13 | stream.end(new Vinyl({ 14 | path: path.join(__dirname, 'fixture.tar.gz'), 15 | contents: buf 16 | })); 17 | 18 | return stream; 19 | }; 20 | 21 | test('extract file', async t => { 22 | const stream = await createStream(); 23 | const files = await getStream.array(stream); 24 | 25 | t.is(files[1].path, 'test.jpg'); 26 | t.is(typeof files[1].stat, 'object'); 27 | t.true(isJpg(files[1].contents)); 28 | }); 29 | 30 | test('ensure directory contents is `null`', async t => { 31 | const stream = await createStream(); 32 | const files = await getStream.array(stream); 33 | 34 | t.is(files[0].path, 'test'); 35 | t.is(files[0].contents, null); 36 | t.true(files[0].stat.isDirectory()); 37 | }); 38 | 39 | test('ensure symlinks are valid', async t => { 40 | const stream = await createStream(); 41 | const files = await getStream.array(stream); 42 | 43 | t.is(files[3].path, 'folder/batman.jpg'); 44 | t.true(isJpg(files[3].contents)); 45 | t.is(files[4].path, 'folder/batsymlink.jpg'); 46 | t.is(files[4]._symlink, 'batman.jpg'); 47 | t.is(files[4].contents, null); 48 | t.false(files[4].stat.isDirectory()); 49 | t.true(files[4].stat.isSymbolicLink()); 50 | }); 51 | --------------------------------------------------------------------------------