-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathheader_src.js
63 lines (49 loc) · 2.2 KB
/
header_src.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
var path = require('path');
var fs = require('fs');
var falafel = require('falafel');
var glob = require('glob');
var constants = require('./util/constants');
var common = require('./util/common');
(function updateHeadersInSrcAndLibFiles() {
var srcGlob = path.join(constants.pathToSrc, '**/*.js');
var libGlob = path.join(constants.pathToLib, '**/*.js');
// remove leading '/*' and trailing '*/' for comparison with falafel output
var licenseSrc = constants.licenseSrc;
var licenseStr = licenseSrc.substring(2, licenseSrc.length - 2);
glob('{' + srcGlob + ',' + libGlob + '}', function(err, files) {
files.forEach(function(file) {
fs.readFile(file, 'utf-8', function(err, code) {
// parse through code string while keeping track of comments
var comments = [];
falafel(code, {onComment: comments, locations: true}, function() {});
var header = comments[0];
// error out if no header is found
if(!header || header.loc.start.line > 1) {
throw new Error(file + ' : has no header information.');
}
// if header and license are the same, do nothing
if(isCorrect(header)) return;
// if header and license only differ by date, update header
else if(hasWrongDate(header)) {
var codeLines = code.split('\n');
codeLines.splice(header.loc.start.line - 1, header.loc.end.line);
var newCode = licenseSrc + '\n' + codeLines.join('\n');
common.writeFile(file, newCode);
} else {
// otherwise, throw an error
throw new Error(file + ' : has wrong header information.');
}
});
});
});
function isCorrect(header) {
return (
header.value.replace(/\s+$/gm, '') ===
licenseStr.replace(/\s+$/gm, '')
);
}
function hasWrongDate(header) {
var regex = /Copyright 20[0-9][0-9]-20[0-9][0-9]/g;
return (header.value.replace(regex, '') === licenseStr.replace(regex, ''));
}
})();