From c5395d23e54600665af696d272eece34c4e55605 Mon Sep 17 00:00:00 2001 From: Andrew Clark Date: Thu, 16 Mar 2017 11:55:41 -0700 Subject: [PATCH] Merge pull request #9187 from acdlite/prettierci Failing to run prettier should block CI (cherry picked from commit 9549ebfae52332a3f62ce4a13e7b050cb984c77e) --- .github/PULL_REQUEST_TEMPLATE.md | 7 ++- docs/contributing/how-to-contribute.md | 7 ++- package.json | 4 +- scripts/circleci/test_entry_point.sh | 1 + scripts/prettier/index.js | 78 ++++++++++++++++++++++++++ 5 files changed, 90 insertions(+), 7 deletions(-) create mode 100644 scripts/prettier/index.js diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index 4a2bf79a20..5de9012e12 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -5,6 +5,7 @@ 3. If you've changed APIs, update the documentation. 4. Ensure the test suite passes (`npm test`). 5. Make sure your code lints (`npm run lint`). -6. Run the [Flow](https://flowtype.org/) typechecks (`npm run flow`). -7. If you added or removed any tests, run `./scripts/fiber/record-tests` before submitting the pull request, and commit the resulting changes. -8. If you haven't already, complete the [CLA](https://code.facebook.com/cla). +6. Format your code with [prettier](https://github.com/prettier/prettier) (`npm run prettier`). +7. Run the [Flow](https://flowtype.org/) typechecks (`npm run flow`). +8. If you added or removed any tests, run `./scripts/fiber/record-tests` before submitting the pull request, and commit the resulting changes. +9. If you haven't already, complete the CLA. diff --git a/docs/contributing/how-to-contribute.md b/docs/contributing/how-to-contribute.md index 9be878c237..6613a3c74b 100644 --- a/docs/contributing/how-to-contribute.md +++ b/docs/contributing/how-to-contribute.md @@ -81,9 +81,10 @@ The core team is monitoring for pull requests. We will review your pull request 3. If you've changed APIs, update the documentation. 4. Ensure the test suite passes (`npm test`). 5. Make sure your code lints (`npm run lint`). -6. Run the [Flow](https://flowtype.org/) typechecks (`npm run flow`). -7. If you added or removed any tests, run `./scripts/fiber/record-tests` before submitting the pull request, and commit the resulting changes. -8. If you haven't already, complete the CLA. +6. Format your code with [prettier](https://github.com/prettier/prettier) (`npm run prettier`). +7. Run the [Flow](https://flowtype.org/) typechecks (`npm run flow`). +8. If you added or removed any tests, run `./scripts/fiber/record-tests` before submitting the pull request, and commit the resulting changes. +9. If you haven't already, complete the CLA. ### Contributor License Agreement (CLA) diff --git a/package.json b/package.json index e79c527877..509b80197d 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,7 @@ "babylon": "6.15.0", "browserify": "^13.0.0", "bundle-collapser": "^1.1.1", + "chalk": "^1.1.3", "coffee-script": "^1.8.0", "core-js": "^2.2.1", "coveralls": "^2.11.6", @@ -90,7 +91,8 @@ "lint": "grunt lint", "postinstall": "node node_modules/fbjs-scripts/node/check-dev-engines.js package.json", "test": "jest", - "flow": "flow" + "flow": "flow", + "prettier": "node ./scripts/prettier/index.js write" }, "jest": { "modulePathIgnorePatterns": [ diff --git a/scripts/circleci/test_entry_point.sh b/scripts/circleci/test_entry_point.sh index 2f9c042f7e..4788827df3 100755 --- a/scripts/circleci/test_entry_point.sh +++ b/scripts/circleci/test_entry_point.sh @@ -23,6 +23,7 @@ fi # These seem out of order but extract-errors must be run after jest. if [ $((0 % CIRCLE_NODE_TOTAL)) -eq "$CIRCLE_NODE_INDEX" ]; then COMMANDS_TO_RUN+=('./node_modules/.bin/gulp lint') + COMMANDS_TO_RUN+=('node ./scripts/prettier/index') COMMANDS_TO_RUN+=('./node_modules/.bin/gulp flow') COMMANDS_TO_RUN+=('./node_modules/.bin/grunt build') COMMANDS_TO_RUN+=('./scripts/circleci/test_extract_errors.sh') diff --git a/scripts/prettier/index.js b/scripts/prettier/index.js new file mode 100644 index 0000000000..85af4bcf87 --- /dev/null +++ b/scripts/prettier/index.js @@ -0,0 +1,78 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ +'use strict'; + +// Based on similar script in Jest +// https://github.com/facebook/jest/blob/master/scripts/prettier.js + +const chalk = require('chalk'); +const glob = require('glob'); +const path = require('path'); +const execFileSync = require('child_process').execFileSync; + +const shouldWrite = process.argv[2] === 'write'; +const isWindows = process.platform === 'win32'; +const prettier = isWindows ? 'prettier.cmd' : 'prettier'; +const prettierCmd = path.resolve(__dirname, '../../node_modules/.bin/' + prettier); +const defaultOptions = { + 'bracket-spacing': 'false', + 'single-quote': 'true', + 'jsx-bracket-same-line': 'true', + 'trailing-comma': 'all', + 'print-width': 80, +}; +const config = { + default: { + patterns: ['src/**/*.js'], + ignore: [ + '**/third_party/**', + '**/node_modules/**', + ], + }, +}; + +function exec(command, args) { + console.log('> ' + [command].concat(args).join(' ')); + var options = {}; + return execFileSync(command, args, options).toString(); +} + +Object.keys(config).forEach(key => { + const patterns = config[key].patterns; + const options = config[key].options; + const ignore = config[key].ignore; + + const globPattern = patterns.length > 1 + ? `{${patterns.join(',')}}` + : `${patterns.join(',')}`; + const files = glob.sync(globPattern, {ignore}); + + const args = Object.keys(defaultOptions).map( + k => `--${k}=${(options && options[k]) || defaultOptions[k]}` + ); + args.push(`--${shouldWrite ? 'write' : 'l'}`); + + try { + exec(prettierCmd, [...args, ...files]); + } catch (e) { + if (!shouldWrite) { + console.log( + '\n' + + chalk.red( + ` This project uses prettier to format all JavaScript code.\n` + ) + + chalk.dim(` Please run `) + + chalk.reset('yarn prettier') + + chalk.dim(` and add changes to files listed above to your commit.`) + + `\n` + ); + process.exit(1); + } + throw e; + } +});