-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Scripts: Add Prettier formatting script
- Loading branch information
Showing
6 changed files
with
115 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Import the default config file and expose it in the project root. | ||
// Useful for editor integrations. | ||
module.exports = require( '@wordpress/scripts/config/.prettierrc.js' ); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module.exports = { | ||
useTabs: true, | ||
tabWidth: 2, | ||
printWidth: 100, | ||
singleQuote: true, | ||
trailingComma: 'es5', | ||
bracketSpacing: true, | ||
parenSpacing: true, | ||
jsxBracketSameLine: false, | ||
semi: true, | ||
arrowParens: 'always', | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
const { sync: spawn } = require( 'cross-spawn' ); | ||
const { sync: resolveBin } = require( 'resolve-bin' ); | ||
const { sync: dirGlob } = require( 'dir-glob' ); | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
const { | ||
fromConfigRoot, | ||
fromProjectRoot, | ||
getArgFromCLI, | ||
getFileArgsFromCLI, | ||
hasArgInCLI, | ||
hasPackageProp, | ||
hasProjectFile, | ||
} = require( '../utils' ); | ||
|
||
// See: https://prettier.io/docs/en/configuration.html | ||
const hasProjectPrettierConfig = | ||
hasProjectFile( '.prettierrc' ) || | ||
hasProjectFile( '.prettierrc.json' ) || | ||
hasProjectFile( '.prettierrc.yaml' ) || | ||
hasProjectFile( '.prettierrc.yml' ) || | ||
hasProjectFile( '.prettierrc.js' ) || | ||
hasProjectFile( '.prettierrc.config.js' ) || | ||
hasProjectFile( '.prettierrc.toml' ) || | ||
hasPackageProp( 'prettier' ); | ||
|
||
// Get the configuration file, with preference highest to lowers: | ||
// 1. use the `--config` arg from CLI | ||
// 2. use configuration provided by the project | ||
// 3. use the default provided in the scripts module | ||
let configArgs; | ||
const configFromCLI = getArgFromCLI( '--config' ); | ||
if ( configFromCLI ) { | ||
configArgs = [ '--config', configFromCLI ]; | ||
} else if ( hasProjectPrettierConfig ) { | ||
configArgs = []; // no `--config` when project configuration is found: Prettier will find it | ||
} else { | ||
configArgs = [ '--config', fromConfigRoot( '.prettierrc.js' ) ]; | ||
} | ||
|
||
// 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' ] } ); | ||
|
||
const result = spawn( | ||
resolveBin( 'prettier' ), | ||
[ '--write', ...configArgs, ...ignoreArgs, ...pragmaArgs, ...globArgs ], | ||
{ stdio: 'inherit' } | ||
); | ||
|
||
process.exit( result.status ); |