Skip to content
This repository was archived by the owner on Jan 11, 2023. It is now read-only.

Commit 9c8b46d

Browse files
committed
feat: add support for gatsby 5
1 parent 0d5f549 commit 9c8b46d

File tree

12 files changed

+120
-17
lines changed

12 files changed

+120
-17
lines changed

src/core.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,14 @@ const getContext = (context) => {
6767
*/
6868
export const listFrameworks = async function (context) {
6969
const { pathExists, packageJson, packageJsonPath, nodeVersion } = getContext(context)
70-
const { npmDependencies, scripts, runScriptCommand } = await getProjectInfo({
70+
const { npmDependencies, npmDependenciesVersions, scripts, runScriptCommand } = await getProjectInfo({
7171
pathExists,
7272
packageJson,
7373
packageJsonPath,
7474
})
75-
const frameworks = await pFilter(FRAMEWORKS, (framework) => usesFramework(framework, { pathExists, npmDependencies }))
75+
const frameworks = await pFilter(FRAMEWORKS, (framework) =>
76+
usesFramework(framework, { pathExists, npmDependencies, npmDependenciesVersions }),
77+
)
7678
const frameworkInfos = frameworks.map((framework) =>
7779
getFrameworkInfo(framework, { scripts, runScriptCommand, nodeVersion }),
7880
)
@@ -90,8 +92,12 @@ export const listFrameworks = async function (context) {
9092
export const hasFramework = async function (frameworkId, context) {
9193
const framework = getFrameworkById(frameworkId)
9294
const { pathExists, packageJson, packageJsonPath } = getContext(context)
93-
const { npmDependencies } = await getProjectInfo({ pathExists, packageJson, packageJsonPath })
94-
const result = await usesFramework(framework, { pathExists, npmDependencies })
95+
const { npmDependencies, npmDependenciesVersions } = await getProjectInfo({
96+
pathExists,
97+
packageJson,
98+
packageJsonPath,
99+
})
100+
const result = await usesFramework(framework, { pathExists, npmDependencies, npmDependenciesVersions })
95101
return result
96102
}
97103

@@ -131,11 +137,12 @@ const getFrameworkId = function ({ id }) {
131137
}
132138

133139
const getProjectInfo = async function ({ pathExists, packageJson, packageJsonPath }) {
134-
const { npmDependencies, scripts } = await getPackageJsonContent({
140+
const { npmDependencies, npmDependenciesVersions, scripts } = getPackageJsonContent({
135141
packageJson,
136142
})
137143
const runScriptCommand = await getRunScriptCommand({ pathExists, packageJsonPath })
138-
return { npmDependencies, scripts, runScriptCommand }
144+
145+
return { npmDependencies, npmDependenciesVersions, scripts, runScriptCommand }
139146
}
140147

141148
const getFrameworkInfo = function (
@@ -155,11 +162,12 @@ const getFrameworkInfo = function (
155162
) {
156163
const devCommands = getDevCommands({ frameworkDevCommand, scripts, runScriptCommand })
157164
const recommendedPlugins = getPlugins(plugins, { nodeVersion })
165+
158166
return {
159167
id,
160168
name,
161169
package: {
162-
name: detect.npmDependencies[0],
170+
name: typeof detect.npmDependencies[0] === 'string' ? detect.npmDependencies[0] : detect.npmDependencies[0]?.name,
163171
version: 'unknown',
164172
},
165173
category,

src/detect.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pLocate from 'p-locate'
2+
import semver from 'semver'
23

34
// Checks if the project is using a specific framework:
45
// - if `framework.npmDependencies` is set, one of them must be present in the
@@ -14,20 +15,34 @@ export const usesFramework = async function (
1415
configFiles,
1516
},
1617
},
17-
{ pathExists, npmDependencies },
18+
{ pathExists, npmDependencies, npmDependenciesVersions },
1819
) {
1920
return (
20-
usesNpmDependencies(frameworkNpmDependencies, npmDependencies) &&
21+
usesNpmDependencies(frameworkNpmDependencies, npmDependencies, npmDependenciesVersions) &&
2122
lacksExcludedNpmDependencies(frameworkExcludedNpmDependencies, npmDependencies) &&
2223
(await usesConfigFiles(configFiles, pathExists))
2324
)
2425
}
2526

26-
const usesNpmDependencies = function (frameworkNpmDependencies, npmDependencies) {
27-
return (
28-
frameworkNpmDependencies.length === 0 ||
29-
frameworkNpmDependencies.some((frameworkNpmDependency) => npmDependencies.includes(frameworkNpmDependency))
30-
)
27+
const usesNpmDependencies = function (frameworkNpmDependencies, npmDependencies, npmDependenciesVersions) {
28+
if (frameworkNpmDependencies.length === 0) {
29+
return true
30+
}
31+
32+
return frameworkNpmDependencies.some((frameworkNpmDependency) => {
33+
if (typeof frameworkNpmDependency === 'string') {
34+
return npmDependencies.includes(frameworkNpmDependency)
35+
}
36+
37+
return (
38+
npmDependencies.includes(frameworkNpmDependency.name) &&
39+
(frameworkNpmDependency.version == null ||
40+
semver.satisfies(
41+
semver.minVersion(npmDependenciesVersions[frameworkNpmDependency.name]),
42+
frameworkNpmDependency.version,
43+
))
44+
)
45+
})
3146
}
3247

3348
const lacksExcludedNpmDependencies = function (frameworkExcludedNpmDependencies, npmDependencies) {

src/frameworks/gatsby.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"name": "Gatsby",
44
"category": "static_site_generator",
55
"detect": {
6-
"npmDependencies": ["gatsby"],
6+
"npmDependencies": [{ "name": "gatsby", "version": "<5.0.0" }],
77
"excludedNpmDependencies": [],
88
"configFiles": ["gatsby-config.js"]
99
},

src/frameworks/gatsby5.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"id": "gatsby5",
3+
"name": "Gatsby 5",
4+
"category": "static_site_generator",
5+
"detect": {
6+
"npmDependencies": [{ "name": "gatsby", "version": ">=5.0.0" }],
7+
"excludedNpmDependencies": [],
8+
"configFiles": ["gatsby-config.js"]
9+
},
10+
"dev": {
11+
"command": "gatsby develop",
12+
"port": 8000,
13+
"pollingStrategies": [{ "name": "TCP" }, { "name": "HTTP" }]
14+
},
15+
"build": {
16+
"command": "gatsby build",
17+
"directory": "public"
18+
},
19+
"staticAssetsDirectory": "static",
20+
"env": {
21+
"GATSBY_LOGGER": "yurnalist",
22+
"GATSBY_PRECOMPILE_DEVELOP_FUNCTIONS": "true",
23+
"AWS_LAMBDA_JS_RUNTIME": "nodejs18.x",
24+
"NODE_VERSION": "18"
25+
},
26+
"logo": {
27+
"default": "/logos/gatsby/default.svg",
28+
"light": "/logos/gatsby/light.svg",
29+
"dark": "/logos/gatsby/dark.svg"
30+
},
31+
"plugins": [
32+
{
33+
"packageName": "@netlify/plugin-gatsby",
34+
"condition": { "minNodeVersion": "12.13.0" }
35+
}
36+
]
37+
}

src/frameworks/main.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export const FRAMEWORK_NAMES = [
88
'docusaurus-v2',
99
'eleventy',
1010
'gatsby',
11+
'gatsby5',
1112
'gridsome',
1213
'hexo',
1314
'hugo',

src/package.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,21 @@ export const getPackageJsonContent = function ({ packageJson }) {
77
}
88

99
const npmDependencies = getNpmDependencies(packageJson)
10+
const npmDependenciesVersions = getNpmDependenciesVersions(packageJson)
1011
const scripts = getScripts(packageJson)
11-
return { npmDependencies, scripts }
12+
return { npmDependencies, npmDependenciesVersions, scripts }
1213
}
1314

1415
// Retrieve `package.json` `dependencies` and `devDependencies` names
1516
const getNpmDependencies = function ({ dependencies, devDependencies }) {
1617
return [...getObjectKeys(dependencies), ...getObjectKeys(devDependencies)]
1718
}
1819

20+
// Retrieve `package.json` `dependencies` and `devDependencies` versions
21+
const getNpmDependenciesVersions = function ({ dependencies, devDependencies }) {
22+
return { ...devDependencies, ...dependencies }
23+
}
24+
1925
const getObjectKeys = function (value) {
2026
if (!isPlainObj(value)) {
2127
return []

test/detect.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,15 @@ if (nodeVersion === 'v8.3.0') {
4949
t.is(frameworks[0].plugins.length, 0)
5050
})
5151
}
52+
53+
test('Should detect specific version of dependencies', async (t) => {
54+
const frameworks = await getFrameworks('gatsby5')
55+
t.is(frameworks.length, 1)
56+
t.is(frameworks[0].id, 'gatsby5')
57+
})
58+
59+
test('Should not detect specific version of dependencies if version does not match', async (t) => {
60+
const frameworks = await getFrameworks('gatsby4')
61+
t.is(frameworks.length, 1)
62+
t.is(frameworks[0].id, 'gatsby')
63+
})

test/fixtures/gatsby4/gatsby-config.js

Whitespace-only changes.

test/fixtures/gatsby4/package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "test",
3+
"version": "1.0.0",
4+
"dependencies": {
5+
"gatsby": "4"
6+
}
7+
}

test/fixtures/gatsby5/gatsby-config.js

Whitespace-only changes.

test/fixtures/gatsby5/package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "test",
3+
"version": "1.0.0",
4+
"dependencies": {
5+
"gatsby": "5"
6+
}
7+
}

test/frameworks.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,17 @@ const FRAMEWORK_JSON_SCHEMA = {
7070
properties: {
7171
npmDependencies: {
7272
type: 'array',
73-
items: { type: 'string', minLength: 1 },
73+
items: {
74+
anyOf: [
75+
{ type: 'string', minLength: 1 },
76+
{
77+
type: 'object',
78+
required: ['name'],
79+
additionalProperties: false,
80+
properties: { name: { type: 'string', minLength: 1 }, version: { type: 'string', minLength: 1 } },
81+
},
82+
],
83+
},
7484
},
7585
excludedNpmDependencies: {
7686
type: 'array',

0 commit comments

Comments
 (0)