import { EOL as newline } from "os" ; import { Handler , readFile , writeFile , } from "../common" ; const copyrightText = "Copyright (c) Microsoft Corporation and contributors. All rights reserved." + newline + "Licensed under the MIT License." ; const autoGenText = newline + "THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY" ; interface IFileConfig { /** File type reported in error message (e.g., 'Html') */ type : string ; /** Regex matching header prefix (e.g. '/*!\r?\n') */ headerStart ? : RegExp ; /** Regex matching beginning of each line (e.g. ' * ') */ lineStart : RegExp ; /** Regex matching the end of each line (e.g., '\r?\n') */ lineEnd : RegExp ; /** Regex matching the header postfix. */ headerEnd ? : RegExp ; } /**
 * Given an 'IFileConfig' produces a function that detects correct copyright headers
 * and returns an error string if the header is missing or incorrect.
 */ function makeHandler ( config ) { const pre = config . headerStart ?. source ?? "" ; const start = config . lineStart . source ; const end = config . lineEnd . source ; const post = config . headerEnd ?. source ?? "" ; const toRegex = ( text ) => text . split ( newline ) . map ( ( line ) => `${ start }${ line . replace ( /[.*+?^${}()|[\]\\]/g , '\\$&' ) }${ end }` ) . join ( "" ) . replace ( /\s*\\r\?\\n/ , "\\r?\\n" ) ; const regex = new RegExp ( `^${ pre }${ toRegex ( copyrightText ) }(${ toRegex ( autoGenText ) })?${ post }` ) ; return ( file ) => { const content = readFile ( file ) ; if ( ! regex . test ( content ) ) { return `${ config . type } file missing copyright header` ; } } ; } export const handlers = [ { name : "html-copyright-file-header" , match : /(^|\/)[^\/]+\.html$/i , handler : makeHandler ( { type : "Html" , lineStart : /<!-- / , lineEnd : / -->\r?\n/ , } ) , resolver : file => { const prevContent = readFile ( file ) ; const newContent = '<!-- ' + copyrightText . replace ( newline , ' -->' + newline + '<!-- ' ) + ' -->' + newline + newline + prevContent ; writeFile ( file , newContent ) ; return { resolved : true } ; } } , { name : "dockerfile-copyright-file-header" , match : /(^|\/)Dockerfile$/i , handler : makeHandler ( { type : "Dockerfile" , lineStart : /# / , lineEnd : /\r?\n/ , } ) , resolver : file => { const prevContent = readFile ( file ) ; const newContent = '# ' + copyrightText . replace ( newline , newline + '# ' ) + newline + newline + prevContent ; writeFile ( file , newContent ) ; return { resolved : true } ; } } , { name : "js-ts-copyright-file-header" , match : /(^|\/)[^\/]+\.[jt]sx?$/i , handler : makeHandler ( { type : "JavaScript/TypeScript" , headerStart : /(#![^\n]*\r?\n)?\/\*!\r?\n/ , lineStart : / \* / , lineEnd : /\r?\n/ , headerEnd : / \*\/\r?\n/ , } ) , resolver : file => { const prevContent = readFile ( file ) ; const separator = prevContent . startsWith ( '\r' ) || prevContent . startsWith ( '\n' ) ? newline : newline + newline ; const newContent = '/*!' + newline + ' * ' + copyrightText . replace ( newline , newline + ' * ' ) + newline + ' */' + separator + prevContent ; writeFile ( file , newContent ) ; return { resolved : true } ; } } , ] ;