|
|
const execSync = require( 'child_process' ).execSync; |
|
|
const spawnSync = require( 'child_process' ).spawnSync; |
|
|
const existsSync = require( 'fs' ).existsSync; |
|
|
const path = require( 'path' ); |
|
|
const chalk = require( 'chalk' ); |
|
|
const _ = require( 'lodash' ); |
|
|
|
|
|
const phpcsPath = getPathForCommand( 'phpcs' ); |
|
|
const phpcbfPath = getPathForCommand( 'phpcbf' ); |
|
|
|
|
|
function quotedPath( pathToQuote ) { |
|
|
if ( pathToQuote.includes( ' ' ) ) { |
|
|
return `"${ pathToQuote }"`; |
|
|
} |
|
|
return pathToQuote; |
|
|
} |
|
|
|
|
|
console.log( |
|
|
'\nBy contributing to this project, you license the materials you contribute ' + |
|
|
'under the GNU General Public License v2 (or later). All materials must have ' + |
|
|
'GPLv2 compatible licenses — see docs/CONTRIBUTING.md for details.\n\n' |
|
|
); |
|
|
|
|
|
|
|
|
require( './validate-config-keys' ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function parseGitDiffToPathArray( command ) { |
|
|
return execSync( command, { encoding: 'utf8' } ) |
|
|
.split( '\n' ) |
|
|
.map( ( name ) => name.trim() ) |
|
|
.filter( ( name ) => /(?:\.json|\.[jt]sx?|\.scss|\.php)$/.test( name ) ); |
|
|
} |
|
|
|
|
|
function getPathForCommand( command ) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const path_to_command = path.join( __dirname, '..', 'vendor', 'bin', command ); |
|
|
return _.trim( path_to_command ); |
|
|
} |
|
|
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.' |
|
|
); |
|
|
process.exit( 1 ); |
|
|
} |
|
|
|
|
|
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( phpcbfPath ) ) { |
|
|
return true; |
|
|
} |
|
|
return false; |
|
|
} |
|
|
|
|
|
|
|
|
const phpcs = phpcsInstalled(); |
|
|
|
|
|
|
|
|
const files = parseGitDiffToPathArray( 'git diff --cached --name-only --diff-filter=ACM' ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const dirtyFiles = new Set( parseGitDiffToPathArray( 'git diff --name-only --diff-filter=ACM' ) ); |
|
|
|
|
|
|
|
|
dirtyFiles.forEach( ( file ) => |
|
|
console.log( |
|
|
chalk.red( `${ file } will not be auto-formatted because it has unstaged changes.` ) |
|
|
) |
|
|
); |
|
|
|
|
|
|
|
|
const toFormat = files.filter( ( file ) => ! dirtyFiles.has( file ) ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const { |
|
|
toPrettify = [], |
|
|
toStylelintfix = [], |
|
|
toPHPCBF = [], |
|
|
} = _.groupBy( toFormat, ( file ) => { |
|
|
switch ( true ) { |
|
|
case file.endsWith( '.scss' ): |
|
|
return 'toStylelintfix'; |
|
|
case file.endsWith( '.php' ): |
|
|
return 'toPHPCBF'; |
|
|
default: |
|
|
return 'toPrettify'; |
|
|
} |
|
|
} ); |
|
|
|
|
|
|
|
|
toPrettify.forEach( ( file ) => console.log( `Prettier formatting staged file: ${ file }` ) ); |
|
|
if ( toPrettify.length ) { |
|
|
|
|
|
_.forEach( _.chunk( toPrettify, 500 ), ( chunk ) => { |
|
|
execSync( |
|
|
`./node_modules/.bin/prettier --ignore-path .eslintignore --write ${ chunk.join( ' ' ) }` |
|
|
); |
|
|
execSync( `git add ${ chunk.join( ' ' ) }` ); |
|
|
} ); |
|
|
} |
|
|
|
|
|
|
|
|
toStylelintfix.forEach( ( file ) => console.log( `stylelint formatting staged file: ${ file }` ) ); |
|
|
if ( toStylelintfix.length ) { |
|
|
spawnSync( `./node_modules/.bin/stylelint --fix ${ toStylelintfix.join( ' ' ) }` ); |
|
|
execSync( `git add ${ toStylelintfix.join( ' ' ) }` ); |
|
|
} |
|
|
|
|
|
|
|
|
toPHPCBF.forEach( ( file ) => console.log( `PHPCBF formatting staged file: ${ file }` ) ); |
|
|
if ( toPHPCBF.length ) { |
|
|
if ( phpcs ) { |
|
|
try { |
|
|
execSync( |
|
|
`${ quotedPath( phpcbfPath ) } --standard=apps/phpcs.xml ${ toPHPCBF.join( ' ' ) }` |
|
|
); |
|
|
} catch ( error ) { |
|
|
|
|
|
|
|
|
if ( 2 === error.status ) { |
|
|
linterFailure(); |
|
|
} |
|
|
} |
|
|
execSync( `git add ${ toPHPCBF.join( ' ' ) }` ); |
|
|
} else { |
|
|
printPhpcsDocs(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
const { |
|
|
toEslint = [], |
|
|
toStylelint = [], |
|
|
toPHPCS = [], |
|
|
} = _.groupBy( |
|
|
files.filter( ( file ) => ! file.endsWith( '.json' ) ), |
|
|
( file ) => { |
|
|
switch ( true ) { |
|
|
case file.endsWith( '.scss' ): |
|
|
return 'toStylelint'; |
|
|
case file.endsWith( '.php' ): |
|
|
return 'toPHPCS'; |
|
|
default: |
|
|
return 'toEslint'; |
|
|
} |
|
|
} |
|
|
); |
|
|
|
|
|
|
|
|
if ( toStylelint.length ) { |
|
|
const lintResult = spawnSync( './node_modules/.bin/stylelint', [ ...toStylelint ], { |
|
|
shell: true, |
|
|
stdio: 'inherit', |
|
|
} ); |
|
|
|
|
|
if ( lintResult.status ) { |
|
|
linterFailure(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if ( toEslint.length ) { |
|
|
const lintResult = spawnSync( './node_modules/.bin/eslint', [ '--quiet', ...toEslint ], { |
|
|
shell: true, |
|
|
stdio: 'inherit', |
|
|
} ); |
|
|
|
|
|
if ( lintResult.status ) { |
|
|
linterFailure(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if ( toPHPCS.length ) { |
|
|
if ( phpcs ) { |
|
|
const lintResult = spawnSync( |
|
|
quotedPath( phpcsPath ), |
|
|
[ '--standard=apps/phpcs.xml', ...toPHPCS ], |
|
|
{ |
|
|
shell: true, |
|
|
stdio: 'inherit', |
|
|
} |
|
|
); |
|
|
|
|
|
if ( lintResult.status ) { |
|
|
linterFailure(); |
|
|
} |
|
|
} else { |
|
|
printPhpcsDocs(); |
|
|
} |
|
|
} |
|
|
|