From 3b2bbf4f03cbacc6a95a4ef10a7be5695b0dfc48 Mon Sep 17 00:00:00 2001 From: Michael LaCroix Date: Tue, 13 Jun 2017 11:41:20 -0500 Subject: [PATCH] Add a Prettier task (#88) * Add a Prettier task * Add docs for setting up Prettier in an app * Update Prettier readme * Update Prettier docs * Don't write files with Prettier by default * Upgrade Prettier * Bump minor version 7.3.0 --- MIGRATION_GUIDE.md => docs/MIGRATION_GUIDE.md | 0 docs/PRETTIER.md | 11 ++++++ packages/react-scripts/bin/react-scripts.js | 1 + packages/react-scripts/package.json | 10 ++++- packages/react-scripts/scripts/prettier.js | 39 +++++++++++++++++++ 5 files changed, 59 insertions(+), 2 deletions(-) rename MIGRATION_GUIDE.md => docs/MIGRATION_GUIDE.md (100%) create mode 100644 docs/PRETTIER.md create mode 100644 packages/react-scripts/scripts/prettier.js diff --git a/MIGRATION_GUIDE.md b/docs/MIGRATION_GUIDE.md similarity index 100% rename from MIGRATION_GUIDE.md rename to docs/MIGRATION_GUIDE.md diff --git a/docs/PRETTIER.md b/docs/PRETTIER.md new file mode 100644 index 00000000000..b98c8399f04 --- /dev/null +++ b/docs/PRETTIER.md @@ -0,0 +1,11 @@ +# Prettier + +## Adding Prettier to an App + +Prettier is available in @trunkclub/build version 7.3 and above. + +### Usage + +```bash +yarn tcweb-build prettier -- --write "{src,spec}/**/*.{js,jsx,es6}" +``` diff --git a/packages/react-scripts/bin/react-scripts.js b/packages/react-scripts/bin/react-scripts.js index 51e66c57308..fea51c13a19 100755 --- a/packages/react-scripts/bin/react-scripts.js +++ b/packages/react-scripts/bin/react-scripts.js @@ -25,6 +25,7 @@ function run (s) { case 'test': case 'build-module': case 'lint': + case 'prettier': case 'publish': case 'deploy': var result = spawn.sync( diff --git a/packages/react-scripts/package.json b/packages/react-scripts/package.json index e4071ed64f0..da18ffa29cf 100644 --- a/packages/react-scripts/package.json +++ b/packages/react-scripts/package.json @@ -1,6 +1,6 @@ { "name": "@trunkclub/build", - "version": "7.2.0", + "version": "7.3.0", "upstream-version": "0.9.5", "description": "Configuration and scripts for Create React App. Fork maintained by Trunk Club", "repository": "trunkclub/create-react-app", @@ -71,6 +71,7 @@ "object-assign": "4.1.1", "path-exists": "2.1.0", "postcss-loader": "1.2.2", + "prettier": "^1.4.4", "progress-bar-webpack-plugin": "1.9.0", "promise": "7.1.1", "recursive-readdir": "2.1.0", @@ -89,11 +90,16 @@ "devDependencies": { "babel-runtime": "^6.11.6", "bundle-deps": "1.0.0", - "flow-runtime": "^0.12.0", + "husky": "^0.13.3", + "lint-staged": "^3.4.1", "publish": "^0.6.0", "react": "^15.3.0", "react-dom": "^15.3.0" }, + "peerDependencies": { + "husky": "^0.13.3", + "lint-staged": "^3.4.1" + }, "optionalDependencies": { "fsevents": "1.0.17" } diff --git a/packages/react-scripts/scripts/prettier.js b/packages/react-scripts/scripts/prettier.js new file mode 100644 index 00000000000..1a112bd1494 --- /dev/null +++ b/packages/react-scripts/scripts/prettier.js @@ -0,0 +1,39 @@ +'use strict'; + +var path = require('path'); +var spawn = require('cross-spawn'); + +// First two args will always be: +// - node +// - path/to/prettier.js (this file) +var argsIndex = process.argv[2] === '--' ? 3 : 2; +var args = process.argv.slice(argsIndex); + +var result = spawn.sync( + path.resolve(__dirname, '../node_modules/.bin/prettier'), + [ + '--single-quote', + '--no-semi', + '--trailing-comma', 'all', + ].concat(args), + { stdio: 'inherit' } +); + +if (result.signal) { + if (result.signal === 'SIGKILL') { + console.log( + 'The prettier task failed because the process exited too early. ' + + 'This probably means the system ran out of memory or someone called ' + + '`kill -9` on the process.' + ); + } else if (result.signal === 'SIGTERM') { + console.log( + 'The prettier task failed because the process exited too early. ' + + 'Someone might have called `kill` or `killall`, or the system could ' + + 'be shutting down.' + ); + } + process.exit(1); +} + +process.exit(result.status);