From 5ae2a743206a8573bab7fe4b4ef83d32d63e5864 Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Wed, 31 Mar 2021 16:47:01 +0100 Subject: [PATCH] test commit --- package-lock.json | 5 ++ package.json | 5 +- pre-commit-hook.js | 62 ++++++++++++++++++------- seedlet/assets/js/primary-navigation.js | 60 ++++++++++++++---------- 4 files changed, 88 insertions(+), 44 deletions(-) diff --git a/package-lock.json b/package-lock.json index f48aefced..174431e1d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -627,6 +627,11 @@ "integrity": "sha1-Zws6+8VS4LVd9rd4DKdGFfI60cs=", "dev": true }, + "wp-prettier": { + "version": "2.0.5", + "resolved": "https://registry.npmjs.org/wp-prettier/-/wp-prettier-2.0.5.tgz", + "integrity": "sha512-5GCgdeevIXwR3cW4Qj5XWC5MO1iSCz8+IPn0mMw6awAt/PBiey8yyO7MhePRsaMqghJAhg6Q3QLYWSnUHWkG6A==" + }, "wrap-ansi": { "version": "5.1.0", "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-5.1.0.tgz", diff --git a/package.json b/package.json index 1fe7be14d..e8175a420 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "Automattic public themes", "author": "automattic", "license": "GPL-2.0", - "prettier": "@wordpress/prettier-config", + "prettier": "@wordpress/prettier-config", "scripts": { "sandbox:clean": "./sandbox.sh clean", "sandbox:push": "./sandbox.sh push", @@ -22,5 +22,8 @@ "hooks": { "pre-commit": "node pre-commit-hook.js" } + }, + "dependencies": { + "wp-prettier": "^2.0.5" } } diff --git a/pre-commit-hook.js b/pre-commit-hook.js index 38028e574..b75615d5f 100644 --- a/pre-commit-hook.js +++ b/pre-commit-hook.js @@ -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( ' ' ) }` ); +} diff --git a/seedlet/assets/js/primary-navigation.js b/seedlet/assets/js/primary-navigation.js index 6992ab4c2..61d6239d6 100644 --- a/seedlet/assets/js/primary-navigation.js +++ b/seedlet/assets/js/primary-navigation.js @@ -3,59 +3,69 @@ * * Required to open and close the mobile navigation. */ -( function() { - +( function () { /** * Menu Toggle Behaviors * * @param {Element} element */ - var navMenu = function ( id ){ - var wrapper = document.body; // this is the element to which a CSS class is added when a mobile nav menu is open - var openButton = document.getElementById( id + '-open-menu' ); - var closeButton = document.getElementById( id + '-close-menu' ); + var navMenu = function ( id ) { + var wrapper = document.body; // this is the element to which a CSS class is added when a mobile nav menu is open + var openButton = document.getElementById( id + '-open-menu' ); + var closeButton = document.getElementById( id + '-close-menu' ); - if ( openButton && closeButton ){ - openButton.onclick = function() { + if ( openButton && closeButton ) { + openButton.onclick = function () { wrapper.classList.add( id + '-navigation-open' ); wrapper.classList.add( 'lock-scrolling' ); closeButton.focus(); - } + }; - closeButton.onclick = function() { + closeButton.onclick = function () { wrapper.classList.remove( id + '-navigation-open' ); wrapper.classList.remove( 'lock-scrolling' ); openButton.focus(); - } + }; } /** * Trap keyboard navigation in the menu modal. * Adapted from TwentyTwenty */ - document.addEventListener( 'keydown', function( event ) { - if ( ! wrapper.classList.contains( id + '-navigation-open' ) ){ + document.addEventListener( 'keydown', function ( event ) { + if ( ! wrapper.classList.contains( id + '-navigation-open' ) ) { return; - } - var modal, elements, selectors, lastEl, firstEl, activeEl, tabKey, shiftKey, escKey; + } + var modal, + elements, + selectors, + lastEl, + firstEl, + activeEl, + tabKey, + shiftKey, + escKey; modal = document.querySelector( '.' + id + '-navigation' ); - selectors = "input, a, button"; + selectors = 'input, a, button'; elements = modal.querySelectorAll( selectors ); elements = Array.prototype.slice.call( elements ); - elements = elements.filter( function( el ) { + elements = elements.filter( function ( el ) { return ! el.classList.contains( 'woocommerce-cart-link' ); // ignore this element because it's hidden on mobile - }); + } ); tabKey = event.keyCode === 9; shiftKey = event.shiftKey; escKey = event.keyCode === 27; activeEl = document.activeElement; lastEl = elements[ elements.length - 1 ]; - firstEl = elements[0]; + firstEl = elements[ 0 ]; if ( escKey ) { event.preventDefault(); - wrapper.classList.remove( id + '-navigation-open', 'lock-scrolling' ); + wrapper.classList.remove( + id + '-navigation-open', + 'lock-scrolling' + ); openButton.focus(); } @@ -73,11 +83,11 @@ if ( tabKey && firstEl === lastEl ) { event.preventDefault(); } - }); - } + } ); + }; - window.addEventListener( 'load', function() { + window.addEventListener( 'load', function () { new navMenu( 'primary' ); new navMenu( 'woo' ); - }); -} )(); \ No newline at end of file + } ); +} )();