66 * @link https://github.com/splish-me/copyright-headers for the canonical source repository
77 */
88import * as fs from 'fs'
9+ import * as R from 'ramda'
910import * as signale from 'signale'
1011import * as util from 'util'
1112
@@ -120,33 +121,42 @@ export function getUpdatedCopyrightHeader(
120121 options : CopyrightHeaderOptions
121122) : [ CopyrightHeaderStatus , string ] {
122123 const header = getLicenseHeader ( language , options . lines )
123- const re = getLicenseHeaderRegExp ( language )
124- const match = content . match ( re )
124+ const existingCopyrightHeader = getFirstCopyrightHeader ( content , language )
125125
126- if ( ! match ) {
126+ if ( ! existingCopyrightHeader ) {
127127 let newHeader = header
128128 let newContent = content
129129
130- if ( language . before && newContent . startsWith ( language . before ) ) {
131- newContent = content . substring ( language . before . length )
132- } else {
133- if ( language . after ) {
134- newHeader += `${ language . after } `
130+ const contentStartsWithOpeningTag =
131+ language . before && content . startsWith ( language . before )
132+
133+ if ( language . before ) {
134+ newHeader = language . before + newHeader
135+
136+ if ( contentStartsWithOpeningTag ) {
137+ newContent = content . substring ( language . before . length )
135138 }
136139 }
137140
141+ if ( language . after && ! contentStartsWithOpeningTag ) {
142+ newHeader += language . after
143+ }
144+
138145 return [ CopyrightHeaderStatus . Added , `${ newHeader } \n${ newContent } ` ]
139146 }
140147
141- if ( match [ 0 ] === header ) {
148+ if ( existingCopyrightHeader === header ) {
142149 return [ CopyrightHeaderStatus . Unchanged , '' ]
143150 }
144151
145152 if (
146153 typeof options . shouldUpdate === 'function' &&
147- options . shouldUpdate ( match [ 0 ] )
154+ options . shouldUpdate ( existingCopyrightHeader )
148155 ) {
149- return [ CopyrightHeaderStatus . Changed , content . replace ( re , header ) ]
156+ return [
157+ CopyrightHeaderStatus . Changed ,
158+ content . replace ( existingCopyrightHeader , header )
159+ ]
150160 }
151161
152162 return [ CopyrightHeaderStatus . External , content ]
@@ -167,19 +177,20 @@ function getSourceLanguage(filePath: string) {
167177
168178function getLicenseHeader ( language : SourceLanguage , lines : string [ ] ) {
169179 return (
170- ( language . before || '' ) +
171180 `${ language . begin } \n` +
172181 lines . map ( language . buildLine ) . join ( '\n' ) +
173182 `\n${ language . end } `
174183 )
175184}
176185
177- function getLicenseHeaderRegExp ( language : SourceLanguage ) {
178- const forRe = ( s : string ) => s . replace ( / ( \? | \/ | \* ) / g , match => `\\ ${ match } ` )
186+ function getFirstCopyrightHeader ( content : string , language : SourceLanguage ) {
187+ const after = R . tail ( R . split ( language . begin , content ) )
179188
180- return new RegExp (
181- `${ forRe ( language . before || '' ) } ${ forRe ( language . begin ) } \n(.+\n)*${ forRe (
182- language . end
183- ) } `
184- )
189+ if ( R . isEmpty ( after ) ) {
190+ return ''
191+ }
192+
193+ const [ comment ] = R . split ( language . end , after . join ( '' ) )
194+
195+ return `${ language . begin } ${ comment } ${ language . end } `
185196}
0 commit comments