Use Prettier Config API (#11980)
Lucas Azzola committed
Jan 7, 2018 at 22:51 UTC
052a5f27f35f453ee33a075df81bddf9ced7300a
2 files changed
+59
-72
.prettierrc.js
new
+18
@@ -0,0 +1,18 @@
1
+const {esNextPaths} = require('./scripts/shared/pathsByLanguageVersion');
2
+
3
+module.exports = {
4
+ bracketSpacing: false,
5
+ singleQuote: true,
6
+ jsxBracketSameLine: true,
7
+ trailingComma: 'es5',
8
+ printWidth: 80,
9
+
10
+ overrides: [
11
+ {
12
+ files: esNextPaths,
13
+ options: {
14
+ trailingComma: 'all',
15
+ },
16
+ },
17
+ ],
18
+};
scripts/prettier/index.js
+41
-72
@@ -7,98 +7,67 @@
7
'use strict';
8
9
// Based on similar script in Jest
10
-// https://github.com/facebook/jest/blob/master/scripts/prettier.js
10
+// https://github.com/facebook/jest/blob/a7acc5ae519613647ff2c253dd21933d6f94b47f/scripts/prettier.js
11
12
const chalk = require('chalk');
13
const glob = require('glob');
14
const prettier = require('prettier');
15
const fs = require('fs');
16
const listChangedFiles = require('../shared/listChangedFiles');
17
-const {esNextPaths} = require('../shared/pathsByLanguageVersion');
17
+const prettierConfigPath = require.resolve('../../.prettierrc');
18
19
const mode = process.argv[2] || 'check';
20
const shouldWrite = mode === 'write' || mode === 'write-changed';
21
const onlyChanged = mode === 'check-changed' || mode === 'write-changed';
22
23
-const defaultOptions = {
24
- bracketSpacing: false,
25
- singleQuote: true,
26
- jsxBracketSameLine: true,
27
- trailingComma: 'all',
28
- printWidth: 80,
29
-};
30
-const config = {
31
- default: {
32
- patterns: ['**/*.js'],
33
- ignore: [
34
- '**/node_modules/**',
35
- // ESNext paths can have trailing commas.
36
- // We'll handle them separately below.
37
- ...esNextPaths,
38
- ],
39
- options: {
40
- trailingComma: 'es5',
41
- },
42
- },
43
- esNext: {
44
- patterns: [...esNextPaths],
45
- ignore: ['**/node_modules/**'],
46
- },
47
-};
48
-
23
const changedFiles = onlyChanged ? listChangedFiles() : null;
24
let didWarn = false;
25
let didError = false;
52
-Object.keys(config).forEach(key => {
53
- const patterns = config[key].patterns;
54
- const options = config[key].options;
55
- const ignore = config[key].ignore;
26
57
- const globPattern =
58
- patterns.length > 1 ? `{${patterns.join(',')}}` : `${patterns.join(',')}`;
59
- const files = glob
60
- .sync(globPattern, {ignore})
61
- .filter(f => !onlyChanged || changedFiles.has(f));
27
+const files = glob
28
+ .sync('**/*.js', {ignore: '**/node_modules/**'})
29
+ .filter(f => !onlyChanged || changedFiles.has(f));
30
63
- if (!files.length) {
64
- return;
65
- }
31
+if (!files.length) {
32
+ return;
33
+}
34
67
- const args = Object.assign({}, defaultOptions, options);
68
- files.forEach(file => {
69
- try {
70
- const input = fs.readFileSync(file, 'utf8');
71
- if (shouldWrite) {
72
- const output = prettier.format(input, args);
73
- if (output !== input) {
74
- fs.writeFileSync(file, output, 'utf8');
75
- }
76
- } else {
77
- if (!prettier.check(input, args)) {
78
- if (!didWarn) {
79
- console.log(
80
- '\n' +
81
- chalk.red(
82
- ` This project uses prettier to format all JavaScript code.\n`
83
- ) +
84
- chalk.dim(` Please run `) +
85
- chalk.reset('yarn prettier-all') +
86
- chalk.dim(
87
- ` and add changes to files listed below to your commit:`
88
- ) +
89
- `\n\n`
90
- );
91
- didWarn = true;
92
- }
93
- console.log(file);
35
+files.forEach(file => {
36
+ const options = prettier.resolveConfig.sync(file, {
37
+ config: prettierConfigPath,
38
+ });
39
+ try {
40
+ const input = fs.readFileSync(file, 'utf8');
41
+ if (shouldWrite) {
42
+ const output = prettier.format(input, options);
43
+ if (output !== input) {
44
+ fs.writeFileSync(file, output, 'utf8');
45
+ }
46
+ } else {
47
+ if (!prettier.check(input, options)) {
48
+ if (!didWarn) {
49
+ console.log(
50
+ '\n' +
51
+ chalk.red(
52
+ ` This project uses prettier to format all JavaScript code.\n`
53
+ ) +
54
+ chalk.dim(` Please run `) +
55
+ chalk.reset('yarn prettier-all') +
56
+ chalk.dim(
57
+ ` and add changes to files listed below to your commit:`
58
+ ) +
59
+ `\n\n`
60
+ );
61
+ didWarn = true;
62
}
63
+ console.log(file);
64
}
96
- } catch (error) {
97
- didError = true;
98
- console.log('\n\n' + error.message);
99
- console.log(file);
65
}
101
- });
66
+ } catch (error) {
67
+ didError = true;
68
+ console.log('\n\n' + error.message);
69
+ console.log(file);
70
+ }
71
});
72
73
if (didWarn || didError) {