11#!/usr/bin/env node
2-
32/* * * * * * * * * * * * * * * * * * * * * * * * * * * */
43/* Copyright (c) 2017 Mobify Research & Development Inc. All rights reserved. */
54/* * * * * * * * * * * * * * * * * * * * * * * * * * * */
@@ -63,6 +62,19 @@ const buildSupportedExtensions = () => {
6362 } )
6463}
6564
65+ /**
66+ * Removes extra \n characters from the top of any files
67+ * to ensure more consistent spacing between copyright headers
68+ * @param {String } content original file to edit
69+ * @return {String } new file with no leading \n
70+ */
71+ const removeLeadingNewlines = ( content ) => {
72+ if ( content [ 0 ] !== '' ) {
73+ return content
74+ }
75+ content . shift ( )
76+ return removeLeadingNewlines ( content )
77+ }
6678
6779if ( args . length === 0 || args . indexOf ( '--help' ) >= 0 ) {
6880
@@ -105,13 +117,15 @@ args
105117 const content = fs . readFileSync ( file )
106118 const hasCopyrightHeader = content . includes ( 'Copyright (c)' )
107119 const ext = file . match ( / \. [ 0 - 9 a - z ] + $ / i) [ 0 ]
108-
109120 let newData = ''
110121
111122 if ( hasCopyrightHeader && updateMode ) {
112- newData = content . toString ( ) . replace ( / ( \( c \) \s ) ( \d { 4 } ) / , `$1 ${ currentYear } ` )
113- fs . writeFileSync ( file , newData )
114- console . log ( `${ green } Copyright header succesfully updated to ${ currentYear } in ${ magenta } ${ file } ` )
123+ let previousHeaderYear = content . toString ( ) . match ( / (?: \( c \) ) (?: \s ) ( \d { 4 } ) / ) [ 1 ]
124+ if ( previousHeaderYear !== currentYear . toString ( ) ) {
125+ newData = content . toString ( ) . replace ( `(c) ${ previousHeaderYear } ` , `(c) ${ currentYear } ` )
126+ fs . writeFileSync ( file , newData )
127+ console . log ( `${ green } Copyright header succesfully updated from ${ previousHeaderYear } to ${ currentYear } in ${ magenta } ${ file } ` )
128+ }
115129 }
116130
117131 if ( ! hasCopyrightHeader ) {
@@ -124,10 +138,11 @@ args
124138 // accomodate for shebang and insert before header
125139 if ( contentStr [ 0 ] . indexOf ( '#!' ) >= 0 ) {
126140 const shebang = contentStr . shift ( )
127- contentStr = contentStr . join ( '\n' )
128- newData = shebang + '\n\n ' + getHeaderText ( ext ) + '\n' + contentStr // eslint-disable-line prefer-template
141+ contentStr = removeLeadingNewlines ( contentStr ) . join ( '\n' )
142+ newData = shebang + '\n' + getHeaderText ( ext ) + '\n' + contentStr // eslint-disable-line prefer-template
129143 } else {
130- newData = getHeaderText ( ext ) + `\n${ content } ` // eslint-disable-line prefer-template
144+ contentStr = removeLeadingNewlines ( contentStr ) . join ( '\n' )
145+ newData = getHeaderText ( ext ) + `\n${ contentStr } ` // eslint-disable-line prefer-template
131146 }
132147
133148 fs . writeFileSync ( file , newData )
0 commit comments