Use prettier api (#11458)
* use prettier-api * fix string to boolean * fix eslint * fix typo * cleanup * use object assign
Joe Lim committed
Nov 5, 2017 at 13:06 UTC
40fbed572112a4ac90566fc822e33eb8cb4986c3
1 file changed
+41
-36
scripts/prettier/index.js
+41
-36
@@ -11,25 +11,20 @@
11
12
const chalk = require('chalk');
13
const glob = require('glob');
14
-const path = require('path');
14
const execFileSync = require('child_process').execFileSync;
15
+const prettier = require('prettier');
16
+const fs = require('fs');
17
18
const mode = process.argv[2] || 'check';
19
const shouldWrite = mode === 'write' || mode === 'write-changed';
20
const onlyChanged = mode === 'check-changed' || mode === 'write-changed';
21
21
-const isWindows = process.platform === 'win32';
22
-const prettier = isWindows ? 'prettier.cmd' : 'prettier';
23
-const prettierCmd = path.resolve(
24
- __dirname,
25
- '../../node_modules/.bin/' + prettier
26
-);
22
const defaultOptions = {
28
- 'bracket-spacing': 'false',
29
- 'single-quote': 'true',
30
- 'jsx-bracket-same-line': 'true',
31
- 'trailing-comma': 'all',
32
- 'print-width': 80,
23
+ bracketSpacing: false,
24
+ singleQuote: true,
25
+ jsxBracketSameLine: true,
26
+ trailingComma: 'all',
27
+ printWidth: 80,
28
};
29
const config = {
30
default: {
@@ -56,7 +51,7 @@ const config = {
51
'scripts/bench/benchmarks/**',
52
],
53
options: {
59
- 'trailing-comma': 'es5',
54
+ trailingComma: 'es5',
55
},
56
},
57
};
@@ -83,6 +78,7 @@ var changedFiles = new Set(
78
]).match(/[^\0]+/g)
79
);
80
81
+let didWarn = false;
82
Object.keys(config).forEach(key => {
83
const patterns = config[key].patterns;
84
const options = config[key].options;
@@ -99,28 +95,37 @@ Object.keys(config).forEach(key => {
95
return;
96
}
97
102
- const args = Object.keys(defaultOptions).map(
103
- k => `--${k}=${(options && options[k]) || defaultOptions[k]}`
104
- );
105
- args.push(`--${shouldWrite ? 'write' : 'l'}`);
106
-
107
- try {
108
- exec(prettierCmd, [...args, ...files]).trim();
109
- } catch (e) {
110
- if (!shouldWrite) {
111
- console.log(
112
- '\n' +
113
- chalk.red(
114
- ` This project uses prettier to format all JavaScript code.\n`
115
- ) +
116
- chalk.dim(` Please run `) +
117
- chalk.reset('yarn prettier-all') +
118
- chalk.dim(` and add changes to files listed below to your commit:`) +
119
- `\n\n` +
120
- e.stdout
121
- );
122
- process.exit(1);
98
+ const args = Object.assign({}, defaultOptions, options);
99
+ files.forEach(file => {
100
+ const input = fs.readFileSync(file, 'utf8');
101
+ if (shouldWrite) {
102
+ const output = prettier.format(input, args);
103
+ if (output !== input) {
104
+ fs.writeFileSync(file, output, 'utf8');
105
+ }
106
+ } else {
107
+ if (!prettier.check(input, args)) {
108
+ if (!didWarn) {
109
+ console.log(
110
+ '\n' +
111
+ chalk.red(
112
+ ` This project uses prettier to format all JavaScript code.\n`
113
+ ) +
114
+ chalk.dim(` Please run `) +
115
+ chalk.reset('yarn prettier-all') +
116
+ chalk.dim(
117
+ ` and add changes to files listed below to your commit:`
118
+ ) +
119
+ `\n\n`
120
+ );
121
+ didWarn = true;
122
+ }
123
+ console.log(file);
124
+ }
125
}
124
- throw e;
125
- }
126
+ });
127
});
128
+
129
+if (didWarn) {
130
+ process.exit(1);
131
+}