|
| 1 | +import type { EmojiVersion } from "./lockfile"; |
1 | 2 | import consola from "consola"; |
2 | 3 | import semver from "semver"; |
| 4 | +import { isEmojiVersionValid } from "./utils"; |
3 | 5 |
|
4 | 6 | export interface DraftVersion { |
5 | 7 | emoji_version: string; |
@@ -167,3 +169,134 @@ export function extractUnicodeVersion(emojiVersion: string | null, unicodeVersio |
167 | 169 | return "6.0"; |
168 | 170 | } |
169 | 171 | } |
| 172 | + |
| 173 | +/** |
| 174 | + * Retrieves all available emoji versions from Unicode.org. |
| 175 | + * This function fetches both the root Unicode directory and the emoji-specific directory |
| 176 | + * to compile a comprehensive list of valid emoji versions. |
| 177 | + * |
| 178 | + * The function performs the following steps: |
| 179 | + * 1. Fetches content from Unicode.org's public directories |
| 180 | + * 2. Extracts version numbers using regex |
| 181 | + * 3. Validates each version |
| 182 | + * 4. Normalizes version numbers to valid semver format |
| 183 | + * |
| 184 | + * @throws {Error} When either the root or emoji page fetch fails |
| 185 | + * @returns {Promise<EmojiVersion[]>} A promise that resolves to an array of emoji versions, |
| 186 | + * sorted according to semver rules |
| 187 | + */ |
| 188 | +export async function getAllEmojiVersions(): Promise<EmojiVersion[]> { |
| 189 | + const [rootResult, emojiResult] = await Promise.allSettled([ |
| 190 | + "https://unicode.org/Public/", |
| 191 | + "https://unicode.org/Public/emoji/", |
| 192 | + ].map(async (url) => { |
| 193 | + const res = await fetch(url); |
| 194 | + |
| 195 | + if (!res.ok) { |
| 196 | + throw new Error(`failed to fetch ${url}: ${res.statusText}`); |
| 197 | + } |
| 198 | + |
| 199 | + return res.text(); |
| 200 | + })); |
| 201 | + |
| 202 | + if (rootResult == null || emojiResult == null) { |
| 203 | + throw new Error("failed to fetch root or emoji page"); |
| 204 | + } |
| 205 | + |
| 206 | + if (rootResult.status === "rejected" || emojiResult.status === "rejected") { |
| 207 | + consola.error({ |
| 208 | + root: rootResult.status === "rejected" ? rootResult.reason : "ok", |
| 209 | + emoji: emojiResult.status === "rejected" ? emojiResult.reason : "ok", |
| 210 | + }); |
| 211 | + |
| 212 | + throw new Error("failed to fetch root or emoji page"); |
| 213 | + } |
| 214 | + |
| 215 | + const rootHtml = rootResult.value; |
| 216 | + const emojiHtml = emojiResult.value; |
| 217 | + |
| 218 | + const versionRegex = /href="(\d+\.\d+(?:\.\d+)?)\/?"/g; |
| 219 | + |
| 220 | + const draft = await getCurrentDraftVersion(); |
| 221 | + |
| 222 | + if (draft == null) { |
| 223 | + throw new Error("failed to fetch draft version"); |
| 224 | + } |
| 225 | + |
| 226 | + const versions: EmojiVersion[] = []; |
| 227 | + |
| 228 | + for (const match of rootHtml.matchAll(versionRegex)) { |
| 229 | + if (match == null || match[1] == null) continue; |
| 230 | + |
| 231 | + const version = match[1]; |
| 232 | + |
| 233 | + if (!await isEmojiVersionValid(version)) { |
| 234 | + continue; |
| 235 | + } |
| 236 | + |
| 237 | + if (versions.some((v) => v.unicode_version === version)) { |
| 238 | + continue; |
| 239 | + } |
| 240 | + |
| 241 | + versions.push({ |
| 242 | + emoji_version: null, |
| 243 | + unicode_version: version, |
| 244 | + draft: version === draft.unicode_version || version === draft.emoji_version, |
| 245 | + }); |
| 246 | + } |
| 247 | + |
| 248 | + for (const match of emojiHtml.matchAll(versionRegex)) { |
| 249 | + if (match == null || match[1] == null) continue; |
| 250 | + |
| 251 | + let version = match[1]; |
| 252 | + |
| 253 | + // for the emoji page, the versions is not valid semver. |
| 254 | + // so we will add the last 0 to the version. |
| 255 | + // handle both 5.0 and 12.0 -> 5.0.0 and 12.0.0 |
| 256 | + if (version.length === 3 || version.length === 4) { |
| 257 | + version += ".0"; |
| 258 | + } |
| 259 | + |
| 260 | + if (!await isEmojiVersionValid(version)) { |
| 261 | + continue; |
| 262 | + } |
| 263 | + |
| 264 | + // check if the unicode_version already exists. |
| 265 | + // if it does, we will update the emoji version. |
| 266 | + const existing = versions.find((v) => v.unicode_version === version); |
| 267 | + |
| 268 | + let unicode_version = null; |
| 269 | + |
| 270 | + // the emoji version 13.1 is using the unicode |
| 271 | + // 13.0, since it was never released. |
| 272 | + if (match[1] === "13.1") { |
| 273 | + unicode_version = "13.0.0"; |
| 274 | + } |
| 275 | + |
| 276 | + if (match[1] === "5.0") { |
| 277 | + unicode_version = "10.0.0"; |
| 278 | + } |
| 279 | + |
| 280 | + if (match[1] === "4.0" || match[1] === "3.0") { |
| 281 | + unicode_version = "9.0.0"; |
| 282 | + } |
| 283 | + |
| 284 | + if (match[1] === "2.0" || match[1] === "1.0") { |
| 285 | + unicode_version = "8.0.0"; |
| 286 | + } |
| 287 | + |
| 288 | + if (existing) { |
| 289 | + existing.unicode_version = unicode_version || existing.unicode_version; |
| 290 | + existing.emoji_version = match[1]; |
| 291 | + continue; |
| 292 | + } |
| 293 | + |
| 294 | + versions.push({ |
| 295 | + emoji_version: match[1], |
| 296 | + unicode_version, |
| 297 | + draft: version === draft.unicode_version || version === draft.emoji_version, |
| 298 | + }); |
| 299 | + } |
| 300 | + |
| 301 | + return versions.sort((a, b) => semver.compare(`${b.emoji_version}.0`, `${a.emoji_version}.0`)); |
| 302 | +} |
0 commit comments