import defaultConfig from './config' ; import * as gutil from 'gulp-util' ; import { readFileSync } from 'fs' ; let logInfo = true ; function initParam ( ) { const argv = require ( 'yargs' ) . argv ; logInfo = ( argv . logInfo === 'false' ? false : true ) ; } function doCheck ( files , licenseHeaderPaths ) { const licenseHeaders = [ ] ; try { for ( let i = 0 ; i < licenseHeaderPaths . length ; i ++ ) { licenseHeaders . push ( readFileSync ( licenseHeaderPaths [ i ] ) . toString ( ) ) ; } } catch ( e ) { gutil . log ( 'license-check' , gutil . colors . red ( `load license header from ${ licenseHeaderPaths } error, ${ e }` ) ) ; process . exit ( - 1 ) ; } files . forEach ( filePath => { const fileContent = readFileSync ( filePath ) . toString ( ) ; let find = false ; for ( let i = 0 ; i < licenseHeaders . length ; i ++ ) { if ( fileContent . indexOf ( licenseHeaders [ i ] ) === 0 ) { find = true ; break ; } } if ( ! find ) { process . exitCode = - 1 ; gutil . log ( 'license-check' , gutil . colors . red ( `${ filePath } doesn't contain the license header` ) ) ; } else if ( logInfo ) { gutil . log ( 'license-check' , gutil . colors . green ( `${ filePath } ok` ) ) ; } } ) ; } function run ( ) { const deglob = require ( 'deglob' ) ; defaultConfig . sources . forEach ( config => { deglob ( config . src . patterns , config . src . options , ( err , files ) => { if ( err ) { gutil . log ( 'license-check' , gutil . colors . red ( err ) ) ; process . exit ( - 1 ) ; } doCheck ( files , config . licenseHeaderPath ) ; } ) ; } ) ; } initParam ( ) ; run ( ) ;