Skip to content

Commit

Permalink
Require Node.js 18 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Nov 3, 2023
1 parent c1de237 commit 3adb58b
Show file tree
Hide file tree
Showing 11 changed files with 219 additions and 222 deletions.
10 changes: 4 additions & 6 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ jobs:
fail-fast: false
matrix:
node-version:
- 14
- 12
- 10
- 8
- 20
- 18
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
node_modules
yarn.lock
/dest
.nyc_output
coverage
21 changes: 10 additions & 11 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
'use strict';
const gulp = require('gulp');
const gzip = require('gulp-gzip');
const tar = require('.');
import gulp from 'gulp';
import gzip from 'gulp-gzip';
import tar from './index.js';

exports.default = () => {
export default function main() {
// Tar in buffer mode
gulp.src('fixture/fixture.txt')
gulp.src('test/fixture/fixture.txt')
.pipe(tar('test1.tar'))
.pipe(gulp.dest('dest'));

// Tar in stream mode
gulp.src('fixture/fixture.txt', {buffer: false})
gulp.src('test/fixture/fixture.txt', {buffer: false})
.pipe(tar('test2.tar'))
.pipe(gulp.dest('dest'));

// Tar and gzip in buffer mode
gulp.src('fixture/fixture.txt')
gulp.src('test/fixture/fixture.txt')
.pipe(tar('test3.tar'))
.pipe(gzip())
.pipe(gulp.dest('dest'));

// Tar and gzip in stream mode
gulp.src('fixture/fixture.txt', {buffer: false})
gulp.src('test/fixture/fixture.txt', {buffer: false})
.pipe(tar('test4.tar'))
.pipe(gzip())
.pipe(gulp.dest('dest'));

// Check default parameters for tar-stream
gulp.src('fixture/fixture.txt')
gulp.src('test/fixture/fixture.txt')
.pipe(tar('test_options.tar', {mtime: 0}))
.pipe(gulp.dest('dest'));
};
}
59 changes: 27 additions & 32 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
'use strict';
const path = require('path');
const through = require('through2');
const archiver = require('archiver');
const PluginError = require('plugin-error');
const Vinyl = require('vinyl');

module.exports = (filename, options) => {
import path from 'node:path';
import archiver from 'archiver';
import Vinyl from 'vinyl';
import {gulpPlugin} from 'gulp-plugin-extras';

export default function gulpTar(filename, options) {
if (!filename) {
throw new PluginError('gulp-tar', '`filename` required');
throw new Error('gulp-tar: `filename` required');
}

let firstFile;
const archive = archiver('tar', options);

return through.obj((file, encoding, callback) => {
return gulpPlugin('gulp-tar', async file => {
if (file.relative === '') {
callback();
return;
}

if (firstFile === undefined) {
firstFile = file;
}

const nameNormalized = file.relative.replace(/\\/g, '/');
const nameNormalized = file.relative.replaceAll('\\', '/');

if (file.isSymbolic()) {
archive.symlink(nameNormalized, file.symlink);
Expand All @@ -32,26 +29,24 @@ module.exports = (filename, options) => {
name: nameNormalized + (file.isNull() ? '/' : ''),
mode: file.stat && file.stat.mode,
date: file.stat && file.stat.mtime ? file.stat.mtime : null,
...options
...options,
});
}

callback();
}, function (callback) {
if (firstFile === undefined) {
callback();
return;
}

archive.finalize();

this.push(new Vinyl({
cwd: firstFile.cwd,
base: firstFile.base,
path: path.join(firstFile.base, filename),
contents: archive
}));

callback();
}, {
supportsAnyType: true,
async * onFinish() {
if (firstFile === undefined) {
return;
}

archive.finalize();

yield new Vinyl({
cwd: firstFile.cwd,
base: firstFile.base,
path: path.join(firstFile.base, filename),
contents: archive,
});
},
});
};
}
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <[email protected]> (sindresorhus.com)
Copyright (c) Sindre Sorhus <[email protected]> (https://sindresorhus.com)

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:

Expand Down
33 changes: 19 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@
"description": "Create tarball from files",
"license": "MIT",
"repository": "sindresorhus/gulp-tar",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "[email protected]",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=8"
"node": ">=18"
},
"scripts": {
"test": "xo && nyc mocha"
"test": "xo && ava"
},
"files": [
"index.js"
Expand All @@ -32,20 +35,19 @@
"streams"
],
"dependencies": {
"archiver": "^3.1.1",
"plugin-error": "^1.0.1",
"through2": "^3.0.1",
"vinyl": "^2.1.0"
"archiver": "^6.0.1",
"gulp-plugin-extras": "^0.3.0",
"vinyl": "^3.0.0"
},
"devDependencies": {
"ava": "^5.3.1",
"gulp": "^4.0.2",
"gulp-gzip": "^1.2.0",
"vinyl-fs": "^3.0.3",
"mocha": "^6.2.0",
"nyc": "^14.1.1",
"rimraf": "^3.0.0",
"tar-fs": "^2.0.0",
"xo": "^0.24.0"
"gulp-gzip": "^1.4.2",
"p-event": "^6.0.0",
"rimraf": "^5.0.5",
"tar-fs": "^3.0.4",
"vinyl-fs": "^4.0.0",
"xo": "^0.56.0"
},
"peerDependencies": {
"gulp": ">=4"
Expand All @@ -54,5 +56,8 @@
"gulp": {
"optional": true
}
},
"ava": {
"serial": true
}
}
17 changes: 7 additions & 10 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,27 @@

> Create [tarball](https://en.wikipedia.org/wiki/Tar_(computing)) from files

## Install

```
$ npm install --save-dev gulp-tar
```sh
npm install --save-dev gulp-tar
```


## Usage

```js
const gulp = require('gulp');
const tar = require('gulp-tar');
const gzip = require('gulp-gzip');
import gulp from 'gulp';
import tar from 'gulp-tar';
import gzip from 'gulp-gzip';

exports.default = () => (
export default () => (
gulp.src('src/*')
.pipe(tar('archive.tar'))
.pipe(gzip())
.pipe(gulp.dest('dist'))
);
```


## API

### tar(filename, options?)
Expand All @@ -34,7 +31,7 @@ exports.default = () => (

Type: `string`

Filename for the output tar archive.
The filename for the output tar archive.

#### options

Expand Down
81 changes: 43 additions & 38 deletions test/directory.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,53 @@
/* eslint-env mocha */
const {createReadStream} = require('fs');
const path = require('path');
const zlib = require('zlib');
const assert = require('assert');
const gulp = require('gulp');
const gulpZip = require('gulp-gzip');
const tarfs = require('tar-fs');
const rimraf = require('rimraf');
const gulpTar = require('..');
import {createReadStream} from 'node:fs';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import zlib from 'node:zlib';
import {pipeline} from 'node:stream/promises';
import test from 'ava';
import {pEvent} from 'p-event';
import gulp from 'gulp';
import gulpZip from 'gulp-gzip';
import tarfs from 'tar-fs';
import {rimrafSync} from 'rimraf';
import gulpTar from '../index.js';

it('should include directories', done => {
const onTargz = () => {
const __dirname = path.dirname(fileURLToPath(import.meta.url));

test('should include directories', async t => {
const onTargz = async () => {
const filename = 'archive.tar.gz';
const fullPath = path.join(__dirname, 'dest', filename);
const rs = createReadStream(fullPath);

rs.pipe(zlib.createGunzip())
.pipe(
tarfs.extract(path.join(__dirname, 'dest-out'), {
mapStream: (filestream, header) => {
const expected = expectedNames.pop();
assert.strictEqual(header.name, expected);
return filestream;
}
})
)
.on('finish', () => {
for (const directory of ['dest', 'dest-out']) {
rimraf.sync(path.join(__dirname, directory));
}
const expectedNames = [
'dir-fixture/inside.txt',
'fixture.txt',
];

await pipeline(
rs,
zlib.createGunzip(),
tarfs.extract(path.join(__dirname, 'dest-out'), {
mapStream(filestream, header) {
const expected = expectedNames.pop();
t.is(header.name, expected);
return filestream;
},
}),
);

done();
});
for (const directory of ['dest', 'dest-out']) {
rimrafSync(path.join(__dirname, directory));
}
};

const expectedNames = [
'dir-fixture/inside.txt',
'fixture.txt'
];
await pEvent(
gulp.src('fixture/**/*', {cwd: __dirname})
.pipe(gulpTar('archive.tar'))
.pipe(gulpZip())
.pipe(gulp.dest('dest', {cwd: __dirname})),
'finish',
);

gulp
.src('fixture/**/*', {cwd: __dirname})
.pipe(gulpTar('archive.tar'))
.pipe(gulpZip())
.pipe(gulp.dest('dest', {cwd: __dirname}))
.on('finish', onTargz);
await onTargz();
});
Loading

0 comments on commit 3adb58b

Please sign in to comment.