forked from matteckert/gulp-jison
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
33 lines (28 loc) · 1 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
var through = require('through2');
var gutil = require('gulp-util');
var PluginError = gutil.PluginError;
var Generator = require('jison').Generator;
const PLUGIN_NAME = 'gulp-jison';
module.exports = function (options) {
options = options || {};
return through.obj(function (file, enc, callback) {
if (file.isNull()) {
this.push(file);
return callback();
}
if (file.isStream()) {
this.emit('error', new PluginError(PLUGIN_NAME, 'Streams not supported'));
return callback();
}
if (file.isBuffer()) {
try {
file.contents = new Buffer(new Generator(file.contents.toString(), options).generate());
file.path = gutil.replaceExtension(file.path, ".js");
this.push(file);
} catch (error) {
this.emit('error', new PluginError(PLUGIN_NAME, error));
}
return callback();
}
});
};