2020-09-25 19:09:40 +00:00
/ * *
* External dependencies
* /
const execSync = require ( 'child_process' ) . execSync ;
const spawnSync = require ( 'child_process' ) . spawnSync ;
const existsSync = require ( 'fs' ) . existsSync ;
const chalk = require ( 'chalk' ) ;
const path = require ( 'path' ) ;
const _ = require ( 'lodash' ) ;
/ *
* A lot of this code has been liberally borrowed from the wp - calypso pre - commit script
* https : //github.com/Automattic/wp-calypso/blob/master/bin/pre-commit-hook.js
* /
const phpcsChangedPath = getPathForCommand ( 'phpcs-changed' ) ;
const phpcsPath = getPathForCommand ( 'phpcs' ) ;
2020-10-06 21:09:14 +00:00
const phpcbfPath = getPathForCommand ( 'phpcbf' ) ;
2020-09-25 19:09:40 +00:00
function quotedPath ( pathToQuote ) {
if ( pathToQuote . includes ( ' ' ) ) {
return ` " ${ pathToQuote } " ` ;
}
return pathToQuote ;
}
/ * *
* Parses the output of a git diff command into file paths .
*
* @ param { string } command Command to run . Expects output like ` git diff --name-only […] `
* @ returns { Array } Paths output from git command
* /
function parseGitDiffToPathArray ( command ) {
return execSync ( command , { encoding : 'utf8' } )
. split ( '\n' )
. map ( ( name ) => name . trim ( ) )
2021-03-31 15:47:01 +00:00
. filter ( ( name ) =>
/(?:\.json|\.[jt]sx?|\.scss|\.php)$/ . test ( name )
) ;
2020-09-25 19:09:40 +00:00
}
function getPathForCommand ( command ) {
2020-10-06 21:09:14 +00:00
const composerBinDir = path . join ( _ _dirname , 'vendor' , 'bin' ) ;
2020-09-25 19:09:40 +00:00
let path _to _command ;
try {
2021-03-31 15:47:01 +00:00
path _to _command = execSync ( 'command -v ' + command , {
encoding : 'utf8' ,
} ) ;
2020-09-25 19:09:40 +00:00
} catch ( e ) {
path _to _command = path . join ( composerBinDir , command ) ;
}
if ( typeof path _to _command === 'undefined' || ! path _to _command ) {
return false ;
}
return _ . trim ( path _to _command ) ;
}
function printPhpcsDocs ( ) {
console . log (
chalk . red ( 'COMMIT ABORTED:' ) ,
'Working with PHP files in this repository requires the PHP Code Sniffer and its dependencies to be installed. Make sure you have composer installed on your machine and from the root of this repository, run `composer install`.'
) ;
process . exit ( 1 ) ;
}
function phpcsInstalled ( ) {
if ( existsSync ( phpcsPath ) && existsSync ( phpcsChangedPath ) ) {
return true ;
}
return false ;
}
2020-10-06 21:09:14 +00:00
function phpcbfInstalled ( ) {
if ( existsSync ( phpcbfPath ) ) {
return true ;
}
return false ;
}
2020-09-25 19:09:40 +00:00
function linterFailure ( ) {
console . log (
chalk . red ( 'COMMIT ABORTED:' ) ,
'The linter reported some problems. ' +
2021-03-31 15:47:01 +00:00
'If you are aware of them and it is OK, ' +
'repeat the commit command with --no-verify to avoid this check.'
2020-09-25 19:09:40 +00:00
) ;
process . exit ( 1 ) ;
}
// determine if PHPCS is available
const phpcs = phpcsInstalled ( ) ;
2020-10-06 21:09:14 +00:00
// determine if PHPCBF is available
const phpcbf = phpcbfInstalled ( ) ;
2021-03-31 15:47:01 +00:00
// grab a list of all the php files staged to commit
const phpFiles = parseGitDiffToPathArray (
'git diff --cached --name-only --diff-filter=ACM'
) . filter ( ( file ) => file . endsWith ( '.php' ) ) ;
2020-09-25 19:09:40 +00:00
2021-03-31 15:47:01 +00:00
if ( phpFiles . length ) {
phpcbfResult = spawnSync ( phpcbfPath , [ ... phpFiles ] , {
shell : true ,
stdio : 'inherit' ,
} ) ;
2020-10-06 21:09:14 +00:00
if ( phpcbfResult && phpcbfResult . status ) {
2021-03-31 15:47:01 +00:00
execSync ( ` git add ${ phpFiles . join ( ' ' ) } ` ) ;
console . log (
chalk . yellow (
'PHPCS issues detected and automatically fixed via PHPCBF.'
)
) ;
2020-10-06 21:09:14 +00:00
}
2020-09-25 19:09:40 +00:00
if ( phpcs ) {
const lintResult = spawnSync (
2021-03-31 15:47:01 +00:00
` PHPCS= ${ quotedPath ( phpcsPath ) } ${ quotedPath (
phpcsChangedPath
) } ` ,
[ '--git' , ... phpFiles ] ,
2020-09-25 19:09:40 +00:00
{
shell : true ,
stdio : 'inherit' ,
}
) ;
if ( lintResult . status ) {
linterFailure ( ) ;
}
}
}
2021-03-31 15:47:01 +00:00
// grab a list of all the js files staged to commit
const jsFiles = parseGitDiffToPathArray (
'git diff --cached --name-only --diff-filter=ACM'
) . filter ( ( file ) => file . endsWith ( '.js' ) ) ;
if ( jsFiles . length ) {
jsFiles . forEach ( ( file ) =>
console . log ( ` Prettier formatting staged file: ${ file } ` )
) ;
execSync (
` ./node_modules/.bin/prettier --ignore-path .eslintignore --write ${ jsFiles . join (
' '
) } `
) ;
execSync ( ` git add ${ jsFiles . join ( ' ' ) } ` ) ;
}