From a858bfd552b7e49210529f3d326ecaaabb2e5dab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dafydd=20Ll=C5=B7r=20Pearson?= Date: Tue, 16 Jul 2024 12:23:08 +0100 Subject: [PATCH] refactor: Build scripts for multiple schemas --- .husky/pre-commit | 2 +- package.json | 7 +++---- scripts/build-schema.sh | 20 ++++++++++++++++++++ 3 files changed, 24 insertions(+), 5 deletions(-) create mode 100755 scripts/build-schema.sh diff --git a/.husky/pre-commit b/.husky/pre-commit index 49bae617..3611423b 100755 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -3,7 +3,7 @@ branch="$(git rev-parse --abbrev-ref HEAD)" if [ "$branch" != "main" ]; then - pnpm build-json-examples && pnpm build-schema:dev + pnpm build fi pnpm check && pnpm fix diff --git a/package.json b/package.json index 6d03f9e1..622ec086 100644 --- a/package.json +++ b/package.json @@ -4,14 +4,13 @@ "description": "Digital Planning Data schemas", "main": "schema/schema.json", "scripts": { - "build": "pnpm build-schema:dist && pnpm build-json-examples", + "build": "pnpm build-schema && pnpm build-json-examples", "build-json-examples": "pnpm ts-node scripts/build-json-examples.ts", - "build-schema:dev": "pnpm ts-json-schema-generator --path 'types/**/*.ts' --out 'schemas/digitalPlanningApplication.json' --type DigitalPlanningApplication --id @next --no-top-ref", - "build-schema:dist": "pnpm ts-json-schema-generator --path 'types/**/*.ts' --out 'schemas/digitalPlanningApplication.json' --type DigitalPlanningApplication --id \"${VERSION:-@next}\" --no-top-ref", + "build-schema": "./scripts/build-schema.sh", "lint": "gts lint", "check": "tsc --noEmit", "fix": "gts fix", - "test": "pnpm build-schema:dev && vitest", + "test": "pnpm build-schema && vitest", "prepare": "husky install" }, "keywords": [], diff --git a/scripts/build-schema.sh b/scripts/build-schema.sh new file mode 100755 index 00000000..7eee9132 --- /dev/null +++ b/scripts/build-schema.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +echo "Running build-schema script..." + +# Use environment variable VERSION, defaulting to "@next" if not set +version="${VERSION:-@next}" + +echo "Version set to $version" + +dirs=("digitalPlanningApplication") +types=("DigitalPlanningApplication") + +for i in "${!dirs[@]}"; do + dir=${dirs[$i]} + type=${types[$i]} + + echo "Building schema for $type from directory /$dir" + + pnpm ts-json-schema-generator --path "types/schemas/${dir}/*.ts" --out "schemas/${dir}.json" --type "$type" --id "$version" --no-top-ref +done