2018-03-14 19:32:36 +00:00
const glob = require ( 'glob' ) ;
const prettier = require ( 'prettier' ) ;
const fs = require ( 'fs' ) ;
const getStagedFiles = require ( './frontend_script_utils' ) . getStagedFiles ;
const mode = process . argv [ 2 ] || 'check' ;
const shouldSave = mode === 'save' || mode === 'save-all' ;
const allFiles = mode === 'check-all' || mode === 'save-all' ;
const config = {
patterns : [ '**/*.js' , '**/*.vue' , '**/*.scss' ] ,
ignore : [ '**/node_modules/**' , '**/vendor/**' , '**/public/**' ] ,
parsers : {
js : 'babylon' ,
vue : 'vue' ,
scss : 'css' ,
} ,
} ;
const availableExtensions = Object . keys ( config . parsers ) ;
console . log ( ` Loading ${ allFiles ? 'All' : 'Staged' } Files ... ` ) ;
2018-03-23 16:52:54 +00:00
const stagedFiles = allFiles ? null : getStagedFiles ( availableExtensions . map ( ext => ` *. ${ ext } ` ) ) ;
2018-03-14 19:32:36 +00:00
if ( stagedFiles ) {
if ( ! stagedFiles . length || ( stagedFiles . length === 1 && ! stagedFiles [ 0 ] ) ) {
console . log ( 'No matching staged files.' ) ;
return ;
}
console . log ( ` Matching staged Files : ${ stagedFiles . length } ` ) ;
}
let didWarn = false ;
let didError = false ;
let files ;
if ( allFiles ) {
const ignore = config . ignore ;
const patterns = config . patterns ;
2018-03-23 16:52:54 +00:00
const globPattern = patterns . length > 1 ? ` { ${ patterns . join ( ',' ) } } ` : ` ${ patterns . join ( ',' ) } ` ;
files = glob . sync ( globPattern , { ignore } ) . filter ( f => allFiles || stagedFiles . includes ( f ) ) ;
2018-03-14 19:32:36 +00:00
} else {
2018-03-23 16:52:54 +00:00
files = stagedFiles . filter ( f => availableExtensions . includes ( f . split ( '.' ) . pop ( ) ) ) ;
2018-03-14 19:32:36 +00:00
}
if ( ! files . length ) {
console . log ( 'No Files found to process with Prettier' ) ;
return ;
}
console . log ( ` ${ shouldSave ? 'Updating' : 'Checking' } ${ files . length } file(s) ` ) ;
prettier
. resolveConfig ( '.' )
. then ( options => {
console . log ( 'Found options : ' , options ) ;
files . forEach ( file => {
try {
const fileExtension = file . split ( '.' ) . pop ( ) ;
Object . assign ( options , {
parser : config . parsers [ fileExtension ] ,
} ) ;
const input = fs . readFileSync ( file , 'utf8' ) ;
if ( shouldSave ) {
const output = prettier . format ( input , options ) ;
if ( output !== input ) {
fs . writeFileSync ( file , output , 'utf8' ) ;
console . log ( ` Prettified : ${ file } ` ) ;
}
} else if ( ! prettier . check ( input , options ) ) {
if ( ! didWarn ) {
console . log (
2018-03-23 16:52:54 +00:00
'\n===============================\nGitLab uses Prettier to format all JavaScript code.\nPlease format each file listed below or run "yarn prettier-staged-save"\n===============================\n'
2018-03-14 19:32:36 +00:00
) ;
didWarn = true ;
}
console . log ( ` Prettify Manually : ${ file } ` ) ;
}
} catch ( error ) {
didError = true ;
console . log ( ` \n \n Error with ${ file } : ${ error . message } ` ) ;
}
} ) ;
if ( didWarn || didError ) {
process . exit ( 1 ) ;
}
} )
. catch ( e => {
console . log ( ` Error on loading the Config File: ${ e . message } ` ) ;
process . exit ( 1 ) ;
} ) ;