Skip to content

Commit 70d7a6d

Browse files
committed
feat: auto-install gatsby plugin
1 parent ba53756 commit 70d7a6d

File tree

7 files changed

+282
-195
lines changed

7 files changed

+282
-195
lines changed

plugin/package-lock.json

+220-187
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugin/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"download": "^8.0.0",
4848
"etag": "^1.8.1",
4949
"fs-extra": "^10.0.0",
50+
"gatsby-plugin-netlify": "^4.1.0",
5051
"linkfs": "^2.1.0",
5152
"multer": "^1.4.2",
5253
"node-fetch": "^2.6.1",

plugin/src/helpers/config.ts

+41-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
/* eslint-disable max-lines */
12
import { EOL } from 'os'
23
import path from 'path'
34
import process from 'process'
45

56
import { stripIndent } from 'common-tags'
6-
import fs, { existsSync } from 'fs-extra'
7+
import fs, { existsSync, move, writeFile } from 'fs-extra'
78
import type { GatsbyConfig, PluginRef } from 'gatsby'
9+
import { relative } from 'pathe'
10+
11+
import { getGatsbyConfigWrapper } from '../templates/gatsby-config'
812

913
export async function spliceConfig({
1014
startMarker,
@@ -62,14 +66,47 @@ function hasPlugin(plugins: PluginRef[], pluginName: string): boolean {
6266
)
6367
}
6468

65-
export function checkGatsbyConfig({ utils, netlifyConfig }): void {
69+
function hasAutoInstall(
70+
// eslint-disable-next-line camelcase
71+
plugins: Array<PluginRef & { __nf_auto?: true }>,
72+
): boolean {
73+
// eslint-disable-next-line no-underscore-dangle
74+
return plugins?.some((plugin) => plugin.__nf_auto)
75+
}
76+
77+
/**
78+
* Wrap the Gatsby config to inject the Netlify plugin
79+
*/
80+
export async function wrapGatsbyConfig() {
81+
const configFile = path.resolve(process.cwd(), 'gatsby-config.js')
82+
if (!existsSync(configFile)) {
83+
console.error('Could not find Gatsby config')
84+
return
85+
}
86+
await move(configFile, path.resolve(process.cwd(), 'gatsby-config-orig.js'))
87+
88+
const pluginRoot = require.resolve('gatsby-plugin-netlify')
89+
90+
const pluginPath = relative(process.cwd(), pluginRoot)
91+
console.log({ pluginPath, pluginRoot })
92+
await writeFile(configFile, getGatsbyConfigWrapper(pluginPath))
93+
}
94+
95+
export async function checkGatsbyConfig({
96+
utils,
97+
netlifyConfig,
98+
}): Promise<void> {
6699
// warn if gatsby-plugin-netlify is missing
67100
const gatsbyConfig = loadGatsbyConfig(utils)
68101

69-
if (!hasPlugin(gatsbyConfig.plugins, 'gatsby-plugin-netlify')) {
102+
if (
103+
!hasPlugin(gatsbyConfig.plugins, 'gatsby-plugin-netlify') &&
104+
!hasAutoInstall(gatsbyConfig.plugins)
105+
) {
70106
console.error(
71107
'Please install `gatsby-plugin-netlify` and enable it in your gatsby-config.js. https://www.gatsbyjs.com/plugins/gatsby-plugin-netlify/',
72108
)
109+
await wrapGatsbyConfig()
73110
}
74111

75112
if (hasPlugin(gatsbyConfig.plugins, 'gatsby-plugin-netlify-cache')) {
@@ -152,3 +189,4 @@ export function shouldSkipFunctions(cacheDir: string): boolean {
152189

153190
return false
154191
}
192+
/* eslint-enable max-lines */

plugin/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export async function onPreBuild({
3131
}
3232
await restoreCache({ utils, publish: PUBLISH_DIR })
3333

34-
checkGatsbyConfig({ utils, netlifyConfig })
34+
await checkGatsbyConfig({ utils, netlifyConfig })
3535
}
3636

3737
export async function onBuild({

plugin/src/templates/gatsby-config.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { stripIndent as javascript } from 'common-tags'
2+
3+
export const getGatsbyConfigWrapper = (pluginPath: string): string =>
4+
// A string, but with the right editor plugin this will format as JS
5+
javascript`
6+
7+
const originalConfig = require('./gatsby-config-orig')
8+
module.exports = {
9+
...originalConfig,
10+
plugins: [
11+
...originalConfig.plugins,
12+
{
13+
resolve: require.resolve("${pluginPath}"),
14+
"__nf_auto": true
15+
},
16+
],
17+
}
18+
`

plugin/test/fixtures/functions-without-gatsby-plugin/gatsby-config.js

-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
module.exports = {
2-
flags: {
3-
FUNCTIONS: true,
4-
},
52
siteMetadata: {
63
title: 'Function test',
74
},

plugin/test/fixtures/functions-without-gatsby-plugin/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"test:jest": "jest"
2121
},
2222
"dependencies": {
23-
"gatsby": "^4.5.4",
23+
"gatsby": "latest",
2424
"react": "^17.0.1",
2525
"react-dom": "^17.0.1"
2626
},

0 commit comments

Comments
 (0)