This repository has been archived by the owner on Mar 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
70 lines (62 loc) · 2.01 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
'use strict';
var fs = require('fs');
var path = require('path');
var through = require('through2');
var gutil = require('gulp-util');
var _ = require('lodash');
var KeysetManager = require('./lib/keyset-manager');
var PluginError = gutil.PluginError;
var pluginName = path.basename(__dirname);
/**
* BEM i18n gulp plugin
*
* @param {Object} options options for plugin
* @param {String[]} options.langs list of supported languages
* @param {Function} options.postProcessor function for process keysets after merge
*
* @returns {Stream}
*/
module.exports = function (options) {
options = options || {};
var keysetManager = null;
return through.obj(function (folder, encoding, callback) {
var _this = this;
keysetManager = new KeysetManager(options.langs);
if(folder.isStream()) {
return callback(new PluginError(pluginName, 'Stream not supported'));
}
if(!options.langs) {
return callback(new PluginError(pluginName, 'Please specify languages'));
}
keysetManager.addFolder(folder)
.always(function() {
_this.resume();
})
.then(function() {
callback();
})
.catch(function(err) {
callback(new PluginError(pluginName, err));
});
_this.pause();
}, function (callback) {
var _this = this;
keysetManager.getMergedKeysets()
.always(function() {
_this.resume();
})
.then(function (mergedKeysets) {
var postProcessor = options.postProcessor;
if (postProcessor && _.isFunction(postProcessor)) {
this.push(postProcessor(mergedKeysets));
return;
}
this.push(mergedKeysets);
callback();
})
.catch(function(err) {
callback(new PluginError(pluginName, err));
});
_this.pause();
});
};