Skip to content

Commit 9bead06

Browse files
committed
tasks: wrap add-header-in-src-files sub-task in function
1 parent 34460c3 commit 9bead06

File tree

1 file changed

+42
-41
lines changed

1 file changed

+42
-41
lines changed

tasks/header.js

+42-41
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var glob = require('glob');
77

88
var constants = require('./util/constants');
99

10+
updateHeadersInSrcFiles();
1011

1112
// add headers to dist files
1213

@@ -30,61 +31,61 @@ function headerLicense(path) {
3031
});
3132
}
3233

34+
// add or update header to src/ lib/ files
35+
function updateHeadersInSrcFiles() {
36+
var srcGlob = path.join(constants.pathToSrc, '**/*.js');
37+
var libGlob = path.join(constants.pathToLib, '**/*.js');
3338
pathsDist.forEach(headerLicense);
3439

40+
// remove leading '/*' and trailing '*/' for comparison with falafel output
41+
var licenseSrc = constants.licenseSrc;
42+
var licenseStr = licenseSrc.substring(2, licenseSrc.length - 2);
3543

36-
// add or update header to src files
44+
glob('{' + srcGlob + ',' + libGlob + '}', function(err, files) {
45+
files.forEach(function(file) {
46+
fs.readFile(file, 'utf-8', function(err, code) {
3747

38-
// remove leading '/*' and trailing '*/' for comparison with falafel output
39-
var licenseSrc = constants.licenseSrc;
40-
var licenseStr = licenseSrc.substring(2, licenseSrc.length - 2);
48+
// parse through code string while keeping track of comments
49+
var comments = [];
50+
falafel(code, {onComment: comments, locations: true}, function() {});
4151

42-
var srcGlob = path.join(constants.pathToSrc, '**/*.js');
43-
var libGlob = path.join(constants.pathToLib, '**/*.js');
44-
glob('{' + srcGlob + ',' + libGlob + '}', function(err, files) {
45-
files.forEach(function(file) {
46-
fs.readFile(file, 'utf-8', function(err, code) {
52+
var header = comments[0];
4753

48-
// parse through code string while keeping track of comments
49-
var comments = [];
50-
falafel(code, {onComment: comments, locations: true}, function() {});
54+
// error out if no header is found
55+
if(!header || header.loc.start.line > 1) {
56+
throw new Error(file + ' : has no header information.');
57+
}
5158

52-
var header = comments[0];
59+
// if header and license are the same, do nothing
60+
if(isCorrect(header)) return;
5361

54-
// error out if no header is found
55-
if(!header || header.loc.start.line > 1) {
56-
throw new Error(file + ' : has no header information.');
57-
}
62+
// if header and license only differ by date, update header
63+
else if(hasWrongDate(header)) {
64+
var codeLines = code.split('\n');
5865

59-
// if header and license are the same, do nothing
60-
if(isCorrect(header)) return;
66+
codeLines.splice(header.loc.start.line - 1, header.loc.end.line);
6167

62-
// if header and license only differ by date, update header
63-
else if(hasWrongDate(header)) {
64-
var codeLines = code.split('\n');
68+
var newCode = licenseSrc + '\n' + codeLines.join('\n');
6569

66-
codeLines.splice(header.loc.start.line - 1, header.loc.end.line);
67-
68-
var newCode = licenseSrc + '\n' + codeLines.join('\n');
69-
70-
fs.writeFile(file, newCode, function(err) {
71-
if(err) throw err;
72-
});
73-
}
74-
else {
75-
// otherwise, throw an error
76-
throw new Error(file + ' : has wrong header information.');
77-
}
70+
fs.writeFile(file, newCode, function(err) {
71+
if(err) throw err;
72+
});
73+
}
74+
else {
75+
// otherwise, throw an error
76+
throw new Error(file + ' : has wrong header information.');
77+
}
78+
});
7879
});
7980
});
80-
});
8181

82-
function isCorrect(header) {
83-
return (header.value === licenseStr);
84-
}
82+
function isCorrect(header) {
83+
return (header.value === licenseStr);
84+
}
8585

86-
function hasWrongDate(header) {
87-
var regex = /Copyright 20[0-9][0-9]-20[0-9][0-9]/g;
86+
function hasWrongDate(header) {
87+
var regex = /Copyright 20[0-9][0-9]-20[0-9][0-9]/g;
8888

89-
return (header.value.replace(regex, '') === licenseStr.replace(regex, ''));
89+
return (header.value.replace(regex, '') === licenseStr.replace(regex, ''));
90+
}
9091
}

0 commit comments

Comments
 (0)