forked from WordPress/performance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lint-staged.config.js
61 lines (52 loc) · 1.48 KB
/
lint-staged.config.js
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
/**
* External dependencies
*/
const path = require( 'path' );
const micromatch = require( 'micromatch' );
/**
* Internal dependencies
*/
const { plugins } = require( './plugins.json' );
/**
* Join and escape filenames for shell.
*
* @param {string[]} files Files to join.
*
* @return {string} Joined files.
*/
const joinFiles = ( files ) => {
return files.map( ( file ) => `'${ file }'` ).join( ' ' );
};
// Get plugin base name to match regex more accurately.
// Else it can cause issues when this plugin is placed in `wp-content/plugins` directory.
const PLUGIN_BASE_NAME = path.basename( __dirname );
module.exports = {
'**/*.js': ( files ) => `npm run lint-js -- ${ joinFiles( files ) }`,
'**/*.php': ( files ) => {
const commands = [ 'composer phpstan' ];
plugins.forEach( ( plugin ) => {
const pluginFiles = micromatch(
files,
`**/${ PLUGIN_BASE_NAME }/plugins/${ plugin }/**`,
{ dot: true }
);
if ( pluginFiles.length ) {
// Note: The lint command has to be used directly because the plugin-specific lint command includes the entire plugin directory as an argument.
commands.push(
`composer lint -- --standard=./plugins/${ plugin }/phpcs.xml.dist ${ joinFiles(
pluginFiles
) }`
);
}
} );
const otherFiles = micromatch(
files,
`!**/${ PLUGIN_BASE_NAME }/plugins/**`,
{ dot: true }
);
if ( otherFiles.length ) {
commands.push( `composer lint -- ${ joinFiles( otherFiles ) }` );
}
return commands;
},
};