Skip to content

Commit ccd0a32

Browse files
committed
Require Node.js 18 and move to ESM
1 parent 1990751 commit ccd0a32

File tree

7 files changed

+213
-235
lines changed

7 files changed

+213
-235
lines changed

.github/workflows/main.yml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@ jobs:
1010
fail-fast: false
1111
matrix:
1212
node-version:
13-
- 14
14-
- 12
15-
- 10
16-
- 8
13+
- 20
14+
- 18
1715
steps:
18-
- uses: actions/checkout@v2
19-
- uses: actions/setup-node@v1
16+
- uses: actions/checkout@v4
17+
- uses: actions/setup-node@v4
2018
with:
2119
node-version: ${{ matrix.node-version }}
2220
- run: npm install

gulpfile.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
'use strict';
2-
const gulp = require('gulp');
3-
const zip = require('.');
1+
import gulp from 'gulp';
2+
import zip from './index.js';
43

5-
exports.default = () => (
6-
gulp.src('fixture/fixture.txt')
4+
export default function main() {
5+
return gulp.src('fixture/fixture.txt')
76
.pipe(zip('test.zip'))
8-
.pipe(gulp.dest('dest'))
9-
);
7+
.pipe(gulp.dest('dest'));
8+
}
9+

index.js

Lines changed: 29 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,39 @@
1-
'use strict';
2-
const path = require('path');
3-
const BufferConstants = require('buffer').constants;
4-
const Vinyl = require('vinyl');
5-
const PluginError = require('plugin-error');
6-
const through = require('through2');
7-
const Yazl = require('yazl');
8-
const getStream = require('get-stream');
1+
import path from 'node:path';
2+
import {constants as BufferConstants} from 'node:buffer';
3+
import Vinyl from 'vinyl';
4+
import Yazl from 'yazl';
5+
import {getStreamAsBuffer, MaxBufferError} from 'get-stream';
6+
import {gulpPlugin} from 'gulp-plugin-extras';
97

10-
module.exports = (filename, options) => {
8+
export default function gulpZip(filename, options) {
119
if (!filename) {
12-
throw new PluginError('gulp-zip', '`filename` required');
10+
throw new Error('gulp-zip: `filename` required');
1311
}
1412

1513
options = {
1614
compress: true,
1715
buffer: true,
18-
...options
16+
...options,
1917
};
2018

2119
let firstFile;
2220
const zip = new Yazl.ZipFile();
2321

24-
return through.obj((file, encoding, callback) => {
22+
return gulpPlugin('gulp-zip', async file => {
2523
if (!firstFile) {
2624
firstFile = file;
2725
}
2826

2927
// Because Windows...
30-
const pathname = file.relative.replace(/\\/g, '/');
28+
const pathname = file.relative.replaceAll('\\', '/');
3129

3230
if (!pathname) {
33-
callback();
3431
return;
3532
}
3633

37-
if (file.isNull() && file.stat && file.stat.isDirectory && file.stat.isDirectory()) {
34+
if (file.isDirectory()) {
3835
zip.addEmptyDirectory(pathname, {
39-
mtime: options.modifiedTime || file.stat.mtime || new Date()
36+
mtime: options.modifiedTime || file.stat.mtime || new Date(),
4037
// Do *not* pass a mode for a directory, because it creates platform-dependent
4138
// ZIP files (ZIP files created on Windows that cannot be opened on macOS).
4239
// Re-enable if this PR is resolved: https://github.com/thejoshwolfe/yazl/pull/59
@@ -46,7 +43,7 @@ module.exports = (filename, options) => {
4643
const stat = {
4744
compress: options.compress,
4845
mtime: options.modifiedTime || (file.stat ? file.stat.mtime : new Date()),
49-
mode: file.stat ? file.stat.mode : null
46+
mode: file.stat ? file.stat.mode : null,
5047
};
5148

5249
if (file.isStream()) {
@@ -57,42 +54,33 @@ module.exports = (filename, options) => {
5754
zip.addBuffer(file.contents, pathname, stat);
5855
}
5956
}
57+
}, {
58+
supportsAnyType: true,
59+
async * onFinish() {
60+
zip.end();
6061

61-
callback();
62-
}, function (callback) {
63-
if (!firstFile) {
64-
callback();
65-
return;
66-
}
62+
if (!firstFile) {
63+
return;
64+
}
6765

68-
(async () => {
6966
let data;
7067
if (options.buffer) {
7168
try {
72-
data = await getStream.buffer(zip.outputStream, {maxBuffer: BufferConstants.MAX_LENGTH});
69+
data = await getStreamAsBuffer(zip.outputStream, {maxBuffer: BufferConstants.MAX_LENGTH});
7370
} catch (error) {
74-
if (error instanceof getStream.MaxBufferError) {
75-
callback(new PluginError('gulp-zip', 'The output ZIP file is too big to store in a buffer (larger than Buffer MAX_LENGTH). To output a stream instead, set the gulp-zip buffer option to `false`.'));
76-
} else {
77-
callback(error);
78-
}
79-
80-
return;
71+
const error_ = error instanceof MaxBufferError ? new Error('The output ZIP file is too big to store in a buffer (larger than Buffer MAX_LENGTH). To output a stream instead, set the gulp-zip buffer option to `false`.') : error;
72+
throw error_;
8173
}
8274
} else {
8375
data = zip.outputStream;
8476
}
8577

86-
this.push(new Vinyl({
78+
yield new Vinyl({
8779
cwd: firstFile.cwd,
8880
base: firstFile.base,
8981
path: path.join(firstFile.base, filename),
90-
contents: data
91-
}));
92-
93-
callback();
94-
})();
95-
96-
zip.end();
82+
contents: data,
83+
});
84+
},
9785
});
98-
};
86+
}

license

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
3+
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
44

55
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:
66

package.json

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
"description": "ZIP compress files",
55
"license": "MIT",
66
"repository": "sindresorhus/gulp-zip",
7+
"funding": "https://github.com/sponsors/sindresorhus",
78
"author": {
89
"name": "Sindre Sorhus",
910
"email": "sindresorhus@gmail.com",
10-
"url": "sindresorhus.com"
11+
"url": "https://sindresorhus.com"
1112
},
13+
"type": "module",
14+
"exports": "./index.js",
1215
"engines": {
13-
"node": ">=8"
16+
"node": ">=18"
1417
},
1518
"scripts": {
1619
"test": "xo && ava"
@@ -28,19 +31,19 @@
2831
"file"
2932
],
3033
"dependencies": {
31-
"get-stream": "^5.2.0",
32-
"plugin-error": "^1.0.1",
33-
"through2": "^3.0.1",
34-
"vinyl": "^2.1.0",
34+
"get-stream": "^8.0.1",
35+
"gulp-plugin-extras": "^0.3.0",
36+
"vinyl": "^3.0.0",
3537
"yazl": "^2.5.1"
3638
},
3739
"devDependencies": {
38-
"ava": "^2.3.0",
40+
"ava": "^5.3.1",
3941
"decompress-unzip": "^3.0.0",
42+
"easy-transform-stream": "^1.0.1",
4043
"gulp": "^4.0.2",
4144
"vinyl-assign": "^1.2.1",
42-
"vinyl-file": "^3.0.0",
43-
"xo": "^0.24.0"
45+
"vinyl-file": "^5.0.0",
46+
"xo": "^0.56.0"
4447
},
4548
"peerDependencies": {
4649
"gulp": ">=4"

readme.md

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,25 @@
22

33
> ZIP compress files
44
5-
65
## Install
76

7+
```sh
8+
npm install --save-dev gulp-zip
89
```
9-
$ npm install --save-dev gulp-zip
10-
```
11-
1210

1311
## Usage
1412

1513
```js
16-
const gulp = require('gulp');
17-
const zip = require('gulp-zip');
14+
import gulp from 'gulp';
15+
import zip from 'gulp-zip';
1816

19-
exports.default = () => (
17+
export default () => (
2018
gulp.src('src/*')
2119
.pipe(zip('archive.zip'))
2220
.pipe(gulp.dest('dist'))
2321
);
2422
```
2523

26-
2724
## API
2825

2926
Supports [streaming mode](https://github.com/gulpjs/gulp/blob/master/docs/API.md#optionsbuffer).
@@ -40,12 +37,12 @@ Type: `object`
4037

4138
##### compress
4239

43-
Type: `boolean`<br>
40+
Type: `boolean`\
4441
Default: `true`
4542

4643
##### modifiedTime
4744

48-
Type: `Date`<br>
45+
Type: `Date`\
4946
Default: `undefined`
5047

5148
Overrides the modification timestamp for all files added to the archive.
@@ -54,10 +51,11 @@ Tip: Setting it to the same value across executions enables you to create stable
5451

5552
##### buffer
5653

57-
Type: `boolean`<br>
54+
Type: `boolean`\
5855
Default: `true`
5956

6057
If `true`, the resulting ZIP file contents will be a buffer. Large zip files may not be possible to buffer, depending on the size of [Buffer MAX_LENGTH](https://nodejs.org/api/buffer.html#buffer_buffer_constants_max_length).
58+
6159
If `false`, the ZIP file contents will be a stream.
6260

6361
We use this option instead of relying on [gulp.src's `buffer` option](https://gulpjs.com/docs/en/api/src/#options) because we are mapping many input files to one output file and can't reliably detect what the output mode should be based on the inputs, since Vinyl streams could contain mixed streaming and buffered content.

0 commit comments

Comments
 (0)