Skip to content

Commit

Permalink
Merge pull request #126 from wp-pot/streams
Browse files Browse the repository at this point in the history
Use streams instead of through2
  • Loading branch information
rasmusbe committed Oct 2, 2020
2 parents 71c9c8f + ff484b2 commit 37ca140
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 62 deletions.
55 changes: 29 additions & 26 deletions index.js
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
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
@@ -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
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

0 comments on commit 37ca140

Please sign in to comment.