From c1c26a38e566daff5af82dac200da903308c9112 Mon Sep 17 00:00:00 2001 From: Ruslan Lesiutin Date: Mon, 30 Oct 2023 15:32:40 +0000 Subject: [PATCH] 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. --- .prettierignore | 5 ++++- scripts/prettier/index.js | 32 +++++++++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/.prettierignore b/.prettierignore index 6f69f7f891d67..1ab5d680398d2 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,6 +1,9 @@ +build + packages/react-devtools-core/dist packages/react-devtools-extensions/chrome/build packages/react-devtools-extensions/firefox/build +packages/react-devtools-extensions/edge/build packages/react-devtools-extensions/shared/build packages/react-devtools-extensions/src/ErrorTesterCompiled.js packages/react-devtools-inline/dist @@ -8,4 +11,4 @@ packages/react-devtools-shared/src/hooks/__tests__/__source__/__compiled__/ packages/react-devtools-shared/src/hooks/__tests__/__source__/__untransformed__/ packages/react-devtools-shell/dist packages/react-devtools-timeline/dist -packages/react-devtools-timeline/static \ No newline at end of file +packages/react-devtools-timeline/static diff --git a/scripts/prettier/index.js b/scripts/prettier/index.js index d30ba73315ee9..dd37d8b7b68c6 100644 --- a/scripts/prettier/index.js +++ b/scripts/prettier/index.js @@ -13,6 +13,7 @@ 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'); @@ -24,14 +25,39 @@ 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/**', - '**/__compiled__/**', - '**/__untransformed__/**', - 'packages/react-devtools-extensions/src/ErrorTesterCompiled.js', + ...ignoredPathsListedInPrettierIgnoreInGlobFormat, ], }) .filter(f => !onlyChanged || changedFiles.has(f));