This repository was archived by the owner on Feb 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 364
/
Copy pathpre-commit-hook.mjs
153 lines (134 loc) · 3.87 KB
/
pre-commit-hook.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
* External dependencies
*/
import { execSync, spawnSync } from 'child_process';
import { existsSync } from 'fs';
import chalk from 'chalk';
import path from 'path';
import _ from 'lodash';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
const __filename = fileURLToPath( import.meta.url );
const __dirname = dirname( __filename );
/*
* 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' );
const phpcbfPath = getPathForCommand( 'phpcbf' );
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() )
.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',
} );
} 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;
}
function phpcbfInstalled() {
if ( existsSync( phpcbfPath ) ) {
return true;
}
return false;
}
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 );
}
// determine if PHPCS is available
const phpcs = phpcsInstalled();
// determine if PHPCBF is available
const phpcbf = phpcbfInstalled();
// 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 ( phpFiles.length ) {
const phpcbfResult = spawnSync( phpcbfPath, [ ...phpFiles ], {
shell: true,
stdio: 'inherit',
} );
if ( phpcbfResult && phpcbfResult.status ) {
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', ...phpFiles ],
{
shell: true,
stdio: 'inherit',
}
);
if ( lintResult.status ) {
linterFailure();
}
}
}
// 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( ' ' ) }` );
}