Skip to content

Commit

Permalink
Scripts: Add Prettier formatting script
Browse files Browse the repository at this point in the history
  • Loading branch information
jsnajdr committed Oct 23, 2019
1 parent ebb304c commit 546524c
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .prettierrc.js
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' );
23 changes: 21 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@
"fixtures:server-registered": "wp-scripts env docker-run php ./bin/get-server-blocks.php > test/integration/full-content/server-registered.json",
"fixtures:generate": "npm run fixtures:server-registered && cross-env GENERATE_MISSING_FIXTURES=y npm run test-unit",
"fixtures:regenerate": "npm run fixtures:clean && npm run fixtures:generate",
"format-js": "wp-scripts format-js",
"lint": "concurrently \"npm run lint-js\" \"npm run lint-pkg-json\" \"npm run lint-css\" \"npm run lint-types\"",
"lint-js": "wp-scripts lint-js",
"lint-js:fix": "npm run lint-js -- --fix",
Expand Down
12 changes: 12 additions & 0 deletions packages/scripts/config/.prettierrc.js
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',
};
2 changes: 2 additions & 0 deletions packages/scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@
"command-exists": "^1.2.8",
"cross-spawn": "^5.1.0",
"decompress-zip": "^0.2.2",
"dir-glob": "^3.0.1",
"eslint": "^6.1.0",
"jest": "^24.7.1",
"jest-puppeteer": "^4.3.0",
"js-yaml": "^3.13.1",
"lodash": "^4.17.15",
"minimist": "^1.2.0",
"npm-package-json-lint": "^4.0.2",
"prettier": "npm:wp-prettier@^1.18.2",
"puppeteer": "^1.20.0",
"read-pkg-up": "^1.0.1",
"request": "^2.88.0",
Expand Down
76 changes: 76 additions & 0 deletions packages/scripts/scripts/format-js.js
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 );

0 comments on commit 546524c

Please sign in to comment.