Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2f8b00c

Browse files
committed
add src license update script:
- add header info in constants - check src file header with falafel and glob - if header and license differ only by a date, update the header!
1 parent 473250a commit 2f8b00c

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@
8383
"browserify": "^12.0.1",
8484
"browserify-transform-tools": "^1.5.0",
8585
"ecstatic": "^1.2.0",
86+
"falafel": "^1.2.0",
87+
"glob": "^6.0.1",
8688
"jasmine-core": "^2.3.4",
8789
"jshint": "^2.8.0",
8890
"karma": "^0.13.15",

tasks/header.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
var path = require('path');
2+
var fs = require('fs');
3+
14
var prependFile = require('prepend-file');
5+
var falafel = require('falafel');
6+
var glob = require('glob');
27

38
var constants = require('./util/constants');
49

@@ -19,3 +24,54 @@ function headerLicense(path) {
1924
}
2025

2126
pathsDist.forEach(headerLicense);
27+
28+
29+
// add or update header to src files
30+
31+
// remove leading '/*' and trailing '*/' for comparison with falafel output
32+
var licenseSrc = constants.licenseSrc;
33+
var licenseStr = licenseSrc.substring(2, licenseSrc.length - 2);
34+
35+
36+
glob(path.join(constants.pathToSrc, '**/*.js'), function(err, files) {
37+
files.forEach(function(file) {
38+
fs.readFile(file, 'utf-8', function(err, code) {
39+
40+
// parse through code string while keeping track of comments
41+
var comments = [];
42+
falafel(code, {onComment: comments, locations: true}, function() {});
43+
44+
var header = comments[0];
45+
46+
// if header and license are the same, do nothing
47+
if(isCorrect(header)) return;
48+
49+
// if header and license only differ by date, update header
50+
else if(hasWrongDate(header)) {
51+
var codeLines = code.split('\n');
52+
53+
codeLines.splice(header.loc.start.line-1, header.loc.end.line);
54+
55+
var newCode = licenseSrc + '\n' + codeLines.join('\n');
56+
57+
fs.writeFile(file, newCode, function(err) {
58+
if(err) throw err;
59+
});
60+
}
61+
else {
62+
// otherwise, throw an error
63+
throw new Error(file + ' : has wrong header information.');
64+
}
65+
});
66+
});
67+
});
68+
69+
function isCorrect(header) {
70+
return (header.value === licenseStr);
71+
}
72+
73+
function hasWrongDate(header) {
74+
var regex = /Copyright 20[0-9][0-9]-20[0-9][0-9]/g;
75+
76+
return (header.value.replace(regex, '') === licenseStr.replace(regex, ''));
77+
}

tasks/util/constants.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,15 @@ module.exports = {
6464
'* All rights reserved.',
6565
'* Licensed under the MIT license',
6666
'*/'
67+
].join('\n'),
68+
69+
licenseSrc: [
70+
'/**',
71+
'* Copyright 2012-' + year + ', Plotly, Inc.',
72+
'* All rights reserved.',
73+
'*',
74+
'* This source code is licensed under the MIT license found in the',
75+
'* LICENSE file in the root directory of this source tree.',
76+
'*/'
6777
].join('\n')
6878
};

0 commit comments

Comments
 (0)