@samitouri / QOS-React-1 / commits / 0c6348758f

refactor[scripts/prettier]: respect .prettierignore when resolving js files via glob (#27627)

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.

Ruslan Lesiutin committed Oct 30, 2023 at 15:32 UTC 0c6348758f89be250070560972e736171201f82d
2 files changed +33 -4
.prettierignore
+4 -1
@@ -1,6 +1,9 @@
1 +build
2 +
3 packages/react-devtools-core/dist
4 packages/react-devtools-extensions/chrome/build
5 packages/react-devtools-extensions/firefox/build
6 +packages/react-devtools-extensions/edge/build
7 packages/react-devtools-extensions/shared/build
8 packages/react-devtools-extensions/src/ErrorTesterCompiled.js
9 packages/react-devtools-inline/dist
@@ -8,4 +11,4 @@ packages/react-devtools-shared/src/hooks/__tests__/__source__/__compiled__/
11 packages/react-devtools-shared/src/hooks/__tests__/__source__/__untransformed__/
12 packages/react-devtools-shell/dist
13 packages/react-devtools-timeline/dist
11 -packages/react-devtools-timeline/static
\ No newline at end of file
14 +packages/react-devtools-timeline/static
scripts/prettier/index.js
+29 -3
@@ -13,6 +13,7 @@ const chalk = require('chalk');
13 const glob = require('glob');
14 const prettier = require('prettier');
15 const fs = require('fs');
16 +const path = require('path');
17 const listChangedFiles = require('../shared/listChangedFiles');
18 const prettierConfigPath = require.resolve('../../.prettierrc');
19
@@ -24,14 +25,39 @@ const changedFiles = onlyChanged ? listChangedFiles() : null;
25 let didWarn = false;
26 let didError = false;
27
28 +const prettierIgnoreFilePath = path.join(
29 + __dirname,
30 + '..',
31 + '..',
32 + '.prettierignore'
33 +);
34 +const prettierIgnore = fs.readFileSync(prettierIgnoreFilePath, {
35 + encoding: 'utf8',
36 +});
37 +const ignoredPathsListedInPrettierIgnore = prettierIgnore
38 + .toString()
39 + .replace(/\r\n/g, '\n')
40 + .split('\n')
41 + .filter(line => !!line && !line.startsWith('#'));
42 +
43 +const ignoredPathsListedInPrettierIgnoreInGlobFormat =
44 + ignoredPathsListedInPrettierIgnore.map(ignoredPath => {
45 + const existsAndDirectory =
46 + fs.existsSync(ignoredPath) && fs.lstatSync(ignoredPath).isDirectory();
47 +
48 + if (existsAndDirectory) {
49 + return path.join(ignoredPath, '/**');
50 + }
51 +
52 + return ignoredPath;
53 + });
54 +
55 const files = glob
56 .sync('**/*.js', {
57 ignore: [
58 '**/node_modules/**',
59 '**/cjs/**',
32 - '**/__compiled__/**',
33 - '**/__untransformed__/**',
34 - 'packages/react-devtools-extensions/src/ErrorTesterCompiled.js',
60 + ...ignoredPathsListedInPrettierIgnoreInGlobFormat,
61 ],
62 })
63 .filter(f => !onlyChanged || changedFiles.has(f));