Skip to content
This repository was archived by the owner on Jan 11, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const getContext = (context) => {
*
* @param {Context} context - Context
*
* @returns {Framework[]} frameworks - Frameworks used by a project
* @returns {Promise<Framework[]>} frameworks - Frameworks used by a project
*/
export const listFrameworks = async function (context) {
const { pathExists, packageJson, packageJsonPath, nodeVersion } = getContext(context)
Expand All @@ -85,7 +85,7 @@ export const listFrameworks = async function (context) {
* @param {string} frameworkId - Id such as `"gatsby"`
* @param {Context} [context] - Context
*
* @returns {boolean} result - Whether the project uses this framework
* @returns {Promise<boolean>} result - Whether the project uses this framework
*/
export const hasFramework = async function (frameworkId, context) {
const framework = getFrameworkById(frameworkId)
Expand All @@ -101,7 +101,7 @@ export const hasFramework = async function (frameworkId, context) {
* @param {string} frameworkId - Id such as `"gatsby"`
* @param {Context} [context] - Context
*
* @returns {Framework} framework - Framework used by a project
* @returns {Promise<Framework>} framework - Framework used by a project
*/
export const getFramework = async function (frameworkId, context) {
const framework = getFrameworkById(frameworkId)
Expand Down
36 changes: 18 additions & 18 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { join } from 'path'
import { cwd, chdir } from 'process'
import { cwd } from 'process'

import { findUp } from 'find-up'

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

const npmPackage = frameworkInfo.package.name

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

returnToOriginalDirectory()

return {
...frameworkInfo,
package: {
name: frameworkInfo.package.name,
name: npmPackage,
version: packageJson.version || 'unknown',
},
}
Expand All @@ -85,7 +79,7 @@ const getFrameworkVersion = async (projectDir, frameworkInfo) => {
*
* @param {Options} options - Options
*
* @returns {Framework[]} frameworks - Frameworks used by a project
* @returns {Promise<Framework[]>} frameworks - Frameworks used by a project
*/
export const listFrameworks = async function (opts) {
const context = await getContext(opts)
Expand All @@ -96,7 +90,13 @@ export const listFrameworks = async function (opts) {
const settledPromises = await Promise.allSettled(
frameworkList.map((framework) => getFrameworkVersion(projectDir, framework)),
)
const updatedList = settledPromises.map((result) => result.value)
const updatedList = settledPromises.map((result) => {
if (result.status === 'fulfilled') {
return result.value
}

throw result.reason
})

return updatedList
}
Expand All @@ -107,22 +107,22 @@ export const listFrameworks = async function (opts) {
* @param {string} frameworkId - Id such as `"gatsby"`
* @param {Options} [options] - Context
*
* @returns {boolean} result - Whether the project uses this framework
* @returns {Promise<boolean>} result - Whether the project uses this framework
*/
export const hasFramework = async function (frameworkId, options) {
const context = await getContext(options)
return await has(frameworkId, context)
return has(frameworkId, context)
}

/**
* Return some information about a framework used by a project.
*
* @param {string} frameworkId - Id such as `"gatsby"`
* @param {Context} [context] - Context
* @param {Options} [options] - Context
*
* @returns {Framework} framework - Framework used by a project
* @returns {Promise<Framework>} framework - Framework used by a project
*/
export const getFramework = async function (frameworkId, options) {
const context = await getContext(options)
return await get(frameworkId, context)
return get(frameworkId, context)
}
3 changes: 3 additions & 0 deletions test/fixtures/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"description": "This is only here so fixtures never reach outside of this folder when finding package.json"
}
5 changes: 5 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ test('Should return the version of each framework when multiple are detected', a
t.snapshot(frameworks)
})

test('Should return the version of a framework that is not detected by npm package', async (t) => {
const frameworks = await getFrameworks('no_package')
t.snapshot(frameworks)
})

test('Should return the version of the framework when the installed package is hoisted to the root project directory', async (t) => {
const frameworks = await getFrameworks('monorepos/app1')
t.snapshot(frameworks)
Expand Down
130 changes: 87 additions & 43 deletions test/snapshots/main.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,6 @@ The actual snapshot is saved in `main.js.snap`.

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

## Should return the version of the framework when the installed package is hoisted to the root project directory

> Snapshot 1

[
{
build: {
commands: [
'next build',
],
directory: '.next',
},
category: 'static_site_generator',
dev: {
commands: [
'next',
],
pollingStrategies: [
{
name: 'TCP',
},
],
port: 3000,
},
env: {},
id: 'next',
logo: {
dark: 'https://framework-info.netlify.app/logos/nextjs/dark.svg',
default: 'https://framework-info.netlify.app/logos/nextjs/light.svg',
light: 'https://framework-info.netlify.app/logos/nextjs/light.svg',
},
name: 'Next.js',
package: {
name: 'next',
version: '3.2.1',
},
plugins: [
'@netlify/plugin-nextjs',
],
staticAssetsDirectory: undefined,
},
]

## Should detect frameworks

> Snapshot 1
Expand Down Expand Up @@ -172,6 +129,93 @@ Generated by [AVA](https://avajs.dev).
},
]

## Should return the version of a framework that is not detected by npm package

> Snapshot 1

[
{
build: {
commands: [
'bundle exec middleman build',
],
directory: 'build',
},
category: 'static_site_generator',
dev: {
commands: [
'bundle exec middleman server',
],
pollingStrategies: [
{
name: 'TCP',
},
{
name: 'HTTP',
},
],
port: 4567,
},
env: {},
id: 'middleman',
logo: {
dark: 'https://framework-info.netlify.app/logos/middleman/default.svg',
default: 'https://framework-info.netlify.app/logos/middleman/default.svg',
light: 'https://framework-info.netlify.app/logos/middleman/default.svg',
},
name: 'Middleman',
package: {
name: undefined,
version: 'unknown',
},
plugins: [],
staticAssetsDirectory: undefined,
},
]

## Should return the version of the framework when the installed package is hoisted to the root project directory

> Snapshot 1

[
{
build: {
commands: [
'next build',
],
directory: '.next',
},
category: 'static_site_generator',
dev: {
commands: [
'next',
],
pollingStrategies: [
{
name: 'TCP',
},
],
port: 3000,
},
env: {},
id: 'next',
logo: {
dark: 'https://framework-info.netlify.app/logos/nextjs/dark.svg',
default: 'https://framework-info.netlify.app/logos/nextjs/light.svg',
light: 'https://framework-info.netlify.app/logos/nextjs/light.svg',
},
name: 'Next.js',
package: {
name: 'next',
version: '3.2.1',
},
plugins: [
'@netlify/plugin-nextjs',
],
staticAssetsDirectory: undefined,
},
]

## Should allow getting a specific framework

> Snapshot 1
Expand Down
Binary file modified test/snapshots/main.js.snap
Binary file not shown.