forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
88 lines (80 loc) · 2.3 KB
/
index.js
File metadata and controls
88 lines (80 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/**
* Copyright (c) 2014-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'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/**',
],
},
addons: {
patterns: ['addons/**/*.js'],
ignore: [
'**/node_modules/**',
],
options: {
// We don't compile them with Babel, and
// trailing commas break GCC.
'trailing-comma': 'none',
},
},
};
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;
}
});