forked from ExchangeBC/BCDevExchange-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
264 lines (219 loc) · 9.06 KB
/
gulpfile.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
Copyright 2015 Province of British Columbia
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and limitations under the License.
*/
'use strict';
var gulp = require('gulp');
var es = require('event-stream');
var md5 = require('MD5');
var ConsoleLogger = require('consologger');
var consoleLog = new ConsoleLogger();
var using = require('gulp-using');
var concat = require('gulp-concat');
var targetFile = ['!**/bower_components/**/', '!public/bootstrap/**/',
'!**/gulpfile.js', '!public/js/**/*.*',
'!**/TweenMax.js',
'client/**/*.html', 'client/**/*.js', 'server/**/*.js','config/**/*.json','*.js','e2e/**/*.spec.js'];
var licenseFile = 'copyright-apache2.txt';
var htmlFileSuffix = [];
htmlFileSuffix.push('.html');
htmlFileSuffix.push('.htm');
var jsFileSuffix = [];
jsFileSuffix.push('.js','.json');
(function(){
if (typeof String.prototype.endsWith !== 'function') {
String.prototype.endsWith = function(suffix) {
return this.indexOf(suffix, this.length - suffix.length) !== -1;
};
}
})();
function getArg(key) {
var index = process.argv.indexOf(key);
var next = process.argv[index + 1];
return (index < 0) ? null : (!next || next[0] === "-") ? true : next;
}
function isFileType(fileTypeArray, fileName){
var found = false;
for(var i = fileTypeArray.length -1; i >= 0 && !found; i--){
if(fileName.endsWith(fileTypeArray[i])){
found = true;
}
}
return found;
}
function isHtmlFile(filename){
return isFileType(htmlFileSuffix, filename);
}
function isJsFile(filename){
return isFileType(jsFileSuffix,filename);
}
function getCommentedLicenseTxt(licenseTxt, filename){
var result = "";
if(isHtmlFile(filename)){
result += '<!--';
result += '\n';
result += licenseTxt.trim();
result += '\n';
result += '-->';
result += '\n';
}else if(isJsFile(filename)){
result += '/*';
result += '\n';
result += licenseTxt.trim();
result += '\n';
result += '*/';
result += '\n';
}
return result;
}
// insert license at the top of the file
function insertLicense(originalFileText, filename){
var commentedLcTxt = getCommentedLicenseTxt(lcStat.licenseTxt,filename);
return commentedLcTxt +=originalFileText;
}
function lcStatFactory(){
var stat = {
licenseTxt: '',
lc_checksum: '',
numOfLines: 0
}
return stat;
}
var lcStat = lcStatFactory();
var doUpdate = '';
function selectiveUpdate(fsStream, callback){
if(doUpdate){
console.log("Insert license text to this file: " + fsStream.path + ".");
callback(null, fsStream);
}else{
callback();
}
}
gulp.task('get-args', function(){
doUpdate = getArg("--update")
console.log("--update param is: " + doUpdate);
});
gulp.task('license-stat', function(){
// set buffer to false so that data.contents will be a stream
return gulp.src(licenseFile, {buffer: false})
.pipe(es.map(function(data, cb){
cb(null,data.contents);
}))
.pipe(es.map(function(fsStream, callback){
fsStream.on('data', function(data){
var chunk = data.toString();
var len = chunk.split('\n').length;
lcStat.licenseTxt += chunk.trim();
lcStat.numOfLines = lcStat.numOfLines + len;
callback();
});
fsStream.on('end', function(){
lcStat.lc_checksum = md5(lcStat.licenseTxt.trim());
//console.log("license text checksum is: " + lcStat.lc_checksum);
//console.log("line count in license file: " + lcStat.numOfLines);
//console.log("license text: " + lcStat.licenseTxt);
callback(null, lcStat);
})
}))
;
});
gulp.task('default', ['license-stat', 'get-args'], function(){
return gulp.src(targetFile, {buffer:true})
.pipe(es.map(
function(data, callback){
// data is a stream of vinyl files https://github.com/wearefractal/vinyl
if(data.stat.isDirectory()){
console.log("Directory found: " + data.path);
callback(); // drop the data, not sending it down the pipe.
}else{
// passing the vinyl file right down the pipe
callback(null, data);
}
}
))
.pipe(es.map(function(fsStream, callback){
//var found = true;
var chunk = fsStream.contents.toString().trim();
var file_path = fsStream.path;
var regex_exp = null;
if(isHtmlFile(file_path)){
//regex_exp = /<!--((.|\n)^\[*?)-->.*/;
// we are looking for comment before html tag
regex_exp = /^<!--((.|\n|\r)*?)-->/;
}else if(isJsFile(file_path)) {
regex_exp = /^\s*\/\*((.|\n|\r)*?)\*\//;
}
if(regex_exp){
var match_array = regex_exp.exec(chunk);
if(match_array !=null && match_array.length>0){
var capturedLc = match_array[1].trim();
var cs = md5(capturedLc);
if(cs === lcStat.lc_checksum){
// This file already have a licensing term text that matches
// the content in the standard licences file.
consoleLog.white("Found standard license text this file: " + file_path).print() + ".";
callback();
}else{
consoleLog.red("Non-standard license text found in this file: " + file_path).print();
if(!doUpdate){
consoleLog.red.bold("Run with --update flag to update or insert license text to this file.").print();
console.log("----------------------------------------------------------------------------");
}
//consoleLog.red(capturedLc).print();
/*
The first comment section on top of the file does not match
with the standard license text. We will replace it with standard
license text.
Important condition:
The first comment section of top of the file must be standard license
text, otherwise it will be replaced.
*/
if(doUpdate){
//console.log("Updating this file with license text. " + file_path);
//console.log();
//console.log("Entire file content before replacement: ");
//console.log(chunk);
//console.log();
var newChunk = chunk.replace(regex_exp,
getCommentedLicenseTxt(lcStat.licenseTxt,file_path));
//console.log();
//console.log("Entire file content after replacement: ");
//console.log(newChunk);
//console.log();
fsStream.contents = new Buffer(newChunk);
}
selectiveUpdate(fsStream, callback);
}
}
else{
consoleLog.bold.red("License text not found in this file: " + file_path).print();
if(!doUpdate){
consoleLog.red.bold("Run with --update flag to update or insert license text to this file.").print();
console.log("----------------------------------------------------------------------------");
}
if(doUpdate){
//console.log("Adding license text to this file: " + file_path);
var newText = insertLicense(chunk, file_path);
fsStream.contents = new Buffer(newText);
}
selectiveUpdate(fsStream, callback);
}
}else{
// drop the stream if it is a file type we are currently not handling.
callback();
}
}))
.pipe(
gulp.dest(function(dataPack){
console.log("Writing to directory: " + dataPack.base);
return dataPack.base;
})
)
;
});