|
| 1 | +<script setup lang="ts"> |
| 2 | +import { computed, ref } from 'vue' |
| 3 | +
|
| 4 | +declare const __VITEST_VERSION__: string |
| 5 | +
|
| 6 | +const DIGITS_ONLY_RE = /^\d+$/ |
| 7 | +const NEGATIVE_VERSION_RE = /-\d/ |
| 8 | +
|
| 9 | +const supportedVersionMessage = { |
| 10 | + color: 'var(--vp-c-brand-1)', |
| 11 | + text: 'supported', |
| 12 | +} |
| 13 | +const notSupportedVersionMessage = { |
| 14 | + color: 'var(--vp-c-danger-1)', |
| 15 | + text: 'not supported', |
| 16 | +} |
| 17 | +const previousMajorLatestMinors: Record<string, string> = { |
| 18 | + 1: '1.6', |
| 19 | + 2: '2.1', |
| 20 | + 3: '3.2', |
| 21 | + 4: '4.1', |
| 22 | +} |
| 23 | +
|
| 24 | +// Current latest Vitest version and support info |
| 25 | +const parsedViteVersion = parseVersion(__VITEST_VERSION__)! |
| 26 | +const supportInfo = computeSupportInfo(parsedViteVersion) |
| 27 | +
|
| 28 | +// Check supported version input |
| 29 | +const checkedVersion = ref(`${Math.max(parsedViteVersion.major - 2, 2)}.0.0`) |
| 30 | +const checkedResult = computed(() => { |
| 31 | + const version = checkedVersion.value |
| 32 | + if (!isValidVitestVersion(version)) { |
| 33 | + return notSupportedVersionMessage |
| 34 | + } |
| 35 | +
|
| 36 | + const parsedVersion = parseVersion(checkedVersion.value) |
| 37 | + if (!parsedVersion) { |
| 38 | + return notSupportedVersionMessage |
| 39 | + } |
| 40 | +
|
| 41 | + const satisfies = (targetVersion: string) => { |
| 42 | + const compared = parseVersion(targetVersion)! |
| 43 | + return ( |
| 44 | + parsedVersion.major === compared.major |
| 45 | + && parsedVersion.minor >= compared.minor |
| 46 | + ) |
| 47 | + } |
| 48 | + const satisfiesOneSupportedVersion |
| 49 | + = parsedVersion.major >= parsedViteVersion.major // Treat future major versions as supported |
| 50 | + || supportInfo.regularPatches.some(satisfies) |
| 51 | + || supportInfo.importantFixes.some(satisfies) |
| 52 | + || supportInfo.securityPatches.some(satisfies) |
| 53 | +
|
| 54 | + return satisfiesOneSupportedVersion |
| 55 | + ? supportedVersionMessage |
| 56 | + : notSupportedVersionMessage |
| 57 | +}) |
| 58 | +
|
| 59 | +function parseVersion(version: string) { |
| 60 | + let [major, minor, patch] = version.split('.').map((v) => { |
| 61 | + const num = DIGITS_ONLY_RE.exec(v)?.[0] |
| 62 | + return num ? Number.parseInt(num) : null |
| 63 | + }) |
| 64 | + if (!major) { |
| 65 | + return null |
| 66 | + } |
| 67 | + minor ??= 0 |
| 68 | + patch ??= 0 |
| 69 | +
|
| 70 | + return { major, minor, patch } |
| 71 | +} |
| 72 | +
|
| 73 | +function computeSupportInfo( |
| 74 | + version: NonNullable<ReturnType<typeof parseVersion>>, |
| 75 | +) { |
| 76 | + const { major, minor } = version |
| 77 | + const f = (versions: string[]) => { |
| 78 | + return versions |
| 79 | + .map(v => previousMajorLatestMinors[v] ?? v) |
| 80 | + .filter((version) => { |
| 81 | + if (!isValidVitestVersion(version)) { |
| 82 | + return false |
| 83 | + } |
| 84 | + // Negative versions are invalid |
| 85 | + if (NEGATIVE_VERSION_RE.test(version)) { |
| 86 | + return false |
| 87 | + } |
| 88 | + return true |
| 89 | + }) |
| 90 | + } |
| 91 | +
|
| 92 | + return { |
| 93 | + regularPatches: f([`${major}.${minor}`]), |
| 94 | + importantFixes: f([`${major - 1}`, `${major}.${minor - 1}`]), |
| 95 | + // unlike vite, we only support the last major version |
| 96 | + securityPatches: f([`${major - 1}`, `${major}.${minor - 1}`]), |
| 97 | + } |
| 98 | +} |
| 99 | +
|
| 100 | +function versionsToText(versions: string[]) { |
| 101 | + versions = versions.map(v => `<code>vitest@${v}</code>`) |
| 102 | + if (versions.length === 0) { |
| 103 | + return '' |
| 104 | + } |
| 105 | + if (versions.length === 1) { |
| 106 | + return versions[0] |
| 107 | + } |
| 108 | + return ( |
| 109 | + `${versions.slice(0, -1).join(', ')} and ${versions.at(-1)}` |
| 110 | + ) |
| 111 | +} |
| 112 | +
|
| 113 | +function isValidVitestVersion(version: string) { |
| 114 | + if (version.length === 1) { |
| 115 | + version += '.' |
| 116 | + } |
| 117 | + // Vitest 0.x shouldn't be mentioned |
| 118 | + if (version.startsWith('0.')) { |
| 119 | + return false |
| 120 | + } |
| 121 | + return true |
| 122 | +} |
| 123 | +</script> |
| 124 | + |
| 125 | +<template> |
| 126 | + <div> |
| 127 | + <ul> |
| 128 | + <li v-if="supportInfo.regularPatches.length"> |
| 129 | + Regular patches are released for |
| 130 | + <span v-html="versionsToText(supportInfo.regularPatches)" />. |
| 131 | + </li> |
| 132 | + <li v-if="supportInfo.importantFixes.length"> |
| 133 | + Important fixes and security patches are backported to |
| 134 | + <span v-html="versionsToText(supportInfo.importantFixes)" />. |
| 135 | + </li> |
| 136 | + <li> |
| 137 | + All versions before these are no longer supported. Users should upgrade |
| 138 | + to receive updates. |
| 139 | + </li> |
| 140 | + </ul> |
| 141 | + <p> |
| 142 | + If you're using Vitest |
| 143 | + <input |
| 144 | + v-model="checkedVersion" |
| 145 | + class="checked-input" |
| 146 | + type="text" |
| 147 | + placeholder="0.0.0" |
| 148 | + >, it is |
| 149 | + <strong :style="{ color: checkedResult.color }">{{ |
| 150 | + checkedResult.text |
| 151 | + }}</strong>. |
| 152 | + </p> |
| 153 | + </div> |
| 154 | +</template> |
| 155 | + |
| 156 | +<style scoped> |
| 157 | +.checked-input { |
| 158 | + display: inline-block; |
| 159 | + padding: 0px 5px; |
| 160 | + width: 100px; |
| 161 | + color: var(--vp-c-text-1); |
| 162 | + background: var(--vp-c-bg-soft); |
| 163 | + font-size: var(--vp-code-font-size); |
| 164 | + font-family: var(--vp-font-family-mono); |
| 165 | + border: 1px solid var(--vp-c-divider); |
| 166 | + border-radius: 5px; |
| 167 | + transition: border-color 0.1s; |
| 168 | +} |
| 169 | +
|
| 170 | +.checked-input:focus, |
| 171 | +.checked-input:hover { |
| 172 | + border-color: var(--vp-c-brand); |
| 173 | +} |
| 174 | +</style> |
0 commit comments