forked from wp-pot/gulp-wp-pot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·69 lines (57 loc) · 1.36 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
/* eslint-env node */
/** global: Buffer */
'use strict';
const Vinyl = require('vinyl');
const through = require('through2');
const wpPot = require('wp-pot');
const PluginError = require('plugin-error');
/**
* 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 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 = {};
}
options.src = files;
options.writeFile = false;
try {
const potContents = wpPot(options);
const potFile = new Vinyl({
contents: Buffer.from(potContents),
path: '.'
});
this.push(potFile);
this.emit('end');
cb();
} catch (error) {
this.emit('error', new PluginError('gulp-wp-pot', error));
}
});
return stream;
}
module.exports = gulpWPpot;