Skip to content

Commit 131f1e8

Browse files
masivessuperhawk610wardpeet
authoredMar 2, 2020
feat(gatsby-plugin-preload-fonts): Add option to run in CI (#21037)
Co-authored-by: Aaron Ross <superhawk610@gmail.com> Co-authored-by: Ward Peeters <ward@coding-tech.com>
1 parent ce0efb4 commit 131f1e8

File tree

4 files changed

+48
-14
lines changed

4 files changed

+48
-14
lines changed
 

‎packages/gatsby-plugin-preload-fonts/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ npm run preload-fonts -- --no-sandbox
5454

5555
See [Puppeteer](#puppeteer) for more information.
5656

57+
### Using with CI
58+
59+
If you're planning to use this plugin in CI you can set the environment variable `CI=true` (most CI providers like Travis and CircleCI will set this for you). Please note that in this case `font-preload-cache.json` will only get rebuilt when routes change.
60+
5761
## Explanation of method
5862

5963
A common pattern in modern web development is to link to a remote stylesheet that

‎packages/gatsby-plugin-preload-fonts/src/__tests__/fetch-routes.test.js

+27-12
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
const { request } = require(`graphql-request`)
2-
const createMockLogger = require(`logger-mock`)
3-
const fetchRoutes = require(`../prepare/fetch-routes`)
4-
51
jest.mock(`graphql-request`, () => {
62
return { request: jest.fn() }
73
})
84

5+
jest.mock(`gatsby-core-utils`)
6+
7+
const { request } = require(`graphql-request`)
8+
const { isCI, createContentDigest } = require(`gatsby-core-utils`)
9+
const createMockLogger = require(`logger-mock`)
10+
const fetchRoutes = require(`../prepare/fetch-routes`)
11+
912
describe(`fetch-routes`, () => {
1013
const endpoint = `http://localhost:3000/___graphql`
1114
let cache
@@ -63,16 +66,28 @@ describe(`fetch-routes`, () => {
6366
},
6467
}
6568
})
66-
logger.confirm.mockImplementationOnce(() =>
67-
Promise.resolve(false /* don't crawl routes */)
68-
)
69-
const mockExit = jest
70-
.spyOn(process, `exit`)
71-
.mockImplementationOnce(() => {})
69+
// don't crawl routes
70+
logger.confirm.mockImplementationOnce(() => Promise.resolve(false))
71+
createContentDigest.mockReturnValueOnce(cache.hash)
72+
73+
await expect(fetchRoutes({ logger, endpoint, cache })).resolves.toEqual([])
74+
})
75+
76+
it(`doesn't prompt the user when the list of routes has not changed since last run if run in CI`, async () => {
77+
expect.assertions(1)
78+
79+
request.mockImplementationOnce(() => {
80+
return {
81+
allSitePage: {
82+
nodes: [{ path: `/foo` }, { path: `/bar` }, { path: `/baz` }],
83+
},
84+
}
85+
})
7286

7387
cache.hash = `09f5b092fb87d859e0ac53dbae299a9e`
74-
await fetchRoutes({ logger, endpoint, cache })
88+
createContentDigest.mockReturnValueOnce(cache.hash)
89+
isCI.mockReturnValueOnce(true)
7590

76-
expect(mockExit).toHaveBeenCalledWith(0)
91+
await expect(fetchRoutes({ logger, endpoint, cache })).resolves.toEqual([])
7792
})
7893
})

‎packages/gatsby-plugin-preload-fonts/src/prepare/fetch-routes.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createContentDigest } from "gatsby-core-utils"
1+
const { createContentDigest, isCI } = require(`gatsby-core-utils`)
22
const { request } = require(`graphql-request`)
33
const { formatRelative } = require(`date-fns`)
44
const { red, blue, bold, dim } = require(`chalk`)
@@ -36,7 +36,14 @@ ${red(`err`)} could not establish a connection with the dev server
3636
}
3737

3838
const routesHash = createContentDigest(routes)
39+
// We can't detect all new routes so to make sure we are up to date
40+
// we ask the user if they wants to recrawl or not.
3941
if (cache.hash === routesHash) {
42+
// In CI we can't ask the user anything so we will bail as if the user said no.
43+
if (isCI()) {
44+
return []
45+
}
46+
4047
const lastRun = formatRelative(new Date(cache.timestamp), new Date())
4148
const ok = await logger.confirm(`
4249
@@ -48,7 +55,9 @@ ${red(`err`)} could not establish a connection with the dev server
4855
- ${dim(`route hash`)} ${bold(cache.hash)}
4956
5057
`)
51-
if (!ok) process.exit(0)
58+
if (!ok) {
59+
return []
60+
}
5261
}
5362

5463
return routes

‎packages/gatsby-plugin-preload-fonts/src/prepare/index.js

+6
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ export async function main() {
3737
endpoint: endpoint(GRAPHQL_PATH),
3838
})
3939

40+
// When we haven't found any routes we don't do anything
41+
// Routes can be empty when we're running in CI or the user didn't want to update
42+
if (!routes.length) {
43+
return
44+
}
45+
4046
const sections = [
4147
dim(` crawling routes`),
4248
`:bar`,

0 commit comments

Comments
 (0)
Please sign in to comment.