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

Commit 5735d2e

Browse files
danezkodiakhq[bot]
andauthored
fix: handle version for frameworks which are not detected by npm package (#842)
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent 2506c23 commit 5735d2e

File tree

6 files changed

+116
-64
lines changed

6 files changed

+116
-64
lines changed

src/core.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ const getContext = (context) => {
6363
*
6464
* @param {Context} context - Context
6565
*
66-
* @returns {Framework[]} frameworks - Frameworks used by a project
66+
* @returns {Promise<Framework[]>} frameworks - Frameworks used by a project
6767
*/
6868
export const listFrameworks = async function (context) {
6969
const { pathExists, packageJson, packageJsonPath, nodeVersion } = getContext(context)
@@ -85,7 +85,7 @@ export const listFrameworks = async function (context) {
8585
* @param {string} frameworkId - Id such as `"gatsby"`
8686
* @param {Context} [context] - Context
8787
*
88-
* @returns {boolean} result - Whether the project uses this framework
88+
* @returns {Promise<boolean>} result - Whether the project uses this framework
8989
*/
9090
export const hasFramework = async function (frameworkId, context) {
9191
const framework = getFrameworkById(frameworkId)
@@ -101,7 +101,7 @@ export const hasFramework = async function (frameworkId, context) {
101101
* @param {string} frameworkId - Id such as `"gatsby"`
102102
* @param {Context} [context] - Context
103103
*
104-
* @returns {Framework} framework - Framework used by a project
104+
* @returns {Promise<Framework>} framework - Framework used by a project
105105
*/
106106
export const getFramework = async function (frameworkId, context) {
107107
const framework = getFrameworkById(frameworkId)

src/main.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { join } from 'path'
2-
import { cwd, chdir } from 'process'
2+
import { cwd } from 'process'
33

44
import { findUp } from 'find-up'
55

@@ -53,28 +53,22 @@ import { listFrameworks as list, hasFramework as has, getFramework as get } from
5353
* @returns {Promise<Framework>}
5454
*/
5555
const getFrameworkVersion = async (projectDir, frameworkInfo) => {
56-
// Need to change the CWD to the project directory in order to make sure we find and use the correct
57-
// package.json
58-
const originalCwd = cwd()
59-
const returnToOriginalDirectory = () => {
60-
chdir(originalCwd)
56+
if (!frameworkInfo.package || !frameworkInfo.package.name) {
57+
return frameworkInfo
6158
}
62-
chdir(projectDir)
6359

6460
const npmPackage = frameworkInfo.package.name
6561

6662
// Get path of package.json for the installed framework. We need to traverse up the directories
6763
// in the event that the project uses something like npm workspaces, and the installed framework package
6864
// has been hoisted to the root directory of the project (which differs from the directory of the project/application being built)
69-
const installedFrameworkPath = await findUp(join('node_modules', npmPackage, 'package.json'))
65+
const installedFrameworkPath = await findUp(join('node_modules', npmPackage, 'package.json'), { cwd: projectDir })
7066
const { packageJson } = await getPackageJson(installedFrameworkPath)
7167

72-
returnToOriginalDirectory()
73-
7468
return {
7569
...frameworkInfo,
7670
package: {
77-
name: frameworkInfo.package.name,
71+
name: npmPackage,
7872
version: packageJson.version || 'unknown',
7973
},
8074
}
@@ -85,7 +79,7 @@ const getFrameworkVersion = async (projectDir, frameworkInfo) => {
8579
*
8680
* @param {Options} options - Options
8781
*
88-
* @returns {Framework[]} frameworks - Frameworks used by a project
82+
* @returns {Promise<Framework[]>} frameworks - Frameworks used by a project
8983
*/
9084
export const listFrameworks = async function (opts) {
9185
const context = await getContext(opts)
@@ -96,7 +90,13 @@ export const listFrameworks = async function (opts) {
9690
const settledPromises = await Promise.allSettled(
9791
frameworkList.map((framework) => getFrameworkVersion(projectDir, framework)),
9892
)
99-
const updatedList = settledPromises.map((result) => result.value)
93+
const updatedList = settledPromises.map((result) => {
94+
if (result.status === 'fulfilled') {
95+
return result.value
96+
}
97+
98+
throw result.reason
99+
})
100100

101101
return updatedList
102102
}
@@ -107,22 +107,22 @@ export const listFrameworks = async function (opts) {
107107
* @param {string} frameworkId - Id such as `"gatsby"`
108108
* @param {Options} [options] - Context
109109
*
110-
* @returns {boolean} result - Whether the project uses this framework
110+
* @returns {Promise<boolean>} result - Whether the project uses this framework
111111
*/
112112
export const hasFramework = async function (frameworkId, options) {
113113
const context = await getContext(options)
114-
return await has(frameworkId, context)
114+
return has(frameworkId, context)
115115
}
116116

117117
/**
118118
* Return some information about a framework used by a project.
119119
*
120120
* @param {string} frameworkId - Id such as `"gatsby"`
121-
* @param {Context} [context] - Context
121+
* @param {Options} [options] - Context
122122
*
123-
* @returns {Framework} framework - Framework used by a project
123+
* @returns {Promise<Framework>} framework - Framework used by a project
124124
*/
125125
export const getFramework = async function (frameworkId, options) {
126126
const context = await getContext(options)
127-
return await get(frameworkId, context)
127+
return get(frameworkId, context)
128128
}

test/fixtures/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"description": "This is only here so fixtures never reach outside of this folder when finding package.json"
3+
}

test/main.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ test('Should return the version of each framework when multiple are detected', a
2222
t.snapshot(frameworks)
2323
})
2424

25+
test('Should return the version of a framework that is not detected by npm package', async (t) => {
26+
const frameworks = await getFrameworks('no_package')
27+
t.snapshot(frameworks)
28+
})
29+
2530
test('Should return the version of the framework when the installed package is hoisted to the root project directory', async (t) => {
2631
const frameworks = await getFrameworks('monorepos/app1')
2732
t.snapshot(frameworks)

test/snapshots/main.js.md

Lines changed: 87 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,6 @@ The actual snapshot is saved in `main.js.snap`.
44

55
Generated by [AVA](https://avajs.dev).
66

7-
## Should return the version of the framework when the installed package is hoisted to the root project directory
8-
9-
> Snapshot 1
10-
11-
[
12-
{
13-
build: {
14-
commands: [
15-
'next build',
16-
],
17-
directory: '.next',
18-
},
19-
category: 'static_site_generator',
20-
dev: {
21-
commands: [
22-
'next',
23-
],
24-
pollingStrategies: [
25-
{
26-
name: 'TCP',
27-
},
28-
],
29-
port: 3000,
30-
},
31-
env: {},
32-
id: 'next',
33-
logo: {
34-
dark: 'https://framework-info.netlify.app/logos/nextjs/dark.svg',
35-
default: 'https://framework-info.netlify.app/logos/nextjs/light.svg',
36-
light: 'https://framework-info.netlify.app/logos/nextjs/light.svg',
37-
},
38-
name: 'Next.js',
39-
package: {
40-
name: 'next',
41-
version: '3.2.1',
42-
},
43-
plugins: [
44-
'@netlify/plugin-nextjs',
45-
],
46-
staticAssetsDirectory: undefined,
47-
},
48-
]
49-
507
## Should detect frameworks
518

529
> Snapshot 1
@@ -172,6 +129,93 @@ Generated by [AVA](https://avajs.dev).
172129
},
173130
]
174131

132+
## Should return the version of a framework that is not detected by npm package
133+
134+
> Snapshot 1
135+
136+
[
137+
{
138+
build: {
139+
commands: [
140+
'bundle exec middleman build',
141+
],
142+
directory: 'build',
143+
},
144+
category: 'static_site_generator',
145+
dev: {
146+
commands: [
147+
'bundle exec middleman server',
148+
],
149+
pollingStrategies: [
150+
{
151+
name: 'TCP',
152+
},
153+
{
154+
name: 'HTTP',
155+
},
156+
],
157+
port: 4567,
158+
},
159+
env: {},
160+
id: 'middleman',
161+
logo: {
162+
dark: 'https://framework-info.netlify.app/logos/middleman/default.svg',
163+
default: 'https://framework-info.netlify.app/logos/middleman/default.svg',
164+
light: 'https://framework-info.netlify.app/logos/middleman/default.svg',
165+
},
166+
name: 'Middleman',
167+
package: {
168+
name: undefined,
169+
version: 'unknown',
170+
},
171+
plugins: [],
172+
staticAssetsDirectory: undefined,
173+
},
174+
]
175+
176+
## Should return the version of the framework when the installed package is hoisted to the root project directory
177+
178+
> Snapshot 1
179+
180+
[
181+
{
182+
build: {
183+
commands: [
184+
'next build',
185+
],
186+
directory: '.next',
187+
},
188+
category: 'static_site_generator',
189+
dev: {
190+
commands: [
191+
'next',
192+
],
193+
pollingStrategies: [
194+
{
195+
name: 'TCP',
196+
},
197+
],
198+
port: 3000,
199+
},
200+
env: {},
201+
id: 'next',
202+
logo: {
203+
dark: 'https://framework-info.netlify.app/logos/nextjs/dark.svg',
204+
default: 'https://framework-info.netlify.app/logos/nextjs/light.svg',
205+
light: 'https://framework-info.netlify.app/logos/nextjs/light.svg',
206+
},
207+
name: 'Next.js',
208+
package: {
209+
name: 'next',
210+
version: '3.2.1',
211+
},
212+
plugins: [
213+
'@netlify/plugin-nextjs',
214+
],
215+
staticAssetsDirectory: undefined,
216+
},
217+
]
218+
175219
## Should allow getting a specific framework
176220

177221
> Snapshot 1

test/snapshots/main.js.snap

248 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)