Skip to content

Commit 0c63487

Browse files
authored
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 #27612, where `flow-typed` directory will be added, turned out that including it in `.prettierignore` is not enough.
1 parent 0965fbc commit 0c63487

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

.prettierignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
build
2+
13
packages/react-devtools-core/dist
24
packages/react-devtools-extensions/chrome/build
35
packages/react-devtools-extensions/firefox/build
6+
packages/react-devtools-extensions/edge/build
47
packages/react-devtools-extensions/shared/build
58
packages/react-devtools-extensions/src/ErrorTesterCompiled.js
69
packages/react-devtools-inline/dist
710
packages/react-devtools-shared/src/hooks/__tests__/__source__/__compiled__/
811
packages/react-devtools-shared/src/hooks/__tests__/__source__/__untransformed__/
912
packages/react-devtools-shell/dist
1013
packages/react-devtools-timeline/dist
11-
packages/react-devtools-timeline/static
14+
packages/react-devtools-timeline/static

scripts/prettier/index.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const chalk = require('chalk');
1313
const glob = require('glob');
1414
const prettier = require('prettier');
1515
const fs = require('fs');
16+
const path = require('path');
1617
const listChangedFiles = require('../shared/listChangedFiles');
1718
const prettierConfigPath = require.resolve('../../.prettierrc');
1819

@@ -24,14 +25,39 @@ const changedFiles = onlyChanged ? listChangedFiles() : null;
2425
let didWarn = false;
2526
let didError = false;
2627

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+
2755
const files = glob
2856
.sync('**/*.js', {
2957
ignore: [
3058
'**/node_modules/**',
3159
'**/cjs/**',
32-
'**/__compiled__/**',
33-
'**/__untransformed__/**',
34-
'packages/react-devtools-extensions/src/ErrorTesterCompiled.js',
60+
...ignoredPathsListedInPrettierIgnoreInGlobFormat,
3561
],
3662
})
3763
.filter(f => !onlyChanged || changedFiles.has(f));

0 commit comments

Comments
 (0)