From ff484b2013837a27c858dcc7a3ba31f930726b04 Mon Sep 17 00:00:00 2001 From: Rasmus Bengtsson <2124386+rasmusbe@users.noreply.github.com> Date: Fri, 2 Oct 2020 09:44:07 +0200 Subject: [PATCH] Use streams instead of through2 --- index.js | 55 ++++++++++++++------------ package-lock.json | 35 +--------------- package.json | 1 - test/fixtures/valid-functions-copy.php | 15 +++++++ test/tests.js | 9 ++++- 5 files changed, 53 insertions(+), 62 deletions(-) create mode 100644 test/fixtures/valid-functions-copy.php diff --git a/index.js b/index.js index 6c9d36c..7559280 100755 --- a/index.js +++ b/index.js @@ -3,9 +3,9 @@ 'use strict'; const Vinyl = require('vinyl'); -const through = require('through2'); const wpPot = require('wp-pot'); const PluginError = require('plugin-error'); +const { Transform } = require('stream'); /** * Determine if `obj` is a object or not. @@ -32,38 +32,41 @@ function gulpWPpot (options) { const files = []; - const stream = through.obj(function (file, enc, cb) { - if (file.isStream()) { - this.emit('error', new PluginError('gulp-wp-pot', 'Streams are not supported.')); - } - - files.push(file.path); - cb(); - }, function (cb) { - if (!options) { - options = {}; - } + const transformer = new Transform({ + objectMode: true, + transform (file, encoding, done) { + if (file.isStream()) { + done('error', new PluginError('gulp-wp-pot', 'Streams are not supported.')); + return; + } - options.src = files; - options.writeFile = false; + files.push(file.path); + done(); + }, + flush (done) { + if (!options) { + options = {}; + } - try { - const potContents = wpPot(options); - const potFile = new Vinyl({ - contents: Buffer.from(potContents), - path: '.' - }); + options.src = files; + options.writeFile = false; - this.push(potFile); - this.emit('end'); + try { + const potContents = wpPot(options); + const potFile = new Vinyl({ + contents: Buffer.from(potContents), + path: '.' + }); - cb(); - } catch (error) { - this.emit('error', new PluginError('gulp-wp-pot', error)); + this.push(potFile); + done(); + } catch (error) { + done('error', new PluginError('gulp-wp-pot', error)); + } } }); - return stream; + return transformer; } module.exports = gulpWPpot; diff --git a/package-lock.json b/package-lock.json index 6e89fea..53fc04c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -459,7 +459,7 @@ }, "ansi-colors": { "version": "1.1.0", - "resolved": "https://registry.npmjs.org/ansi-colors/-/ansi-colors-1.1.0.tgz", + "resolved": "http://registry.npmjs.org/ansi-colors/-/ansi-colors-1.1.0.tgz", "integrity": "sha512-SFKX67auSNoVR38N3L+nvsPjOE0bybKTYbkf5tRvushrAPQ9V75huw0ZxBkKVeRU9kqH3d6HA4xTckbwZ4ixmA==", "requires": { "ansi-wrap": "^0.1.0" @@ -2942,16 +2942,6 @@ "read-pkg": "^2.0.0" } }, - "readable-stream": { - "version": "3.6.0", - "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.0.tgz", - "integrity": "sha512-BViHy7LKeTz4oNnkcLJ+lVSL6vpiFeX6/d3oSH8zCW7UxP2onchk+vTGB143xuFjHS3deTgkKoXXymXqymiIdA==", - "requires": { - "inherits": "^2.0.3", - "string_decoder": "^1.1.1", - "util-deprecate": "^1.0.1" - } - }, "readdirp": { "version": "3.4.0", "resolved": "https://registry.npmjs.org/readdirp/-/readdirp-3.4.0.tgz", @@ -3240,21 +3230,6 @@ "es-abstract": "^1.17.5" } }, - "string_decoder": { - "version": "1.3.0", - "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz", - "integrity": "sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA==", - "requires": { - "safe-buffer": "~5.2.0" - }, - "dependencies": { - "safe-buffer": { - "version": "5.2.1", - "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", - "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==" - } - } - }, "strip-ansi": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.0.tgz", @@ -3376,14 +3351,6 @@ "integrity": "sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU=", "dev": true }, - "through2": { - "version": "4.0.2", - "resolved": "https://registry.npmjs.org/through2/-/through2-4.0.2.tgz", - "integrity": "sha512-iOqSav00cVxEEICeD7TjLB1sueEL+81Wpzp2bY17uZjZN0pWZPuo4suZ/61VujxmqSGFfgOcNuTZ85QJwNZQpw==", - "requires": { - "readable-stream": "3" - } - }, "to-fast-properties": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/to-fast-properties/-/to-fast-properties-2.0.0.tgz", diff --git a/package.json b/package.json index b6fbd51..ac286a9 100644 --- a/package.json +++ b/package.json @@ -55,7 +55,6 @@ }, "dependencies": { "plugin-error": "^1.0.1", - "through2": "^4.0.2", "vinyl": "^2.2.1", "wp-pot": "^1.9.6" } diff --git a/test/fixtures/valid-functions-copy.php b/test/fixtures/valid-functions-copy.php new file mode 100644 index 0000000..f0df0fa --- /dev/null +++ b/test/fixtures/valid-functions-copy.php @@ -0,0 +1,15 @@ +