-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
executable file
·72 lines (61 loc) · 1.45 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* eslint-env node */
/** global: Buffer */
'use strict';
const Vinyl = require('vinyl');
const wpPot = require('wp-pot');
const PluginError = require('plugin-error');
const { Transform } = require('stream');
/**
* Determine if `obj` is a object or not.
*
* @param {object} obj
*
* @return {boolean}
*/
function isObject (obj) {
return Object.prototype.toString.call(obj) === '[object Object]';
}
/**
* Run the wp pot generator.
*
* @param {object} options
*
* @return {object}
*/
function gulpWPpot (options) {
if (options !== undefined && !isObject(options)) {
throw new PluginError('gulp-wp-pot', 'Require a argument of type object.');
}
const files = [];
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;
}
files.push(file.path);
done();
},
flush (done) {
if (!options) {
options = {};
}
options.src = files;
options.writeFile = false;
try {
const potContents = wpPot(options);
const potFile = new Vinyl({
contents: Buffer.from(potContents),
path: '.'
});
this.push(potFile);
done();
} catch (error) {
done('error', new PluginError('gulp-wp-pot', error));
}
}
});
return transformer;
}
module.exports = gulpWPpot;