Skip to content

Commit

Permalink
fix: migrate context to TS and ensure we do not access version on und…
Browse files Browse the repository at this point in the history
…efined
  • Loading branch information
danez committed Jan 12, 2023
1 parent 898c210 commit c7783b9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
25 changes: 18 additions & 7 deletions packages/framework-info/src/context.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import { cwd, version as nodejsVersion } from 'process'

import isPlainObj from 'is-plain-obj'
import { locatePath } from 'locate-path'
import { readPackageUp } from 'read-pkg-up'

export const getPackageJson = async (projectDir) => {
/*
interface PackageJsonInfo {
packageJson?: PackageJson
packageJsonPath?: string
}
interface Context extends PackageJsonInfo {
pathExists: (path: string) => Promise<boolean>
nodeVersion: string
}
*/

export const getPackageJson = async (projectDir /*: string*/) /*: Promise<PackageJsonInfo>*/ => {
try {
const result = await readPackageUp({ cwd: projectDir, normalize: false })
if (result === undefined) {
Expand All @@ -13,18 +24,18 @@ export const getPackageJson = async (projectDir) => {

const { packageJson, path: packageJsonPath } = result

if (!isPlainObj(packageJson)) {
return { packageJsonPath }
}

return { packageJson, packageJsonPath }
} catch {
return {}
}
}

export const getContext = async ({ projectDir = cwd(), nodeVersion = nodejsVersion } = {}) => {
export const getContext = async (
projectDir /*: string*/ = cwd(),
nodeVersion /*: string*/ = nodejsVersion,
) /*: Promise<Context>*/ => {
const { packageJson, packageJsonPath = projectDir } = await getPackageJson(projectDir)

return {
pathExists: async (path) => (await locatePath([path], { type: 'file', cwd: projectDir })) !== undefined,
packageJson,
Expand Down
8 changes: 4 additions & 4 deletions packages/framework-info/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const getFrameworkVersion = async (projectDir, frameworkInfo) => {
...frameworkInfo,
package: {
name: npmPackage,
version: packageJson.version || 'unknown',
version: packageJson?.version || 'unknown',
},
}
}
Expand All @@ -82,7 +82,7 @@ const getFrameworkVersion = async (projectDir, frameworkInfo) => {
* @returns {Promise<Framework[]>} frameworks - Frameworks used by a project
*/
export const listFrameworks = async function (opts) {
const context = await getContext(opts)
const context = await getContext(opts.projectDir, opts.nodeVersion)
const frameworkList = await list(context)

const projectDir = opts && opts.projectDir ? opts.projectDir : cwd()
Expand Down Expand Up @@ -110,7 +110,7 @@ export const listFrameworks = async function (opts) {
* @returns {Promise<boolean>} result - Whether the project uses this framework
*/
export const hasFramework = async function (frameworkId, options) {
const context = await getContext(options)
const context = await getContext(options.projectDir, options.nodeVersion)
return has(frameworkId, context)
}

Expand All @@ -123,6 +123,6 @@ export const hasFramework = async function (frameworkId, options) {
* @returns {Promise<Framework>} framework - Framework used by a project
*/
export const getFramework = async function (frameworkId, options) {
const context = await getContext(options)
const context = await getContext(options.projectDir, options.nodeVersion)
return get(frameworkId, context)
}

0 comments on commit c7783b9

Please sign in to comment.