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 1990751 commit ccd0a32
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 235 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
14 changes: 7 additions & 7 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';
const gulp = require('gulp');
const zip = require('.');
import gulp from 'gulp';
import zip from './index.js';

exports.default = () => (
gulp.src('fixture/fixture.txt')
export default function main() {
return gulp.src('fixture/fixture.txt')
.pipe(zip('test.zip'))
.pipe(gulp.dest('dest'))
);
.pipe(gulp.dest('dest'));
}

70 changes: 29 additions & 41 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,39 @@
'use strict';
const path = require('path');
const BufferConstants = require('buffer').constants;
const Vinyl = require('vinyl');
const PluginError = require('plugin-error');
const through = require('through2');
const Yazl = require('yazl');
const getStream = require('get-stream');
import path from 'node:path';
import {constants as BufferConstants} from 'node:buffer';
import Vinyl from 'vinyl';
import Yazl from 'yazl';
import {getStreamAsBuffer, MaxBufferError} from 'get-stream';
import {gulpPlugin} from 'gulp-plugin-extras';

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

options = {
compress: true,
buffer: true,
...options
...options,
};

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

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

// Because Windows...
const pathname = file.relative.replace(/\\/g, '/');
const pathname = file.relative.replaceAll('\\', '/');

if (!pathname) {
callback();
return;
}

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

if (file.isStream()) {
Expand All @@ -57,42 +54,33 @@ module.exports = (filename, options) => {
zip.addBuffer(file.contents, pathname, stat);
}
}
}, {
supportsAnyType: true,
async * onFinish() {
zip.end();

callback();
}, function (callback) {
if (!firstFile) {
callback();
return;
}
if (!firstFile) {
return;
}

(async () => {
let data;
if (options.buffer) {
try {
data = await getStream.buffer(zip.outputStream, {maxBuffer: BufferConstants.MAX_LENGTH});
data = await getStreamAsBuffer(zip.outputStream, {maxBuffer: BufferConstants.MAX_LENGTH});
} catch (error) {
if (error instanceof getStream.MaxBufferError) {
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`.'));
} else {
callback(error);
}

return;
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;
throw error_;
}
} else {
data = zip.outputStream;
}

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

callback();
})();

zip.end();
contents: data,
});
},
});
};
}
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
21 changes: 12 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
"description": "ZIP compress files",
"license": "MIT",
"repository": "sindresorhus/gulp-zip",
"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 && ava"
Expand All @@ -28,19 +31,19 @@
"file"
],
"dependencies": {
"get-stream": "^5.2.0",
"plugin-error": "^1.0.1",
"through2": "^3.0.1",
"vinyl": "^2.1.0",
"get-stream": "^8.0.1",
"gulp-plugin-extras": "^0.3.0",
"vinyl": "^3.0.0",
"yazl": "^2.5.1"
},
"devDependencies": {
"ava": "^2.3.0",
"ava": "^5.3.1",
"decompress-unzip": "^3.0.0",
"easy-transform-stream": "^1.0.1",
"gulp": "^4.0.2",
"vinyl-assign": "^1.2.1",
"vinyl-file": "^3.0.0",
"xo": "^0.24.0"
"vinyl-file": "^5.0.0",
"xo": "^0.56.0"
},
"peerDependencies": {
"gulp": ">=4"
Expand Down
20 changes: 9 additions & 11 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,25 @@

> ZIP compress files

## Install

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


## Usage

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

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


## API

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

##### compress

Type: `boolean`<br>
Type: `boolean`\
Default: `true`

##### modifiedTime

Type: `Date`<br>
Type: `Date`\
Default: `undefined`

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

##### buffer

Type: `boolean`<br>
Type: `boolean`\
Default: `true`

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).

If `false`, the ZIP file contents will be a stream.

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.
Loading

0 comments on commit ccd0a32

Please sign in to comment.