1
+ var path = require ( 'path' ) ;
2
+ var fs = require ( 'fs' ) ;
3
+
1
4
var prependFile = require ( 'prepend-file' ) ;
5
+ var falafel = require ( 'falafel' ) ;
6
+ var glob = require ( 'glob' ) ;
2
7
3
8
var constants = require ( './util/constants' ) ;
4
9
@@ -19,3 +24,54 @@ function headerLicense(path) {
19
24
}
20
25
21
26
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 = / C o p y r i g h t 2 0 [ 0 - 9 ] [ 0 - 9 ] - 2 0 [ 0 - 9 ] [ 0 - 9 ] / g;
75
+
76
+ return ( header . value . replace ( regex , '' ) === licenseStr . replace ( regex , '' ) ) ;
77
+ }
0 commit comments