-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Scripts: Include YAML files in prettification (#30240)
`prettier` has been able to [format YAML files since version 1.14](https://prettier.io/blog/2018/07/29/1.14.0.html) (released in July of 2018). We're starting to size up on YAML files (mostly GitHub Actions workflows), so it'd be nice to have them auto-formatted. This PR adds YAML files to the list of extensions formatted by `prettier`. Existing YAML files have already been updated to the new format by #30409. Co-authored-by: Greg Ziółkowski <grzegorz@gziolo.pl>
- Loading branch information
Showing
21 changed files
with
178 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,116 +1,36 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
const { exit, stdout } = require( 'process' ); | ||
|
||
/** | ||
* External dependencies | ||
*/ | ||
const chalk = require( 'chalk' ); | ||
const { sync: spawn } = require( 'cross-spawn' ); | ||
const { sync: resolveBin } = require( 'resolve-bin' ); | ||
const { sync: dirGlob } = require( 'dir-glob' ); | ||
const { sync: readPkgUp } = require( 'read-pkg-up' ); | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
const { | ||
fromConfigRoot, | ||
fromProjectRoot, | ||
getArgFromCLI, | ||
getFileArgsFromCLI, | ||
hasArgInCLI, | ||
hasPrettierConfig, | ||
hasProjectFile, | ||
} = require( '../utils' ); | ||
|
||
// Check if the project has wp-prettier installed and if the project has a Prettier config | ||
function checkPrettier() { | ||
try { | ||
const prettierResolvePath = require.resolve( 'prettier' ); | ||
const prettierPackageJson = readPkgUp( { cwd: prettierResolvePath } ); | ||
const prettierPackageName = prettierPackageJson.pkg.name; | ||
|
||
if ( | ||
! [ 'wp-prettier', '@wordpress/prettier' ].includes( | ||
prettierPackageName | ||
) | ||
) { | ||
return { | ||
success: false, | ||
message: | ||
chalk.red( | ||
'Incompatible version of Prettier was found in your project\n' | ||
) + | ||
"You need to install the 'wp-prettier' package to get " + | ||
'code formatting compliant with the WordPress coding standards.\n\n', | ||
}; | ||
} | ||
} catch { | ||
return { | ||
success: false, | ||
message: | ||
chalk.red( | ||
"The 'prettier' package was not found in your project\n" | ||
) + | ||
"You need to install the 'wp-prettier' package under an alias to get " + | ||
'code formatting compliant with the WordPress coding standards.\n\n', | ||
}; | ||
} | ||
|
||
return { success: true }; | ||
} | ||
|
||
const checkResult = checkPrettier(); | ||
if ( ! checkResult.success ) { | ||
stdout.write( checkResult.message ); | ||
exit( 1 ); | ||
} | ||
|
||
// Check for existing config in project, if it exists no command-line args are | ||
// needed for config, otherwise pass in args to default config in packages | ||
// See: https://prettier.io/docs/en/configuration.html | ||
let configArgs = []; | ||
if ( ! hasPrettierConfig() ) { | ||
configArgs = [ | ||
'--config', | ||
require.resolve( '@wordpress/prettier-config' ), | ||
]; | ||
} | ||
|
||
// If `--ignore-path` is not explicitly specified, use the project's or global .eslintignore | ||
let ignorePath = getArgFromCLI( '--ignore-path' ); | ||
if ( ! ignorePath ) { | ||
if ( hasProjectFile( '.eslintignore' ) ) { | ||
ignorePath = fromProjectRoot( '.eslintignore' ); | ||
} else { | ||
ignorePath = fromConfigRoot( '.eslintignore' ); | ||
} | ||
} | ||
const ignoreArgs = [ '--ignore-path', ignorePath ]; | ||
|
||
// forward the --require-pragma option that formats only files that already have the @format | ||
// pragma in the first docblock. | ||
const pragmaArgs = hasArgInCLI( '--require-pragma' ) | ||
? [ '--require-pragma' ] | ||
: []; | ||
|
||
// Get the files and directories to format and convert them to globs | ||
let fileArgs = getFileArgsFromCLI(); | ||
if ( fileArgs.length === 0 ) { | ||
fileArgs = [ '.' ]; | ||
} | ||
|
||
// Converts `foo/bar` directory to `foo/bar/**/*.js` | ||
const globArgs = dirGlob( fileArgs, { | ||
extensions: [ 'js', 'jsx', 'ts', 'tsx' ], | ||
} ); | ||
|
||
const result = spawn( | ||
resolveBin( 'prettier' ), | ||
[ '--write', ...configArgs, ...ignoreArgs, ...pragmaArgs, ...globArgs ], | ||
{ stdio: 'inherit' } | ||
); | ||
|
||
process.exit( result.status ); | ||
const { getNodeArgsFromCLI, spawnScript } = require( '../utils' ); | ||
|
||
const { scriptArgs, nodeArgs } = getNodeArgsFromCLI(); | ||
|
||
const keypress = async () => { | ||
process.stdin.setRawMode( true ); | ||
return new Promise( ( resolve ) => | ||
process.stdin.once( 'data', () => { | ||
process.stdin.setRawMode( false ); | ||
resolve(); | ||
} ) | ||
); | ||
}; | ||
|
||
( async () => { | ||
const message = | ||
`Please note that the ${ chalk.red( | ||
'format-js' | ||
) } script has been renamed to ${ chalk.green( 'format' ) }.\n` + | ||
"If you're calling it from any of your own scripts, please update them accordingly.\n" + | ||
'Press any key to continiue.'; | ||
|
||
// eslint-disable-next-line no-console | ||
console.log( message ); | ||
|
||
await keypress(); | ||
spawnScript( 'format', scriptArgs, nodeArgs ); | ||
} )(); |
Oops, something went wrong.