-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
236 lines (211 loc) · 7.7 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/**
* Copyright (c) 2015-2016, fangstar.com
*
* All rights reserved.
*/
/**
* @file gulp plugin for header license.
*
* @author <a href="mailto:liliyuan@fangstar.net">Liyuan Li</a>
* @version 0.1.5.1, Nov 21, 2016
*/
'use strict';
var extend = require('object-assign');
var through = require('through2');
var lodashTemplate = require('lodash.template');
var path = require('path');
/**
* gulp-license plugin.
* @param {string} license The license template string.
* @param {object} [config] The JSON object used to populate the license template.
* @param {float} [rate=0.8] Matching rate.
* @returns {oject} Gulp extension in the pipline.
*/
module.exports = function (license, config, rate) {
/**
* According to rate, get matching.
*
* @param {object} file nodeJS file object.
* @param {string} license The license template string.
* @param {float} rate Matching rate.
* @param {string} srcNLReg new line char code.
* @returns {boolean} dose match.
*/
function isMatch(file, license, rate, srcNLReg) {
var srcLines = file.contents.toString('utf8').split(/\r?\n/),
templateLines = license.split(/\r?\n/),
type = path.extname(file.path),
matchRates = 0,
removed = false;
// after '<?php' has new line, remove it
switch (type) {
case '.php':
if (srcLines[1].replace(/\s/, '') === '') {
srcLines.splice(1, 1);
removed = true;
}
break;
default:
break;
}
// count match line
var minLength = templateLines.length > srcLines.length ? srcLines.length : templateLines.length;
for (var i = 0; i < minLength; i++) {
if (templateLines[templateLines.length - 1] === '' && i === templateLines.length - 1) {
matchRates += 1;
} else {
switch (type) {
case '.php':
matchRates += getMatchRate(srcLines[i + 1], templateLines[i]);
break;
default:
matchRates += getMatchRate(srcLines[i], templateLines[i]);
break;
}
}
}
// has similar license, remove the license.
var matchPer = matchRates / templateLines.length;
if (matchPer >= rate && matchPer < 1) {
// remove
switch (type) {
case '.php':
if (srcLines[templateLines.length].replace(/\s/, '') === '' && !removed) {
// after license, should be have a blank line. if has not, we don't need remove blank line. && after '<?php' has not new line
srcLines.splice(1, templateLines.length);
} else {
srcLines.splice(1, templateLines.length - 1);
}
file.contents = new Buffer(srcLines.join(srcNLReg));
break;
default:
// after license, should be have a blank line. if have not, we don't need remove blank line.
if (srcLines[templateLines.length - 1].replace(/\s/, '') === '') {
srcLines.splice(0, templateLines.length);
} else {
srcLines.splice(0, templateLines.length - 1);
}
file.contents = new Buffer(srcLines.join(srcNLReg));
break;
}
return false;
} else if (matchPer === 1) {
return true;
}
}
/**
* Compare each character for ever line, and get ever line match rate.
*
* @param {type} src text for template.
* @param {type} str text for file.
* @returns {float} match rate.
*/
function getMatchRate(src, str) {
if (typeof (src) === 'undefined' || typeof (str) === 'undefined') {
return 0;
}
var maxLength = src.length > str.length ? src.length : str.length,
matchCnt = 0;
if (maxLength === 0) {
return 1;
}
for (var i = 0; i < maxLength; i++) {
if (str.charAt(i) === src.charAt(i)) {
matchCnt++;
}
}
if (matchCnt === 0) {
return 0;
}
return matchCnt / maxLength;
}
/**
* Test first newline character and get newline character.
*
* @param {type} str file content.
* @returns {String} newline character.
*/
function getSeparator(str, str2) {
// 13 \r 10 \n
for (var i = str.length; i > -1; i--) {
if (str.charCodeAt(i) === 10) {
if (str.charCodeAt(i - 1) === 13) {
return '\r\n';
}
if (str.charCodeAt(i + 1) === 13) {
return '\n\r';
}
return '\n';
}
if (str.charCodeAt(i) === 13) {
if (str.charCodeAt(i - 1) === 10) {
return '\n\r';
}
if (str.charCodeAt(i + 1) === 10) {
return '\r\n';
}
return '\r';
}
}
// if file no newline, will use template
for (var i = str2.length; i > -1; i--) {
if (str2.charCodeAt(i) === 10) {
if (str2.charCodeAt(i - 1) === 13) {
return '\r\n';
}
if (str2.charCodeAt(i + 1) === 13) {
return '\n\r';
}
return '\n';
}
if (str2.charCodeAt(i) === 13) {
if (str2.charCodeAt(i - 1) === 10) {
return '\n\r';
}
if (str2.charCodeAt(i + 1) === 10) {
return '\r\n';
}
return '\r';
}
}
}
return through.obj(function (file, enc, cb) {
license = license || '';
rate = rate || 0.8;
// rate must be [0-1].
if (rate > 1 || rate <= 0) {
rate = 0.8;
}
// merge template & config data
var template = config === false ? license : lodashTemplate(license)(extend({
file: ''
}, config));
var srcNLReg = getSeparator(file.contents.toString('utf8'), template);
// new line char code must match file new line char code.
// when template use windows new line char code, but file use unix new line char code, it will show '^m' in template.
template = template.split(/\r?\n/).join(srcNLReg);
if (!isMatch(file, template, rate, srcNLReg)) {
// add Template
var type = path.extname(file.path);
switch (type) {
case '.php':
var srcLines = file.contents.toString('utf8').split(/\r?\n/);
if (srcLines[1] === '') {
// if after '<?php' has blank line, need to remove this line.
srcLines.splice(1, 1, template);
} else {
srcLines.splice(1, 0, template);
}
file.contents = new Buffer(srcLines.join(srcNLReg), 'utf8');
break;
default:
file.contents = new Buffer(template + srcNLReg + file.contents, 'utf8');
break;
}
}
// make sure the file goes through the next gulp plugin
this.push(file);
// tell the stream engine that we are done with this file
cb();
});
};