2021-11-21 16:14:46 +00:00
|
|
|
import satisfies from 'semver/functions/satisfies.js' // npm/node-semver#381
|
2021-12-21 13:57:26 +00:00
|
|
|
import type { Nuxt, NuxtCompatibility, NuxtCompatibilityIssues } from '@nuxt/schema'
|
2021-11-21 16:14:46 +00:00
|
|
|
import { useNuxt } from './context'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check version constraints and return incompatibility issues as an array
|
|
|
|
*/
|
2021-12-21 13:57:26 +00:00
|
|
|
export async function checkNuxtCompatibility (constraints: NuxtCompatibility, nuxt: Nuxt = useNuxt()): Promise<NuxtCompatibilityIssues> {
|
2021-11-21 16:14:46 +00:00
|
|
|
const issues: NuxtCompatibilityIssues = []
|
2021-12-21 13:57:26 +00:00
|
|
|
|
|
|
|
// Nuxt version check
|
2021-11-21 16:14:46 +00:00
|
|
|
if (constraints.nuxt) {
|
|
|
|
const nuxtVersion = getNuxtVersion(nuxt)
|
2022-08-31 19:18:05 +00:00
|
|
|
const nuxtSemanticVersion = nuxtVersion
|
2022-09-03 13:08:23 +00:00
|
|
|
.replace(/-[0-9]+\.[0-9a-f]{7,8}/, '') // Remove edge prefix
|
2022-08-31 19:18:05 +00:00
|
|
|
if (!satisfies(nuxtSemanticVersion, constraints.nuxt, { includePrerelease: true })) {
|
2021-11-21 16:14:46 +00:00
|
|
|
issues.push({
|
|
|
|
name: 'nuxt',
|
|
|
|
message: `Nuxt version \`${constraints.nuxt}\` is required but currently using \`${nuxtVersion}\``
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-12-21 13:57:26 +00:00
|
|
|
|
|
|
|
// Bridge compatibility check
|
|
|
|
if (isNuxt2(nuxt)) {
|
2022-06-08 19:49:11 +00:00
|
|
|
const bridgeRequirement = constraints.bridge
|
2021-12-21 13:57:26 +00:00
|
|
|
const hasBridge = !!(nuxt.options as any).bridge
|
|
|
|
if (bridgeRequirement === true && !hasBridge) {
|
|
|
|
issues.push({
|
|
|
|
name: 'bridge',
|
|
|
|
message: 'Nuxt bridge is required'
|
|
|
|
})
|
|
|
|
} else if (bridgeRequirement === false && hasBridge) {
|
|
|
|
issues.push({
|
|
|
|
name: 'bridge',
|
|
|
|
message: 'Nuxt bridge is not supported'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow extending compatibility checks
|
|
|
|
await nuxt.callHook('kit:compatibility', constraints, issues)
|
|
|
|
|
|
|
|
// Issues formatter
|
|
|
|
issues.toString = () =>
|
|
|
|
issues.map(issue => ` - [${issue.name}] ${issue.message}`).join('\n')
|
|
|
|
|
2021-11-21 16:14:46 +00:00
|
|
|
return issues
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check version constraints and throw a detailed error if has any, otherwise returns true
|
|
|
|
*/
|
2021-12-21 13:57:26 +00:00
|
|
|
export async function assertNuxtCompatibility (constraints: NuxtCompatibility, nuxt: Nuxt = useNuxt()): Promise<true> {
|
|
|
|
const issues = await checkNuxtCompatibility(constraints, nuxt)
|
2021-11-21 16:14:46 +00:00
|
|
|
if (issues.length) {
|
|
|
|
throw new Error('Nuxt compatibility issues found:\n' + issues.toString())
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check version constraints and return true if passed, otherwise returns false
|
|
|
|
*/
|
2021-12-21 13:57:26 +00:00
|
|
|
export async function hasNuxtCompatibility (constraints: NuxtCompatibility, nuxt: Nuxt = useNuxt()): Promise<boolean> {
|
|
|
|
const issues = await checkNuxtCompatibility(constraints, nuxt)
|
|
|
|
return !issues.length
|
2021-11-21 16:14:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if current nuxt instance is version 2 legacy
|
|
|
|
*/
|
|
|
|
export function isNuxt2 (nuxt: Nuxt = useNuxt()) {
|
|
|
|
return getNuxtVersion(nuxt).startsWith('2.')
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if current nuxt instance is version 3
|
|
|
|
*/
|
|
|
|
export function isNuxt3 (nuxt: Nuxt = useNuxt()) {
|
|
|
|
return getNuxtVersion(nuxt).startsWith('3.')
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get nuxt version
|
|
|
|
*/
|
|
|
|
export function getNuxtVersion (nuxt: Nuxt | any = useNuxt() /* TODO: LegacyNuxt */) {
|
|
|
|
const version = (nuxt?._version || nuxt?.version || nuxt?.constructor?.version || '').replace(/^v/g, '')
|
|
|
|
if (!version) {
|
2022-06-08 19:49:11 +00:00
|
|
|
throw new Error('Cannot determine nuxt version! Is current instance passed?')
|
2021-11-21 16:14:46 +00:00
|
|
|
}
|
|
|
|
return version
|
|
|
|
}
|