-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
41 lines (34 loc) · 937 Bytes
/
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
const through = require( 'through2' ),
PluginError = require( 'plugin-error' );
module.exports = function ( opt ) {
return through.obj( function ( file, enc, cb ) {
if ( file.isNull() ) {
cb( null, file );
return;
}
if ( file.isStream() ) {
cb( new PluginError( 'gulp-remove-duplicate-lines', 'Streaming not supported' ) );
return;
}
let str = file.contents.toString( enc );
const lines = str.split( /\r\n|\r|\n/g );
const newLines = lines.reduce( ( newLines, line ) => {
if ( opt.exclude && line.match( opt.exclude ) ) {
newLines.push( line );
return newLines;
}
if ( opt.include && ! line.match( opt.include ) ) {
newLines.push( line );
return newLines;
}
if ( line && ! newLines.includes( line ) ) {
newLines.push( line );
}
return newLines;
}, [] )
str = newLines.join( '\n' );
file.contents = Buffer.from( str );
this.push( file );
cb();
} );
};