Skip to content

Commit

Permalink
feat: add $id to schemas to allow versioning
Browse files Browse the repository at this point in the history
  • Loading branch information
GeekyEggo committed Feb 12, 2024
1 parent 69b5b0d commit da7e524
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"build:exports": "rollup --config rollup.config.ts --configPlugin typescript",
"clean": "rm -rf ./dist/ && rm -rf ./streamdeck/",
"lint": "eslint . --ext .ts --max-warnings 0",
"preversion": "npm run build && npm run lint"
"preversion": "npm run lint",
"version": "npm run build"
},
"repository": {
"type": "git",
Expand Down
36 changes: 17 additions & 19 deletions scripts/build.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,39 @@
/* eslint-disable no-useless-escape */
import type { JSONSchema7 } from "json-schema";
import { existsSync, mkdirSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { createGenerator } from "ts-json-schema-generator";
import pkg from "../package.json";
import { customKeywordTransformer } from "./transformers/custom-keywords";
import { versionManifests } from "./transformers/version-manifests";

// Create a generator so we're able to produce multiple schemas.
const generator = createGenerator({
extraTags: ["errorMessage", "imageDimensions", "filePath"],
path: join(__dirname, "../src/streamdeck/plugins/index.ts"),
skipTypeCheck: true,
tsconfig: join(__dirname, "../tsconfig.json")
});

// Prepare the output directory.
const outputDir = join(__dirname, "../streamdeck/plugins");
if (!existsSync(outputDir)) {
mkdirSync(outputDir, { recursive: true });
}

generateAndWriteSchema("Manifest", versionManifests);
generateAndWriteSchema("Layout");
generateAndWriteSchema("Manifest", [customKeywordTransformer, versionManifests]);
generateAndWriteSchema("Layout", [customKeywordTransformer]);

/**
* Generates the JSON schema for the specified TypeScript `type`, and writes it locally to `{type}.json`.
* @param type TypeScript type whose schema should be generated.
* @param transform Optional function used to transform the schema.
* @param transformers Optional function used to transform the schema.
*/
function generateAndWriteSchema(type: string, transform?: (schema: JSONSchema7) => void): void {
const schema = generator.createSchema(type);
if (transform) {
transform(schema);
}
function generateAndWriteSchema(type: string, transformers?: ((schema: JSONSchema7) => void)[]): void {
// Build the generator.
const path = join(__dirname, "../src/streamdeck/plugins/index.ts");
const generator = createGenerator({
extraTags: ["errorMessage", "imageDimensions", "filePath"],
path,
skipTypeCheck: true,
schemaId: `${pkg.name}/streamdeck/plugins/${type.toLowerCase()}@${pkg.version}`,
tsconfig: join(__dirname, "../tsconfig.json")
});

// Apply the custom keyword transformer to all schemas
customKeywordTransformer(schema);
// Generate the schema, and apply the transformers.
const schema = generator.createSchema(type);
transformers?.forEach((t) => t(schema));

// Determine the output path, and serialize the schema.
const outputPath = join(outputDir, `${type.toLowerCase()}.json`);
Expand Down

0 comments on commit da7e524

Please sign in to comment.