Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use streams instead of through2 #126

Merged
merged 1 commit into from
Oct 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 29 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
35 changes: 1 addition & 34 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
},
"dependencies": {
"plugin-error": "^1.0.1",
"through2": "^4.0.2",
"vinyl": "^2.2.1",
"wp-pot": "^1.9.6"
}
Expand Down
15 changes: 15 additions & 0 deletions test/fixtures/valid-functions-copy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
__('Return string', 'testdomain');
_e('Print string', 'testdomain');
esc_attr__('Escape for attribute and return string', 'testdomain');
esc_attr_e('Escape for attribute and print string', 'testdomain');
esc_html__('Escape for html and return string', 'testdomain');
esc_html_e('Escape for html and print string', 'testdomain');
_x('Return string with context', 'Some context', 'testdomain');
_ex('Print string with context', 'Some context', 'testdomain');
esc_attr_x('Escape string with context for attribute', 'Some context', 'testdomain');
esc_html_x('Escape string with context for html', 'Some context', 'testdomain');
_n('Singular string', 'Plural string', 2, 'testdomain');
_n_noop('Singular string with noop', 'Plural string with noop', 'testdomain');
_nx('Singular string with context', 'Plural string with context', 2, 'Some context', 'testdomain');
_nx_noop('Singular string with noop and context', 'Plural string with noop and context', 'Some context', 'testdomain');
9 changes: 8 additions & 1 deletion test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,19 @@ describe('File write tests', function () {

it('should read a file correctly', function (done) {
const fixturePath = 'test/fixtures/valid-functions.php';
const fixturePath2 = 'test/fixtures/valid-functions-copy.php';

const testFile = new Vinyl({
path: fixturePath,
contents: fs.readFileSync(fixturePath)
});

es.readArray([testFile])
const testFile2 = new Vinyl({
path: fixturePath2,
contents: fs.readFileSync(fixturePath2)
});

es.readArray([testFile, testFile2])
.pipe(gulpWpPot({
src: fixturePath
}))
Expand All @@ -46,6 +52,7 @@ describe('File write tests', function () {
})
.on('data', function (file) {
const potContents = file.contents.toString();

testHelper.testValidFunctions(potContents, fixturePath);
done();
});
Expand Down