Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: baseline data in page data #16

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
28 changes: 28 additions & 0 deletions src/components/baseline/getWebFeatureStatus.ts
Original file line number Diff line number Diff line change
@@ -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;
}
28 changes: 28 additions & 0 deletions src/components/baseline/validate-feature.ts
Original file line number Diff line number Diff line change
@@ -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<typeof WebFeatureSchema>;

export default function validateWebFeature(feature: unknown): WebFeature {
return parse(WebFeatureSchema, feature);
}
2 changes: 1 addition & 1 deletion src/registry/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import JsonService from './environment/json';

export interface Environment {
browserCompat: unknown;
specUrls?: string;
specUrls?: string[];
path: string;
slug: string;
tags: string[];
Expand Down
15 changes: 12 additions & 3 deletions src/registry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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[];
Expand Down Expand Up @@ -142,6 +145,7 @@ interface InternalPageData {
referencesAll: string[];
referencesFixable: string[];
sourceType?: SourceType;
baseline?: BaselineItem;
}

enum MetaMacros {
Expand Down Expand Up @@ -344,7 +348,7 @@ class Registry {
new Context(
{
browserCompat,
specUrls,
specUrls: typeof specUrls === 'string' ? [specUrls] : specUrls,
path,
slug,
tags,
Expand All @@ -365,6 +369,11 @@ class Registry {
},
path,
hasLocalizedContent,
baseline: browserCompat
? (typeof browserCompat === 'string'
? getWebFeatureStatus(browserCompat)
: getWebFeatureStatus(...browserCompat)) || undefined
: undefined,
...otherPageData,
});

Expand Down
2 changes: 2 additions & 0 deletions src/runner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export default class Runner {
updatesInOriginalRepo,
originalPath,
translationLastUpdatedAt,
baseline,
} = page;

if (!headings) {
Expand Down Expand Up @@ -91,6 +92,7 @@ export default class Runner {
section,
sourceLastUpdatedAt: 0,
translationLastUpdatedAt,
baseline,
});
}

Expand Down
2 changes: 2 additions & 0 deletions src/runner/interfaces.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -38,4 +39,5 @@ export interface PageData {
slug: string;
tags: string[];
pageType: string;
baseline?: BaselineItem;
}
5 changes: 3 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"target": "ES2022",
"module": "ES2020",
"module": "ESNext",
"lib": ["ES2022", "DOM"],
"moduleResolution": "node",
"rootDir": ".",
Expand All @@ -19,7 +19,8 @@
"noImplicitThis": false,
"strictNullChecks": false,
"skipLibCheck": true,
"esModuleInterop": true
"esModuleInterop": true,
"resolveJsonModule": true
},
"include": ["src/**/*"],
"exclude": ["src/**/*.test.ts"]
Expand Down
18 changes: 14 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down