2018-03-14 15:32:36 -04:00
const glob = require ( 'glob' ) ;
const prettier = require ( 'prettier' ) ;
const fs = require ( 'fs' ) ;
2018-03-26 03:39:14 -04:00
const path = require ( 'path' ) ;
const prettierIgnore = require ( 'ignore' ) ( ) ;
2018-03-14 15:32:36 -04:00
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' ;
2018-05-26 14:01:02 -04:00
let dirPath = process . argv [ 3 ] || '' ;
2018-06-14 14:03:14 -04:00
if ( dirPath && dirPath . charAt ( dirPath . length - 1 ) !== '/' ) dirPath += '/' ;
2018-03-14 15:32:36 -04:00
const config = {
patterns : [ '**/*.js' , '**/*.vue' , '**/*.scss' ] ,
2018-03-26 03:39:14 -04:00
/ *
* The ignore patterns below are just to reduce search time with glob , as it includes the
* folders with the most ignored assets , the actual ` .prettierignore ` will be used later on
* /
2018-03-14 15:32:36 -04:00
ignore : [ '**/node_modules/**' , '**/vendor/**' , '**/public/**' ] ,
parsers : {
js : 'babylon' ,
vue : 'vue' ,
scss : 'css' ,
} ,
} ;
2018-03-26 03:39:14 -04:00
/ *
* Unfortunately the prettier API does not expose support for ` .prettierignore ` files , they however
* use the ignore package , so we do the same . We simply cannot use the glob package , because
* gitignore style is not compatible with globs ignore style .
* /
prettierIgnore . add (
fs
. readFileSync ( path . join ( _ _dirname , '../../' , '.prettierignore' ) )
. toString ( )
. trim ( )
. split ( /\r?\n/ )
) ;
2018-03-14 15:32:36 -04:00
const availableExtensions = Object . keys ( config . parsers ) ;
2018-05-26 14:01:02 -04:00
console . log ( ` Loading ${ allFiles ? 'All' : 'Selected' } Files ... ` ) ;
2018-03-14 15:32:36 -04:00
2018-06-14 14:03:14 -04:00
const stagedFiles =
allFiles || dirPath ? null : getStagedFiles ( availableExtensions . map ( ext => ` *. ${ ext } ` ) ) ;
2018-03-14 15:32:36 -04:00
if ( stagedFiles ) {
if ( ! stagedFiles . length || ( stagedFiles . length === 1 && ! stagedFiles [ 0 ] ) ) {
console . log ( 'No matching staged files.' ) ;
2018-07-09 05:53:23 -04:00
process . exit ( 1 ) ;
2018-03-14 15:32:36 -04:00
}
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 12:52:54 -04:00
const globPattern = patterns . length > 1 ? ` { ${ patterns . join ( ',' ) } } ` : ` ${ patterns . join ( ',' ) } ` ;
files = glob . sync ( globPattern , { ignore } ) . filter ( f => allFiles || stagedFiles . includes ( f ) ) ;
2018-05-26 14:01:02 -04:00
} else if ( dirPath ) {
const ignore = config . ignore ;
2018-06-14 14:03:14 -04:00
const patterns = config . patterns . map ( item => {
return dirPath + item ;
} ) ;
2018-05-26 14:01:02 -04:00
const globPattern = patterns . length > 1 ? ` { ${ patterns . join ( ',' ) } } ` : ` ${ patterns . join ( ',' ) } ` ;
files = glob . sync ( globPattern , { ignore } ) ;
2018-03-14 15:32:36 -04:00
} else {
2018-03-23 12:52:54 -04:00
files = stagedFiles . filter ( f => availableExtensions . includes ( f . split ( '.' ) . pop ( ) ) ) ;
2018-03-14 15:32:36 -04:00
}
2018-03-26 03:39:14 -04:00
files = prettierIgnore . filter ( files ) ;
2018-03-14 15:32:36 -04:00
if ( ! files . length ) {
console . log ( 'No Files found to process with Prettier' ) ;
2018-07-09 05:53:23 -04:00
process . exit ( 1 ) ;
2018-03-14 15:32:36 -04:00
}
console . log ( ` ${ shouldSave ? 'Updating' : 'Checking' } ${ files . length } file(s) ` ) ;
2018-05-26 13:36:41 -04:00
files . forEach ( file => {
try {
prettier
. resolveConfig ( file )
. then ( options => {
2018-03-14 15:32:36 -04:00
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 12:52:54 -04: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 15:32:36 -04:00
) ;
didWarn = true ;
}
console . log ( ` Prettify Manually : ${ file } ` ) ;
}
2018-05-26 13:36:41 -04:00
} )
. catch ( e => {
console . log ( ` Error on loading the Config File: ${ e . message } ` ) ;
process . exit ( 1 ) ;
} ) ;
} catch ( error ) {
didError = true ;
console . log ( ` \n \n Error with ${ file } : ${ error . message } ` ) ;
}
} ) ;
if ( didWarn || didError ) {
process . exit ( 1 ) ;
}