Skip to content

Commit 4f8bfe8

Browse files
foolipddbeck
andauthored
Rename schema file to data.schema.json and publish it (#1348)
This will include the schema in the NPM package. The idea is to also publish it with GitHub releases. Co-authored-by: Daniel D. Beck <[email protected]>
1 parent 4b88ef3 commit 4f8bfe8

File tree

7 files changed

+32
-21
lines changed

7 files changed

+32
-21
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ node_modules/
33
packages/compute-baseline/dist/
44
packages/web-features/index.d.ts
55
packages/**/LICENSE.txt
6+
packages/web-features/data.json
7+
packages/web-features/data.schema.json
68
packages/web-features/types.ts
7-
data.json
89
index.js

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
"dist": "tsx scripts/dist.ts",
2121
"feature-init": "tsx scripts/feature-init.ts",
2222
"format": "npx prettier --write .",
23-
"schema-defs:write": "npm run schema-defs -- --out ./schemas/defs.schema.json",
24-
"schema-defs": "ts-json-schema-generator --tsconfig ./tsconfig.json --type WebFeaturesData --path ./types.ts --id defs",
23+
"schema:write": "npm run schema -- --out ./schemas/data.schema.json",
24+
"schema": "ts-json-schema-generator --tsconfig ./tsconfig.json --path ./types.ts",
2525
"test:caniuse": "tsx scripts/caniuse.ts",
2626
"test:coverage": "npm run --workspaces test:coverage",
2727
"test:dist": "tsx scripts/dist.ts --check",

packages/web-features/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ import data from "web-features/data.json" with { type: "json" };
1919
const { features, groups, snapshots } = data;
2020
```
2121

22+
To import the JSON schema with or without Node.js:
23+
24+
```js
25+
import schema from "web-features/data.schema.json" with { type: "json" };
26+
```
27+
2228
## Rendering Baseline statuses with `web-features`
2329

2430
If you're using `web-features` to render Baseline iconography or browser logos with support markers, then you must follow these procedures to ensure consistent usage.

packages/web-features/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111
"main": "index.js",
1212
"exports": {
1313
".": "./index.js",
14-
"./data.json": "./data.json"
14+
"./data.json": "./data.json",
15+
"./data.schema.json": "./data.schema.json"
1516
},
1617
"types": "./index.d.ts",
1718
"files": [
1819
"index.d.ts",
1920
"index.js",
20-
"data.json"
21+
"data.json",
22+
"data.schema.json"
2123
],
2224
"scripts": {
2325
"prepare": "tsc && rm types.js && tsup ./index.ts --dts-only --format=esm --out-dir=."

schemas/defs.schema.json renamed to schemas/data.schema.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
{
2-
"$id": "defs",
3-
"$ref": "#/definitions/WebFeaturesData",
42
"$schema": "http://json-schema.org/draft-07/schema#",
53
"definitions": {
64
"FeatureData": {

scripts/build.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { execSync } from "child_process";
1+
import { basename } from "node:path";
2+
import { execSync } from "node:child_process";
3+
import fs from "node:fs";
24
import stringify from "fast-json-stable-stringify";
3-
import fs from "fs";
45
import yargs from "yargs";
56
import * as data from "../index.js";
67

@@ -17,14 +18,17 @@ yargs(process.argv.slice(2))
1718

1819
function buildPackage() {
1920
const packageDir = new URL("./packages/web-features/", rootDir);
20-
const filesToCopy = ["LICENSE.txt", "types.ts"];
21+
const filesToCopy = ["LICENSE.txt", "types.ts", "schemas/data.schema.json"];
2122

2223
const json = stringify(data);
2324
// TODO: Validate the resulting JSON against a schema.
2425
const path = new URL("data.json", packageDir);
2526
fs.writeFileSync(path, json);
2627
for (const file of filesToCopy) {
27-
fs.copyFileSync(new URL(file, rootDir), new URL(file, packageDir));
28+
fs.copyFileSync(
29+
new URL(file, rootDir),
30+
new URL(basename(file), packageDir),
31+
);
2832
}
2933
execSync("npm install", {
3034
cwd: "./packages/web-features",

scripts/schema.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ import addFormats from 'ajv-formats';
88

99
import * as data from '../index.js';
1010

11-
import defs from '../schemas/defs.schema.json' assert { type: 'json' };
11+
import schema from '../schemas/data.schema.json' assert { type: 'json' };
1212

1313
let status: 0 | 1 = 0;
1414

15-
function checkDefsConsistency(): void {
16-
const defsPath: string = path.join(path.dirname(url.fileURLToPath(import.meta.url)), "../schemas/defs.schema.json");
17-
const defsOnDisk: string = fs.readFileSync(defsPath, { encoding: "utf-8"});
18-
const defsGenerated: string = child_process.execSync("npm run --silent schema-defs", { encoding: "utf-8"}).trim();
15+
function checkSchemaConsistency(): void {
16+
const schemaPath: string = path.join(path.dirname(url.fileURLToPath(import.meta.url)), "../schemas/data.schema.json");
17+
const schemaOnDisk: string = fs.readFileSync(schemaPath, { encoding: "utf-8"});
18+
const schemaGenerated: string = child_process.execSync("npm run --silent schema", { encoding: "utf-8"}).trim();
1919

20-
if (defsOnDisk !== defsGenerated) {
21-
console.error("There's a mismatch between the schema defs on disk and types in `index.ts`.");
20+
if (schemaOnDisk !== schemaGenerated) {
21+
console.error("There's a mismatch between the schema on disk and types in `index.ts`.");
2222
console.error("This may produce misleading results for feature validation.");
23-
console.error("To fix this, run `npm run schema-defs:write`.");
23+
console.error("To fix this, run `npm run schema:write`.");
2424
status = 1;
2525
}
2626
}
@@ -29,7 +29,7 @@ function validate() {
2929
const ajv = new Ajv({allErrors: true});
3030
addFormats(ajv);
3131

32-
const validate = ajv.compile(defs);
32+
const validate = ajv.compile(schema);
3333

3434
const valid = validate(data);
3535
if (!valid) {
@@ -40,6 +40,6 @@ function validate() {
4040
}
4141
}
4242

43-
checkDefsConsistency();
43+
checkSchemaConsistency();
4444
validate();
4545
process.exit(status);

0 commit comments

Comments
 (0)