mirror of
https://github.com/zebrajr/react.git
synced 2025-12-06 12:20:20 +01:00
Use Prettier Config API (#11980)
This commit is contained in:
parent
301edeaac8
commit
052a5f27f3
18
.prettierrc.js
Normal file
18
.prettierrc.js
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
const {esNextPaths} = require('./scripts/shared/pathsByLanguageVersion');
|
||||
|
||||
module.exports = {
|
||||
bracketSpacing: false,
|
||||
singleQuote: true,
|
||||
jsxBracketSameLine: true,
|
||||
trailingComma: 'es5',
|
||||
printWidth: 80,
|
||||
|
||||
overrides: [
|
||||
{
|
||||
files: esNextPaths,
|
||||
options: {
|
||||
trailingComma: 'all',
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
|
|
@ -7,74 +7,44 @@
|
|||
'use strict';
|
||||
|
||||
// Based on similar script in Jest
|
||||
// https://github.com/facebook/jest/blob/master/scripts/prettier.js
|
||||
// https://github.com/facebook/jest/blob/a7acc5ae519613647ff2c253dd21933d6f94b47f/scripts/prettier.js
|
||||
|
||||
const chalk = require('chalk');
|
||||
const glob = require('glob');
|
||||
const prettier = require('prettier');
|
||||
const fs = require('fs');
|
||||
const listChangedFiles = require('../shared/listChangedFiles');
|
||||
const {esNextPaths} = require('../shared/pathsByLanguageVersion');
|
||||
const prettierConfigPath = require.resolve('../../.prettierrc');
|
||||
|
||||
const mode = process.argv[2] || 'check';
|
||||
const shouldWrite = mode === 'write' || mode === 'write-changed';
|
||||
const onlyChanged = mode === 'check-changed' || mode === 'write-changed';
|
||||
|
||||
const defaultOptions = {
|
||||
bracketSpacing: false,
|
||||
singleQuote: true,
|
||||
jsxBracketSameLine: true,
|
||||
trailingComma: 'all',
|
||||
printWidth: 80,
|
||||
};
|
||||
const config = {
|
||||
default: {
|
||||
patterns: ['**/*.js'],
|
||||
ignore: [
|
||||
'**/node_modules/**',
|
||||
// ESNext paths can have trailing commas.
|
||||
// We'll handle them separately below.
|
||||
...esNextPaths,
|
||||
],
|
||||
options: {
|
||||
trailingComma: 'es5',
|
||||
},
|
||||
},
|
||||
esNext: {
|
||||
patterns: [...esNextPaths],
|
||||
ignore: ['**/node_modules/**'],
|
||||
},
|
||||
};
|
||||
|
||||
const changedFiles = onlyChanged ? listChangedFiles() : null;
|
||||
let didWarn = false;
|
||||
let didError = false;
|
||||
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})
|
||||
.sync('**/*.js', {ignore: '**/node_modules/**'})
|
||||
.filter(f => !onlyChanged || changedFiles.has(f));
|
||||
|
||||
if (!files.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
const args = Object.assign({}, defaultOptions, options);
|
||||
files.forEach(file => {
|
||||
const options = prettier.resolveConfig.sync(file, {
|
||||
config: prettierConfigPath,
|
||||
});
|
||||
try {
|
||||
const input = fs.readFileSync(file, 'utf8');
|
||||
if (shouldWrite) {
|
||||
const output = prettier.format(input, args);
|
||||
const output = prettier.format(input, options);
|
||||
if (output !== input) {
|
||||
fs.writeFileSync(file, output, 'utf8');
|
||||
}
|
||||
} else {
|
||||
if (!prettier.check(input, args)) {
|
||||
if (!prettier.check(input, options)) {
|
||||
if (!didWarn) {
|
||||
console.log(
|
||||
'\n' +
|
||||
|
|
@ -99,7 +69,6 @@ Object.keys(config).forEach(key => {
|
|||
console.log(file);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
if (didWarn || didError) {
|
||||
process.exit(1);
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user