Skip to content

Commit 06d961d

Browse files
committed
feat: add version check for gatsby-netlify-plugin
1 parent 9b316ac commit 06d961d

File tree

5 files changed

+66
-11
lines changed

5 files changed

+66
-11
lines changed

plugin/package-lock.json

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugin/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,15 @@
5454
"node-stream-zip": "^1.15.0",
5555
"pathe": "^0.2.0",
5656
"pretty-bytes": "^5.6.0",
57+
"semver": "^7.3.5",
5758
"tempy": "^1.0.0"
5859
},
5960
"devDependencies": {
6061
"@gatsbyjs/reach-router": "^1.3.6",
6162
"@netlify/build": "^26.5.1",
6263
"@types/fs-extra": "^9.0.12",
6364
"@types/multer": "^1.4.7",
65+
"@types/semver": "^7.3.9",
6466
"gatsby": "^4.9.0",
6567
"npm-run-all": "^4.1.5",
6668
"rimraf": "^3.0.2",

plugin/src/helpers/config.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { stripIndent } from 'common-tags'
66
import fs, { existsSync } from 'fs-extra'
77
import type { GatsbyConfig, PluginRef } from 'gatsby'
88

9+
import { checkPackageVersion } from './files'
10+
911
export async function spliceConfig({
1012
startMarker,
1113
endMarker,
@@ -38,11 +40,9 @@ export async function spliceConfig({
3840
return fs.writeFile(fileName, out)
3941
}
4042

41-
function loadGatsbyConfig({ utils, publish }): GatsbyConfig | never {
42-
const gatsbyConfigFile = path.resolve(
43-
getGatsbyRoot(publish),
44-
'gatsby-config.js',
45-
)
43+
function loadGatsbyConfig({ gatsbyRoot, utils }): GatsbyConfig | never {
44+
const gatsbyConfigFile = path.resolve(gatsbyRoot, 'gatsby-config.js')
45+
4646
if (!existsSync(gatsbyConfigFile)) {
4747
return {}
4848
}
@@ -65,14 +65,25 @@ function hasPlugin(plugins: PluginRef[], pluginName: string): boolean {
6565
)
6666
}
6767

68-
export function checkGatsbyConfig({ utils, netlifyConfig }): void {
68+
export async function checkConfig({ utils, netlifyConfig }): Promise<void> {
69+
const gatsbyRoot = getGatsbyRoot(netlifyConfig.build.publish)
70+
6971
// warn if gatsby-plugin-netlify is missing
7072
const gatsbyConfig = loadGatsbyConfig({
7173
utils,
72-
publish: netlifyConfig.build.publish,
74+
gatsbyRoot,
7375
})
7476

75-
if (!hasPlugin(gatsbyConfig.plugins, 'gatsby-plugin-netlify')) {
77+
if (hasPlugin(gatsbyConfig.plugins, 'gatsby-plugin-netlify')) {
78+
if (
79+
// prettier-ignore
80+
!(await checkPackageVersion(gatsbyRoot, 'gatsby-plugin-netlify', '>=4.2.0',))
81+
) {
82+
console.error(
83+
'The plugin `gatsby-plugin-netlify` does not support DSG, please update to >=4.2.0',
84+
)
85+
}
86+
} else {
7687
console.error(
7788
'Please install `gatsby-plugin-netlify` and enable it in your gatsby-config.js. https://www.gatsbyjs.com/plugins/gatsby-plugin-netlify/',
7889
)

plugin/src/helpers/files.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
import os from 'os'
22
import process from 'process'
33

4-
import { copyFile, ensureDir, existsSync, readFile, writeFile } from 'fs-extra'
4+
import {
5+
copyFile,
6+
ensureDir,
7+
existsSync,
8+
readFile,
9+
writeFile,
10+
readJson,
11+
} from 'fs-extra'
512
import { dirname, join, resolve } from 'pathe'
13+
import semver from 'semver'
614

715
const DEFAULT_LAMBDA_PLATFORM = 'linux'
816
const DEFAULT_LAMBDA_ABI = '83'
@@ -70,6 +78,26 @@ export const findModuleFromBase = ({ paths, candidates }): string | null => {
7078
return null
7179
}
7280

81+
export async function checkPackageVersion(
82+
root: string,
83+
name: string,
84+
version: string,
85+
): Promise<boolean> {
86+
const packagePath = findModuleFromBase({
87+
paths: [root],
88+
candidates: [name],
89+
})
90+
91+
let packageObj: { version: string }
92+
try {
93+
packageObj = await readJson(`${packagePath}/package.json`)
94+
} catch {
95+
return false
96+
}
97+
98+
return semver.satisfies(packageObj.version, version)
99+
}
100+
73101
/**
74102
* When Gatsby runs a build, it copies binary dependencies to the "query-engine" folder. These are auto-detected, based
75103
* on the environment in which the build is running. This can cause problems if these don't match the lambda environment.

plugin/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { existsSync } from 'fs-extra'
77

88
import { normalizedCacheDir, restoreCache, saveCache } from './helpers/cache'
99
import {
10-
checkGatsbyConfig,
10+
checkConfig,
1111
mutateConfig,
1212
shouldSkipFunctions,
1313
spliceConfig,
@@ -32,7 +32,7 @@ export async function onPreBuild({
3232
}
3333
await restoreCache({ utils, publish: PUBLISH_DIR })
3434

35-
checkGatsbyConfig({ utils, netlifyConfig })
35+
await checkConfig({ utils, netlifyConfig })
3636
}
3737

3838
export async function onBuild({

0 commit comments

Comments
 (0)