Skip to content

Commit 482416b

Browse files
mikelaxpieh
authored andcommitted
fix: ignore dot files in page creator plugin (#8573)
* fix: ignore dot files in page creator plugin. * fix: Also ignore json and yaml files, remove stray .only. * chore: refactor tests to be eaier to manage. * chore: fix failing test, typo.
1 parent a87eceb commit 482416b

File tree

3 files changed

+97
-54
lines changed

3 files changed

+97
-54
lines changed

packages/gatsby-plugin-page-creator/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@
33
Gatsby plugin that automatically creates pages from React components in specified directories. Gatsby
44
includes this plugin automatically in all sites for creating pages from components in `src/pages`.
55

6+
With this plugin, *any* file that lives in the `src/pages` folder (or subfolders) will be expected to export a React Component to generate a Page. The following files are automatically excluded:
7+
8+
- `template-*`
9+
- `__tests__/*`
10+
- `*.test.jsx?`
11+
- `*.spec.jsx?`
12+
- `*.d.tsx?`
13+
- `*.json`
14+
- `*.yaml`
15+
- `_*`
16+
- `.*`
17+
618
## Install
719

820
`npm install --save gatsby-plugin-page-creator`

packages/gatsby-plugin-page-creator/src/__tests__/gatsby-node.js

Lines changed: 81 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,77 +2,105 @@ const validatePath = require(`../validate-path`)
22
const createPath = require(`../create-path`)
33

44
describe(`JavaScript page creator`, () => {
5+
it(`includes the correct file types`, () => {
6+
const validFiles = [
7+
{ path: `test1.js` },
8+
{ path: `somedir/test1.js` },
9+
{ path: `somedir/test2.ts` },
10+
{ path: `somedir/dir2/test1.js` },
11+
]
12+
13+
expect(validFiles.filter(file => validatePath(file.path)).length).toEqual(validFiles.length)
14+
})
15+
516
it(`filters out files that start with underscores`, () => {
6-
const files = [
7-
{
8-
path: `something/blah.js`,
9-
},
10-
{
11-
path: `something/_blah.js`,
12-
},
13-
{
14-
path: `_blah.js`,
15-
},
17+
const validFiles = [
18+
{ path: `something/blah.js` },
19+
{ path: `test1.js` },
20+
]
21+
22+
const testFiles = validFiles.concat([
23+
{ path: `something/_foo.js` },
24+
{ path: `_blah.js` },
25+
])
26+
27+
expect(testFiles.filter(file => validatePath(file.path)).length).toEqual(validFiles.length)
28+
})
29+
30+
it(`filters out files that start with dot`, () => {
31+
const validFiles = [
32+
{ path: `something/blah.js` },
33+
{ path: `test1.ts` },
1634
]
1735

18-
expect(files.filter(file => validatePath(file.path)).length).toEqual(1)
36+
const testFiles = validFiles.concat([
37+
{ path: `.eslintrc` },
38+
{ path: `something/.eslintrc` },
39+
{ path: `something/.eslintrc.js` },
40+
{ path: `.markdownlint.json` },
41+
{ path: `something/.markdownlint.json` },
42+
])
43+
44+
expect(testFiles.filter(file => validatePath(file.path)).length).toEqual(validFiles.length)
45+
})
46+
47+
it(`filters out json and yaml files`, () => {
48+
const validFiles = [
49+
{ path: `somefile.js` },
50+
{ path: `something/blah.js` },
51+
]
52+
53+
const testFiles = validFiles.concat([
54+
{ path: `something/otherConfig.yml` },
55+
{ path: `config.yaml` },
56+
{ path: `somefile.json` },
57+
{ path: `dir1/file.json` },
58+
{ path: `dir1/dir2/file.json` },
59+
])
60+
61+
expect(testFiles.filter(file => validatePath(file.path)).length).toEqual(validFiles.length)
1962
})
2063

2164
it(`filters out files that start with template-*`, () => {
22-
const files = [
23-
{
24-
path: `something/blah.js`,
25-
},
26-
{
27-
path: `something/template-cool-page-type.js`,
28-
},
29-
{
30-
path: `template-cool-page-type.js`,
31-
},
65+
const validFiles = [
66+
{ path: `something/blah.js` },
67+
{ path: `file1.js` },
3268
]
3369

34-
expect(files.filter(file => validatePath(file.path)).length).toEqual(1)
70+
const testFiles = validFiles.concat([
71+
{ path: `template-cool-page-type.js` },
72+
])
73+
74+
expect(testFiles.filter(file => validatePath(file.path)).length).toEqual(validFiles.length)
3575
})
3676

3777
it(`filters out files that have TypeScript declaration extensions`, () => {
38-
const files = [
39-
{
40-
path: `something/foo.ts`,
41-
},
42-
{
43-
path: `something/bar.tsx`,
44-
},
45-
{
46-
path: `something/declaration-file.d.ts`,
47-
},
48-
{
49-
path: `something-else/other-declaration-file.d.tsx`,
50-
},
78+
const validFiles = [
79+
{ path: `something/foo.ts` },
80+
{ path: `something/bar.tsx` },
5181
]
5282

53-
expect(files.filter(file => validatePath(file.path)).length).toEqual(2)
83+
const testFiles = validFiles.concat([
84+
{ path: `something/declaration-file.d.ts` },
85+
{ path: `something-else/other-declaration-file.d.tsx` },
86+
])
87+
88+
expect(testFiles.filter(file => validatePath(file.path)).length).toEqual(validFiles.length)
5489
})
5590

5691
it(`filters out test files`, () => {
57-
const files = [
58-
{
59-
path: `__tests__/something.test.js`,
60-
},
61-
{
62-
path: `foo.spec.js`,
63-
},
64-
{
65-
path: `bar.test.js`,
66-
},
67-
{
68-
path: `page.js`,
69-
},
70-
{
71-
path: `page.jsx`,
72-
},
92+
const validFiles = [
93+
{ path: `page.js` },
94+
{ path: `page.jsx` },
7395
]
7496

75-
expect(files.filter(file => validatePath(file.path)).length).toEqual(2)
97+
const testFiles = validFiles.concat([
98+
{ path: `__tests__/something.test.js` },
99+
{ path: `foo.spec.js` },
100+
{ path: `bar.test.js` },
101+
])
102+
103+
expect(testFiles.filter(file => validatePath(file.path)).length).toEqual(validFiles.length)
76104
})
77105

78106
describe(`create-path`, () => {
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
const systemPath = require(`path`)
22

33
const tsDeclarationExtTest = /\.d\.tsx?$/
4+
const jsonYamlExtTest = /\.(json|ya?ml)$/
45

56
function isTestFile(path) {
67
const testFileTest = new RegExp(`(/__tests__/.*|(\\.|/)(test|spec))\\.jsx?$`)
78
return path.match(testFileTest)
89
}
910

1011
module.exports = path => {
11-
// Disallow paths starting with an underscore
12+
// Disallow paths starting with an underscore (_) or dot (.)
1213
// and template-.
1314
// and .d.ts
1415
const parsedPath = systemPath.parse(path)
1516
return (
1617
parsedPath.name.slice(0, 1) !== `_` &&
18+
parsedPath.name.slice(0, 1) !== `.` &&
1719
parsedPath.name.slice(0, 9) !== `template-` &&
1820
!tsDeclarationExtTest.test(parsedPath.base) &&
21+
!jsonYamlExtTest.test(parsedPath.base) &&
1922
!isTestFile(parsedPath.base)
2023
)
2124
}

0 commit comments

Comments
 (0)