mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
This script is used on CI in `yarn_lint` job. With current `glob` call settings, it includes a bunch of build files, which are actually ignored by listing them in `.prettierignore`. This check is not failing only because there is no build step before it. If you run `node ./scripts/prettier/index` with build files present, you will see a bunch of files listed as non-formatted. These changes add a simple logic to include all paths listed in `.prettierignore` to ignore list of `glob` call with transforming them from gitignore-style to glob-style. This should unblock CI for https://github.com/facebook/react/pull/27612, where `flow-typed` directory will be added, turned out that including it in `.prettierignore` is not enough.
110 lines
2.9 KiB
JavaScript
110 lines
2.9 KiB
JavaScript
/**
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* 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/a7acc5ae519613647ff2c253dd21933d6f94b47f/scripts/prettier.js
|
|
|
|
const chalk = require('chalk');
|
|
const glob = require('glob');
|
|
const prettier = require('prettier');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const listChangedFiles = require('../shared/listChangedFiles');
|
|
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 changedFiles = onlyChanged ? listChangedFiles() : null;
|
|
let didWarn = false;
|
|
let didError = false;
|
|
|
|
const prettierIgnoreFilePath = path.join(
|
|
__dirname,
|
|
'..',
|
|
'..',
|
|
'.prettierignore'
|
|
);
|
|
const prettierIgnore = fs.readFileSync(prettierIgnoreFilePath, {
|
|
encoding: 'utf8',
|
|
});
|
|
const ignoredPathsListedInPrettierIgnore = prettierIgnore
|
|
.toString()
|
|
.replace(/\r\n/g, '\n')
|
|
.split('\n')
|
|
.filter(line => !!line && !line.startsWith('#'));
|
|
|
|
const ignoredPathsListedInPrettierIgnoreInGlobFormat =
|
|
ignoredPathsListedInPrettierIgnore.map(ignoredPath => {
|
|
const existsAndDirectory =
|
|
fs.existsSync(ignoredPath) && fs.lstatSync(ignoredPath).isDirectory();
|
|
|
|
if (existsAndDirectory) {
|
|
return path.join(ignoredPath, '/**');
|
|
}
|
|
|
|
return ignoredPath;
|
|
});
|
|
|
|
const files = glob
|
|
.sync('**/*.js', {
|
|
ignore: [
|
|
'**/node_modules/**',
|
|
'**/cjs/**',
|
|
...ignoredPathsListedInPrettierIgnoreInGlobFormat,
|
|
],
|
|
})
|
|
.filter(f => !onlyChanged || changedFiles.has(f));
|
|
|
|
if (!files.length) {
|
|
process.exit(0);
|
|
}
|
|
|
|
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, options);
|
|
if (output !== input) {
|
|
fs.writeFileSync(file, output, 'utf8');
|
|
}
|
|
} else {
|
|
if (!prettier.check(input, options)) {
|
|
if (!didWarn) {
|
|
console.log(
|
|
'\n' +
|
|
chalk.red(
|
|
` This project uses prettier to format all JavaScript code.\n`
|
|
) +
|
|
chalk.dim(` Please run `) +
|
|
chalk.reset('yarn prettier-all') +
|
|
chalk.dim(
|
|
` and add changes to files listed below to your commit:`
|
|
) +
|
|
`\n\n`
|
|
);
|
|
didWarn = true;
|
|
}
|
|
console.log(file);
|
|
}
|
|
}
|
|
} catch (error) {
|
|
didError = true;
|
|
console.log('\n\n' + error.message);
|
|
console.log(file);
|
|
}
|
|
});
|
|
|
|
if (didWarn || didError) {
|
|
process.exit(1);
|
|
}
|