Skip to content

Commit

Permalink
Rename schema file to data.schema.json and publish it
Browse files Browse the repository at this point in the history
This will include the schema in the NPM package. The idea is to also
publish it with GitHub releases.
  • Loading branch information
foolip committed Jul 19, 2024
1 parent ff67993 commit d3b7813
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 20 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ node_modules/
packages/compute-baseline/dist/
packages/web-features/index.d.ts
packages/**/LICENSE.txt
packages/web-features/data.json
packages/web-features/data.schema.json
packages/web-features/types.ts
data.json
index.js
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
"dist": "tsx scripts/dist.ts",
"feature-init": "tsx scripts/feature-init.ts",
"format": "npx prettier --write .",
"schema-defs:write": "npm run schema-defs -- --out ./schemas/defs.schema.json",
"schema-defs": "ts-json-schema-generator --tsconfig ./tsconfig.json --type WebFeaturesData --path ./types.ts --id defs",
"schema:write": "npm run schema -- --out ./schemas/data.schema.json",
"schema": "ts-json-schema-generator --tsconfig ./tsconfig.json --type WebFeaturesData --path ./types.ts",
"test:caniuse": "tsx scripts/caniuse.ts",
"test:coverage": "npm run --workspaces test:coverage",
"test:dist": "tsx scripts/dist.ts --check",
Expand Down
6 changes: 4 additions & 2 deletions packages/web-features/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
"main": "index.js",
"exports": {
".": "./index.js",
"./data.json": "./data.json"
"./data.json": "./data.json",
"./data.schema.json": "./data.schema.json"
},
"types": "./index.d.ts",
"files": [
"index.d.ts",
"index.js",
"data.json"
"data.json",
"data.schema.json"
],
"scripts": {
"prepare": "tsc && rm types.js && tsup ./index.ts --dts-only --format=esm --out-dir=."
Expand Down
1 change: 0 additions & 1 deletion schemas/defs.schema.json → schemas/data.schema.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"$id": "defs",
"$ref": "#/definitions/WebFeaturesData",
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
Expand Down
12 changes: 8 additions & 4 deletions scripts/build.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { execSync } from "child_process";
import { basename } from "node:path";
import { execSync } from "node:child_process";
import fs from "node:fs";
import stringify from "fast-json-stable-stringify";
import fs from "fs";
import yargs from "yargs";
import * as data from "../index.js";

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

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

const json = stringify(data);
// TODO: Validate the resulting JSON against a schema.
const path = new URL("data.json", packageDir);
fs.writeFileSync(path, json);
for (const file of filesToCopy) {
fs.copyFileSync(new URL(file, rootDir), new URL(file, packageDir));
fs.copyFileSync(
new URL(file, rootDir),
new URL(basename(file), packageDir),
);
}
execSync("npm install", {
cwd: "./packages/web-features",
Expand Down
20 changes: 10 additions & 10 deletions scripts/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ import addFormats from 'ajv-formats';

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

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

let status: 0 | 1 = 0;

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

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

const validate = ajv.compile(defs);
const validate = ajv.compile(schema);

const valid = validate(data);
if (!valid) {
Expand All @@ -40,6 +40,6 @@ function validate() {
}
}

checkDefsConsistency();
checkSchemaConsistency();
validate();
process.exit(status);

0 comments on commit d3b7813

Please sign in to comment.