From 072e679d3701ed9a77f6b7fd6b3846082b435f90 Mon Sep 17 00:00:00 2001 From: Vitalii Perehonchuk Date: Fri, 26 Jan 2024 11:26:10 +0200 Subject: [PATCH] feat: baseline data in page data --- package.json | 6 ++-- .../baseline/getWebFeatureStatus.ts | 28 +++++++++++++++++++ src/components/baseline/validate-feature.ts | 28 +++++++++++++++++++ src/registry/context.ts | 2 +- src/registry/index.ts | 15 ++++++++-- src/runner/index.ts | 2 ++ src/runner/interfaces.ts | 2 ++ tsconfig.json | 5 ++-- yarn.lock | 18 +++++++++--- 9 files changed, 94 insertions(+), 12 deletions(-) create mode 100644 src/components/baseline/getWebFeatureStatus.ts create mode 100644 src/components/baseline/validate-feature.ts diff --git a/package.json b/package.json index 596218c..b7b8b6b 100644 --- a/package.json +++ b/package.json @@ -67,11 +67,13 @@ "rollup-plugin-import-assert": "^2.1.0", "ts-node": "^10.8.1", "tslib": "~2.4", - "typescript": "~4.7", + "typescript": "~5.3.3", "unified": "^10.1.1", "unist-builder": "^3.0.0", "unist-util-visit": "^4.1.0", - "unist-util-visit-parents": "^5.1.0" + "unist-util-visit-parents": "^5.1.0", + "valibot": "0.27.0", + "web-features": "0.5.1" }, "ava": { "extensions": { diff --git a/src/components/baseline/getWebFeatureStatus.ts b/src/components/baseline/getWebFeatureStatus.ts new file mode 100644 index 0000000..a0346df --- /dev/null +++ b/src/components/baseline/getWebFeatureStatus.ts @@ -0,0 +1,28 @@ +import webFeatures from 'web-features/index.json' assert { type: 'json' }; +import validateWebFeature from './validate-feature'; + +export interface BaselineItem { + baseline: false | 'low' | 'high'; +} + +export default function getWebFeatureStatus(...features: string[]) { + if (features.length === 0) { + return null; + } + + for (const feature of Object.values(webFeatures)) { + if (!feature) { + continue; + } + const validatedFeature = validateWebFeature(feature); + if ( + validatedFeature.status && + validatedFeature.compat_features?.some((feature) => + features.includes(feature), + ) + ) { + return validatedFeature.status; + } + } + return null; +} diff --git a/src/components/baseline/validate-feature.ts b/src/components/baseline/validate-feature.ts new file mode 100644 index 0000000..455a4c6 --- /dev/null +++ b/src/components/baseline/validate-feature.ts @@ -0,0 +1,28 @@ +import { + type Output, + array, + literal, + object, + optional, + parse, + string, + union, + record, +} from 'valibot'; + +const WebFeatureSchema = object({ + compat_features: optional(array(string())), + status: optional( + object({ + baseline: union([literal(false), literal('low'), literal('high')]), + baseline_low_date: optional(string()), + support: record(string()), + }), + ), +}); + +export type WebFeature = Output; + +export default function validateWebFeature(feature: unknown): WebFeature { + return parse(WebFeatureSchema, feature); +} diff --git a/src/registry/context.ts b/src/registry/context.ts index 802d594..a48a502 100644 --- a/src/registry/context.ts +++ b/src/registry/context.ts @@ -3,7 +3,7 @@ import JsonService from './environment/json'; export interface Environment { browserCompat: unknown; - specUrls?: string; + specUrls?: string[]; path: string; slug: string; tags: string[]; diff --git a/src/registry/index.ts b/src/registry/index.ts index 5267ab1..545fdec 100644 --- a/src/registry/index.ts +++ b/src/registry/index.ts @@ -26,6 +26,9 @@ import { SerializedMetaMacro } from '../runner/interfaces'; import { Heading } from './utils/find-headings'; import { pick } from 'lodash-es'; import trimHash from '../utils/trim-hash'; +import getWebFeatureStatus, { + type BaselineItem, +} from '../components/baseline/getWebFeatureStatus'; /** * Transforms a list of paths to content files @@ -104,8 +107,8 @@ export interface RegistryInitOptions { type SourceType = 'md' | 'html'; interface PageFrontMatter { - 'browser-compat': string; - 'spec-urls': string; + 'browser-compat': string | string[]; + 'spec-urls': string | string[]; 'page-type': string; title: string; tags: string[]; @@ -142,6 +145,7 @@ interface InternalPageData { referencesAll: string[]; referencesFixable: string[]; sourceType?: SourceType; + baseline?: BaselineItem; } enum MetaMacros { @@ -344,7 +348,7 @@ class Registry { new Context( { browserCompat, - specUrls, + specUrls: typeof specUrls === 'string' ? [specUrls] : specUrls, path, slug, tags, @@ -365,6 +369,11 @@ class Registry { }, path, hasLocalizedContent, + baseline: browserCompat + ? (typeof browserCompat === 'string' + ? getWebFeatureStatus(browserCompat) + : getWebFeatureStatus(...browserCompat)) || undefined + : undefined, ...otherPageData, }); diff --git a/src/runner/index.ts b/src/runner/index.ts index 076032c..107f4e9 100644 --- a/src/runner/index.ts +++ b/src/runner/index.ts @@ -64,6 +64,7 @@ export default class Runner { updatesInOriginalRepo, originalPath, translationLastUpdatedAt, + baseline, } = page; if (!headings) { @@ -91,6 +92,7 @@ export default class Runner { section, sourceLastUpdatedAt: 0, translationLastUpdatedAt, + baseline, }); } diff --git a/src/runner/interfaces.ts b/src/runner/interfaces.ts index 504f4b1..c69b530 100644 --- a/src/runner/interfaces.ts +++ b/src/runner/interfaces.ts @@ -1,5 +1,6 @@ import { Heading } from '../registry/utils/find-headings'; import { ExtractedSample } from '../registry/utils/extract-live-sample'; +import type { BaselineItem } from '../components/baseline/getWebFeatureStatus'; export interface IndexFileObject { index: MainIndexData; @@ -38,4 +39,5 @@ export interface PageData { slug: string; tags: string[]; pageType: string; + baseline?: BaselineItem; } diff --git a/tsconfig.json b/tsconfig.json index 73c14c4..3674079 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,7 @@ { "compilerOptions": { "target": "ES2022", - "module": "ES2020", + "module": "ESNext", "lib": ["ES2022", "DOM"], "moduleResolution": "node", "rootDir": ".", @@ -19,7 +19,8 @@ "noImplicitThis": false, "strictNullChecks": false, "skipLibCheck": true, - "esModuleInterop": true + "esModuleInterop": true, + "resolveJsonModule": true }, "include": ["src/**/*"], "exclude": ["src/**/*.test.ts"] diff --git a/yarn.lock b/yarn.lock index df58c15..8a17177 100644 --- a/yarn.lock +++ b/yarn.lock @@ -9050,10 +9050,10 @@ typescript@^4.6.4: resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.8.4.tgz#c464abca159669597be5f96b8943500b238e60e6" integrity sha512-QCh+85mCy+h0IGff8r5XWzOVSbBO+KfeYrMQh7NJ58QujwcE22u+NUSmUxqF+un70P9GXKxa2HCNiTTMJknyjQ== -typescript@~4.7: - version "4.7.4" - resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.7.4.tgz#1a88596d1cf47d59507a1bcdfb5b9dfe4d488235" - integrity sha512-C0WQT0gezHuw6AdY1M2jxUO83Rjf0HP7Sk1DtXj6j1EwkQNZrHAg2XPWlq62oqEhYvONq5pkC2Y9oPljWToLmQ== +typescript@~5.3.3: + version "5.3.3" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.3.3.tgz#b3ce6ba258e72e6305ba66f5c9b452aaee3ffe37" + integrity sha512-pXWcraxM0uxAS+tN0AG/BF2TyqmHO014Z070UsJ+pFvYuRSq8KH8DmWpnbXe0pEPDHXZV3FcAbJkijJ5oNEnWw== uglify-js@^3.1.4: version "3.17.3" @@ -9315,6 +9315,11 @@ v8-compile-cache@^2.0.3: resolved "https://registry.yarnpkg.com/v8-compile-cache/-/v8-compile-cache-2.3.0.tgz#2de19618c66dc247dcfb6f99338035d8245a2cee" integrity sha512-l8lCEmLcLYZh4nbunNZvQCJc5pv7+RCwa8q/LdUx8u7lsWvPDKmpodJAJNwkAhJC//dFY48KuIEmjtd4RViDrA== +valibot@0.27.0: + version "0.27.0" + resolved "https://registry.yarnpkg.com/valibot/-/valibot-0.27.0.tgz#057222ef134928c190ba645584ec021312aaadf8" + integrity sha512-1Wv0FfiosoAYruPhueHJgJtgj18KlUoBaIEeCefCEsohStcJQWp3jL59J8Tj9w8jiuQ3USkZGH8BtzDHDGgdxQ== + validate-npm-package-license@^3.0.1, validate-npm-package-license@^3.0.4: version "3.0.4" resolved "https://registry.yarnpkg.com/validate-npm-package-license/-/validate-npm-package-license-3.0.4.tgz#fc91f6b9c7ba15c857f4cb2c5defeec39d4f410a" @@ -9403,6 +9408,11 @@ wcwidth@^1.0.0, wcwidth@^1.0.1: dependencies: defaults "^1.0.3" +web-features@0.5.1: + version "0.5.1" + resolved "https://registry.yarnpkg.com/web-features/-/web-features-0.5.1.tgz#edf75127605fe80e19451d7092a760445d9d8309" + integrity sha512-2ZB/vJdyeJod7qUD6gupETkbjyzPkIlyx3InCwLkauJYXVwCaTkM24qenZbTmoFXwSqIOlK4wO7HKv4ZXHmXkg== + web-namespaces@^1.0.0: version "1.1.4" resolved "https://registry.yarnpkg.com/web-namespaces/-/web-namespaces-1.1.4.tgz#bc98a3de60dadd7faefc403d1076d529f5e030ec"