-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathheader.js
90 lines (66 loc) · 2.69 KB
/
header.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
var path = require('path');
var fs = require('fs');
var prependFile = require('prepend-file');
var falafel = require('falafel');
var glob = require('glob');
var constants = require('./util/constants');
// add headers to dist files
var pathsDist = [
constants.pathToPlotlyDistMin,
constants.pathToPlotlyDist,
constants.pathToPlotlyDistWithMeta,
constants.pathToPlotlyGeoAssetsDist
];
constants.partialBundleNames.forEach(function(name) {
var pathToBundle = path.join(constants.pathToDist, 'plotly-' + name + '.js'),
pathToMinBundle = path.join(constants.pathToDist, 'plotly-' + name + '.min.js');
pathsDist.push(pathToBundle, pathToMinBundle);
});
function headerLicense(path) {
prependFile(path, constants.licenseDist + '\n', function(err) {
if(err) throw err;
});
}
pathsDist.forEach(headerLicense);
// add or update header to src files
// remove leading '/*' and trailing '*/' for comparison with falafel output
var licenseSrc = constants.licenseSrc;
var licenseStr = licenseSrc.substring(2, licenseSrc.length - 2);
var srcGlob = path.join(constants.pathToSrc, '**/*.js');
var libGlob = path.join(constants.pathToLib, '**/*.js');
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');
fs.writeFile(file, newCode, function(err) {
if(err) throw err;
});
}
else {
// otherwise, throw an error
throw new Error(file + ' : has wrong header information.');
}
});
});
});
function isCorrect(header) {
return (header.value === licenseStr);
}
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, ''));
}