Total Complexity | 8 |
Complexity/F | 2 |
Lines of Code | 70 |
Function Count | 4 |
Duplicated Lines | 0 |
Ratio | 0 % |
Coverage | 100% |
Changes | 0 |
1 | /* eslint-env node */ |
||
3 | 'use strict'; |
||
4 | |||
5 | 1 | const Vinyl = require('vinyl'); |
|
6 | 1 | const wpPot = require('wp-pot'); |
|
7 | 1 | const PluginError = require('plugin-error'); |
|
8 | 1 | const { Transform } = require('stream'); |
|
9 | |||
10 | /** |
||
11 | * Determine if `obj` is a object or not. |
||
12 | * |
||
13 | * @param {object} obj |
||
14 | * |
||
15 | * @return {boolean} |
||
16 | */ |
||
17 | function isObject (obj) { |
||
18 | 2 | return Object.prototype.toString.call(obj) === '[object Object]'; |
|
19 | } |
||
20 | |||
21 | /** |
||
22 | * Run the wp pot generator. |
||
23 | * |
||
24 | * @param {object} options |
||
25 | * |
||
26 | * @return {object} |
||
27 | */ |
||
28 | function gulpWPpot (options) { |
||
29 | 5 | if (options !== undefined && !isObject(options)) { |
|
30 | 1 | throw new PluginError('gulp-wp-pot', 'Require a argument of type object.'); |
|
31 | } |
||
32 | |||
33 | 4 | const files = []; |
|
34 | |||
35 | 4 | const transformer = new Transform({ |
|
36 | objectMode: true, |
||
37 | transform (file, encoding, done) { |
||
38 | 5 | if (file.isStream()) { |
|
39 | 1 | done('error', new PluginError('gulp-wp-pot', 'Streams are not supported.')); |
|
40 | 1 | return; |
|
41 | } |
||
42 | |||
43 | 4 | files.push(file.path); |
|
44 | 4 | done(); |
|
45 | }, |
||
46 | flush (done) { |
||
47 | 3 | if (!options) { |
|
48 | 2 | options = {}; |
|
49 | } |
||
50 | |||
51 | 3 | options.src = files; |
|
52 | 3 | options.writeFile = false; |
|
53 | |||
54 | 3 | try { |
|
55 | 3 | const potContents = wpPot(options); |
|
56 | 2 | const potFile = new Vinyl({ |
|
57 | contents: Buffer.from(potContents), |
||
58 | path: '.' |
||
59 | }); |
||
60 | |||
61 | 2 | this.push(potFile); |
|
62 | 2 | done(); |
|
63 | } catch (error) { |
||
64 | 1 | done('error', new PluginError('gulp-wp-pot', error)); |
|
65 | } |
||
66 | } |
||
67 | }); |
||
68 | |||
69 | 4 | return transformer; |
|
70 | } |
||
71 | |||
72 | module.exports = gulpWPpot; |
||
73 |