Skip to content

Commit

Permalink
refactor(core): use semver.minVersion over custom function
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Sep 7, 2021
1 parent 8d9af82 commit 1728000
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions packages/@sanity/core/src/util/checkRequiredDependencies.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path'
import execa from 'execa'
import semver from 'semver'
import semver, {SemVer} from 'semver'
import resolveFrom from 'resolve-from'
import {readJSON, access} from 'fs-extra'
import oneline from 'oneline'
Expand Down Expand Up @@ -46,6 +46,21 @@ export async function checkRequiredDependencies(context: CliCommandContext): Pro
return
}

// Theoretically the version specified in package.json could be incorrect, eg `foo`
let minDeclaredStyledComponentsVersion: SemVer | null = null
try {
minDeclaredStyledComponentsVersion = semver.minVersion(declaredStyledComponentsVersion)
} catch (err) {
// Intentional fall-through (variable will be left as null, throwing below)
}

if (!minDeclaredStyledComponentsVersion) {
throw new Error(oneline`
Declared dependency \`styled-components\` has an invalid version range:
\`${declaredStyledComponentsVersion}\`.
`)
}

// The declared version should be semver-compatible with the version specified as a
// peer dependency in `@sanity/base`. If not, we should tell the user to change it.
//
Expand All @@ -54,10 +69,7 @@ export async function checkRequiredDependencies(context: CliCommandContext): Pro
// (^x.x.x / ~x.x.x / x.x.x)
if (
isComparableRange(declaredStyledComponentsVersion) &&
!semver.satisfies(
normalizeVersion(declaredStyledComponentsVersion),
wantedStyledComponentsVersionRange
)
!semver.satisfies(minDeclaredStyledComponentsVersion, wantedStyledComponentsVersionRange)
) {
output.warn(oneline`
Declared version of styled-components (${declaredStyledComponentsVersion})
Expand Down Expand Up @@ -203,10 +215,6 @@ function isComparableRange(range: string): boolean {
return /^[\^~]?\d+(\.\d+)?(\.\d+)?$/.test(range)
}

function normalizeVersion(version: string): string {
return version.replace(/^[\^~]/, '')
}

/**
* fs-extra `fse.exists` seems both incorrectly typed (callback based) and is
* deprecated because it's a potential race condition to check for a file and
Expand Down

2 comments on commit 1728000

@vercel
Copy link

@vercel vercel bot commented on 1728000 Sep 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

test-studio – ./

test-studio-git-next.sanity.build
test-studio.sanity.build

@vercel
Copy link

@vercel vercel bot commented on 1728000 Sep 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

perf-studio – ./

perf-studio-git-next.sanity.build
perf-studio.sanity.build

Please sign in to comment.