-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
50 lines (44 loc) · 1.12 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
"use strict";
var gutil = require("gulp-util");
var iconv = require("iconv-lite");
var through = require("through2");
var convertNewline = require("convert-newline");
var PACKAGE_NAME = "gulp-convert-newline";
module.exports = function(options) {
options = options || {};
var newline = options.newline || "lf";
var encoding = options.encoding;
var converter = convertNewline(newline, encoding);
return through.obj(function(file, enc, cb) {
if (file.isNull()) {
this.push(file);
cb();
return;
}
if (file.isStream()) {
try {
if (encoding) {
file.contents = file.contents
.pipe(iconv.decodeStream(encoding))
.pipe(converter.stream())
.pipe(iconv.encodeStream(encoding));
} else {
file.contents = file.contents
.pipe(converter.stream());
}
this.push(file);
} catch (err) {
this.emit("error", new gutil.PluginError(PACKAGE_NAME, err));
}
}
if (file.isBuffer()) {
try {
file.contents = converter.buffer()(file.contents);
this.push(file);
} catch (err) {
this.emit("error", new gutil.PluginError(PACKAGE_NAME, err));
}
}
cb();
});
};