|
@@ -34,14 +34,18 @@ function parseGitDiffToPathArray( command ) {
|
|
|
return execSync( command, { encoding: 'utf8' } )
|
|
|
.split( '\n' )
|
|
|
.map( ( name ) => name.trim() )
|
|
|
- .filter( ( name ) => /(?:\.json|\.[jt]sx?|\.scss|\.php)$/.test( name ) );
|
|
|
+ .filter( ( name ) =>
|
|
|
+ /(?:\.json|\.[jt]sx?|\.scss|\.php)$/.test( name )
|
|
|
+ );
|
|
|
}
|
|
|
|
|
|
function getPathForCommand( command ) {
|
|
|
const composerBinDir = path.join( __dirname, 'vendor', 'bin' );
|
|
|
let path_to_command;
|
|
|
try {
|
|
|
- path_to_command = execSync( 'command -v ' + command, { encoding: 'utf8' } );
|
|
|
+ path_to_command = execSync( 'command -v ' + command, {
|
|
|
+ encoding: 'utf8',
|
|
|
+ } );
|
|
|
} catch ( e ) {
|
|
|
path_to_command = path.join( composerBinDir, command );
|
|
|
}
|
|
@@ -77,8 +81,8 @@ function linterFailure() {
|
|
|
console.log(
|
|
|
chalk.red( 'COMMIT ABORTED:' ),
|
|
|
'The linter reported some problems. ' +
|
|
|
- 'If you are aware of them and it is OK, ' +
|
|
|
- 'repeat the commit command with --no-verify to avoid this check.'
|
|
|
+ 'If you are aware of them and it is OK, ' +
|
|
|
+ 'repeat the commit command with --no-verify to avoid this check.'
|
|
|
);
|
|
|
process.exit( 1 );
|
|
|
}
|
|
@@ -89,27 +93,32 @@ const phpcs = phpcsInstalled();
|
|
|
// determine if PHPCBF is available
|
|
|
const phpcbf = phpcbfInstalled();
|
|
|
|
|
|
-// grab a list of all the files staged to commit
|
|
|
-const files = parseGitDiffToPathArray( 'git diff --cached --name-only --diff-filter=ACM' ).filter( ( file ) => file.endsWith( '.php' ) );
|
|
|
+// 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' ) );
|
|
|
|
|
|
-if ( files.length ) {
|
|
|
- phpcbfResult = spawnSync( phpcbfPath,
|
|
|
- [ ...files ],
|
|
|
- {
|
|
|
- shell: true,
|
|
|
- stdio: 'inherit',
|
|
|
- }
|
|
|
- );
|
|
|
+if ( phpFiles.length ) {
|
|
|
+ phpcbfResult = spawnSync( phpcbfPath, [ ...phpFiles ], {
|
|
|
+ shell: true,
|
|
|
+ stdio: 'inherit',
|
|
|
+ } );
|
|
|
|
|
|
if ( phpcbfResult && phpcbfResult.status ) {
|
|
|
- execSync( `git add ${ files.join( ' ' ) }` );
|
|
|
- console.log( chalk.yellow( 'PHPCS issues detected and automatically fixed via PHPCBF.' ) );
|
|
|
+ execSync( `git add ${ phpFiles.join( ' ' ) }` );
|
|
|
+ console.log(
|
|
|
+ chalk.yellow(
|
|
|
+ 'PHPCS issues detected and automatically fixed via PHPCBF.'
|
|
|
+ )
|
|
|
+ );
|
|
|
}
|
|
|
|
|
|
if ( phpcs ) {
|
|
|
const lintResult = spawnSync(
|
|
|
- `PHPCS=${ quotedPath( phpcsPath ) } ${ quotedPath( phpcsChangedPath ) }`,
|
|
|
- [ '--git', ...files ],
|
|
|
+ `PHPCS=${ quotedPath( phpcsPath ) } ${ quotedPath(
|
|
|
+ phpcsChangedPath
|
|
|
+ ) }`,
|
|
|
+ [ '--git', ...phpFiles ],
|
|
|
{
|
|
|
shell: true,
|
|
|
stdio: 'inherit',
|
|
@@ -121,3 +130,20 @@ if ( files.length ) {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+// 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( ' ' ) }` );
|
|
|
+}
|