|
1 | 1 | /**
|
2 | 2 | * External dependencies
|
3 | 3 | */
|
4 |
| -const path = require( 'path' ); |
5 |
| -const micromatch = require( 'micromatch' ); |
| 4 | +const fs = require( 'fs' ); |
6 | 5 |
|
7 | 6 | /**
|
8 | 7 | * Internal dependencies
|
9 | 8 | */
|
10 | 9 | const { plugins } = require( './plugins.json' );
|
11 | 10 |
|
12 | 11 | /**
|
13 |
| - * Join and escape filenames for shell. |
14 |
| - * |
15 |
| - * @param {string[]} files Files to join. |
16 |
| - * |
17 |
| - * @return {string} Joined files. |
| 12 | + * @type {import('lint-staged').Configuration} |
18 | 13 | */
|
19 |
| -const joinFiles = ( files ) => { |
20 |
| - return files.map( ( file ) => `'${ file }'` ).join( ' ' ); |
| 14 | +const config = { |
| 15 | + '**/*.{js,ts,mjs}': [ 'npm run lint-js', () => 'npm run tsc' ], |
| 16 | + '**/*.php': () => 'composer phpstan', |
| 17 | + '*.php': 'composer lint', |
| 18 | + '/tools/**.php': 'composer lint', |
| 19 | + // Note: Instead of the preceding two lines, the following line was tried but it is not working: |
| 20 | + // [ `!(plugins/{${ plugins.join( '|' ) }})/**/*.php` ]: 'composer lint', |
21 | 21 | };
|
22 | 22 |
|
23 |
| -// Get plugin base name to match regex more accurately. |
24 |
| -// Else it can cause issues when this plugin is placed in `wp-content/plugins` directory. |
25 |
| -const PLUGIN_BASE_NAME = path.basename( __dirname ); |
| 23 | +for ( const plugin of plugins ) { |
| 24 | + const phpcsConfig = fs.existsSync( `plugins/${ plugin }/phpcs.xml` ) |
| 25 | + ? `plugins/${ plugin }/phpcs.xml` |
| 26 | + : `plugins/${ plugin }/phpcs.xml.dist`; |
| 27 | + config[ |
| 28 | + `plugins/${ plugin }/**/*.php` |
| 29 | + ] = `composer lint -- --standard=${ phpcsConfig }`; |
| 30 | +} |
26 | 31 |
|
27 |
| -module.exports = { |
28 |
| - '**/*.{js,ts}': ( files ) => { |
29 |
| - return [ `npm run lint-js -- ${ joinFiles( files ) }`, `npm run tsc` ]; |
30 |
| - }, |
31 |
| - '**/*.php': ( files ) => { |
32 |
| - const commands = [ 'composer phpstan' ]; |
33 |
| - |
34 |
| - plugins.forEach( ( plugin ) => { |
35 |
| - const pluginFiles = micromatch( |
36 |
| - files, |
37 |
| - `**/${ PLUGIN_BASE_NAME }/plugins/${ plugin }/**`, |
38 |
| - { dot: true } |
39 |
| - ); |
40 |
| - |
41 |
| - if ( pluginFiles.length ) { |
42 |
| - // Note: The lint command has to be used directly because the plugin-specific lint command includes the entire plugin directory as an argument. |
43 |
| - commands.push( |
44 |
| - `composer lint -- --standard=./plugins/${ plugin }/phpcs.xml.dist ${ joinFiles( |
45 |
| - pluginFiles |
46 |
| - ) }` |
47 |
| - ); |
48 |
| - } |
49 |
| - } ); |
50 |
| - |
51 |
| - const otherFiles = micromatch( |
52 |
| - files, |
53 |
| - `!**/${ PLUGIN_BASE_NAME }/plugins/**`, |
54 |
| - { dot: true } |
55 |
| - ); |
56 |
| - |
57 |
| - if ( otherFiles.length ) { |
58 |
| - commands.push( `composer lint -- ${ joinFiles( otherFiles ) }` ); |
59 |
| - } |
60 |
| - |
61 |
| - return commands; |
62 |
| - }, |
63 |
| -}; |
| 32 | +module.exports = config; |
0 commit comments