Add prettier script
`scripts/prettier/index.js write` will run prettier on source files. Run using `yarn prettier`. `scripts/prettier/index.js` will throw if any source files are not formatted with prettier. We'll use this to block CI. Based on similar script in Jest repo.
Andrew Clark committed
Mar 15, 2017 at 14:20 UTC
f365e52dd7946d6eeff927623b13b68ec2468583
3 files changed
+112
-1
package.json
+2
-1
@@ -37,6 +37,7 @@
37
"babylon": "6.15.0",
38
"browserify": "^13.0.0",
39
"bundle-collapser": "^1.1.1",
40
+ "chalk": "^1.1.3",
41
"coffee-script": "^1.8.0",
42
"core-js": "^2.2.1",
43
"coveralls": "^2.11.6",
@@ -94,7 +95,7 @@
95
"postinstall": "node node_modules/fbjs-scripts/node/check-dev-engines.js package.json",
96
"test": "jest",
97
"flow": "flow",
97
- "prettier": "prettier --write --no-bracket-spacing --single-quote --jsx-bracket-same-line --trailing-comma all --print-width 80 \"src/**/!(third_party)/*.js\""
98
+ "prettier": "node ./scripts/prettier/index.js write"
99
},
100
"jest": {
101
"modulePathIgnorePatterns": [
scripts/prettier/index.js
new
+70
@@ -0,0 +1,70 @@
1
+/**
2
+ * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3
+ *
4
+ * This source code is licensed under the BSD-style license found in the
5
+ * LICENSE file in the root directory of this source tree. An additional grant
6
+ * of patent rights can be found in the PATENTS file in the same directory.
7
+ */
8
+'use strict';
9
+
10
+// Based on similar script in Jest
11
+// https://github.com/facebook/jest/blob/master/scripts/prettier.js
12
+
13
+const chalk = require('chalk');
14
+const glob = require('glob');
15
+const path = require('path');
16
+const runCommand = require('./runCommand');
17
+
18
+const shouldWrite = process.argv[2] === 'write';
19
+const isWindows = process.platform === 'win32';
20
+const prettier = isWindows ? 'prettier.cmd' : 'prettier';
21
+const prettierCmd = path.resolve(__dirname, '../../node_modules/.bin/' + prettier);
22
+const defaultOptions = {
23
+ 'bracket-spacing': 'false',
24
+ 'single-quote': 'true',
25
+ 'jsx-bracket-same-line': 'true',
26
+ 'trailing-comma': 'all',
27
+ 'print-width': 80,
28
+};
29
+const config = {
30
+ default: {
31
+ patterns: ['src/**/*.js'],
32
+ ignore: [
33
+ '**/third_party/**',
34
+ '**/node_modules/**',
35
+ ],
36
+ },
37
+};
38
+
39
+Object.keys(config).forEach(key => {
40
+ const patterns = config[key].patterns;
41
+ const options = config[key].options;
42
+ const ignore = config[key].ignore;
43
+
44
+ const globPattern = patterns.length > 1
45
+ ? `{${patterns.join(',')}}`
46
+ : `${patterns.join(',')}`;
47
+ const files = glob.sync(globPattern, {ignore});
48
+
49
+ const args = Object.keys(defaultOptions).map(
50
+ k => `--${k}=${(options && options[k]) || defaultOptions[k]}`
51
+ );
52
+ args.push(`--${shouldWrite ? 'write' : 'l'} {${files.join(' ')}}`);
53
+
54
+ try {
55
+ runCommand(prettierCmd, args.join(' '), path.resolve(__dirname, '../..'));
56
+ } catch (e) {
57
+ console.log(e);
58
+ if (!shouldWrite) {
59
+ console.log(
60
+ chalk.red(
61
+ ` This project uses prettier to format all JavaScript code.\n`
62
+ ) +
63
+ chalk.dim(` Please run `) +
64
+ chalk.reset('yarn prettier') +
65
+ chalk.dim(` and add changes to files listed above to your commit.`) +
66
+ `\n`
67
+ );
68
+ }
69
+ }
70
+});
scripts/prettier/runCommand.js
new
+40
@@ -0,0 +1,40 @@
1
+/**
2
+ * Copyright (c) 2014, Facebook, Inc. All rights reserved.
3
+ *
4
+ * This source code is licensed under the BSD-style license found in the
5
+ * LICENSE file in the root directory of this source tree. An additional grant
6
+ * of patent rights can be found in the PATENTS file in the same directory.
7
+ */
8
+'use strict';
9
+
10
+const chalk = require('chalk');
11
+const spawn = require('child_process').spawnSync;
12
+
13
+module.exports = function runCommand(cmd, args, cwd) {
14
+ if (!cwd) {
15
+ cwd = __dirname;
16
+ }
17
+
18
+ const callArgs = args.split(' ');
19
+ console.log(
20
+ chalk.dim('$ cd ' + cwd) +
21
+ '\n' +
22
+ chalk.dim(
23
+ ' $ ' +
24
+ cmd +
25
+ ' ' +
26
+ (args.length > 1000 ? args.slice(0, 1000) + '...' : args)
27
+ ) +
28
+ '\n'
29
+ );
30
+ const result = spawn(cmd, callArgs, {
31
+ cwd,
32
+ stdio: 'inherit',
33
+ });
34
+ if (result.error || result.status !== 0) {
35
+ const message = 'Error running command.';
36
+ const error = new Error(message);
37
+ error.stack = message;
38
+ throw error;
39
+ }
40
+};