-
Notifications
You must be signed in to change notification settings - Fork 101
/
lint-staged.config.js
63 lines (54 loc) · 1.51 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
62
63
/**
* 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,ts}': ( files ) => {
return [ `npm run lint-js -- ${ joinFiles( files ) }`, `npm run tsc` ];
},
'**/*.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;
},
};