Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
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
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
packages/react-devtools-timeline/static
32 changes: 29 additions & 3 deletions scripts/prettier/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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));
Expand Down