Skip to content

Commit 3277316

Browse files
committed
chg: cli for validation and schema
1 parent 2f4b191 commit 3277316

9 files changed

Lines changed: 205 additions & 95 deletions

File tree

.github/workflows/build-cli.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
matrix:
1515
include:
1616
- target: x86_64-unknown-linux-gnu
17-
output: nina-map-validate-linux-x86_64
17+
output: nina-map-cli-linux-x86_64
1818

1919
steps:
2020
- uses: actions/checkout@v4
@@ -24,7 +24,7 @@ jobs:
2424
deno-version: v2.x
2525

2626
- name: Compile
27-
run: deno compile --sloppy-imports --target ${{ matrix.target }} --output dist/${{ matrix.output }} src/validate.ts
27+
run: deno compile --sloppy-imports --target ${{ matrix.target }} --output dist/${{ matrix.output }} src/cli.ts
2828

2929
- uses: actions/upload-artifact@v4
3030
with:

.husky/pre-commit

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
11
pnpm lint-staged
2-
pnpm generate-schema
3-
git add schemas/map-config.schema.json

deno.lock

Lines changed: 136 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
"lint:fix": "biome check --write .",
1111
"format": "biome format --write .",
1212
"preview": "vite preview",
13-
"generate-schema": "pnpx vite-node scripts/generate-schema.ts",
14-
"validate": "deno run src/validate.ts",
15-
"validate:build": "deno compile --sloppy-imports --output dist/nina-map-validate src/validate.ts",
13+
"generate-schema": "deno run --sloppy-imports src/cli.ts schema > schemas/map-config.schema.json",
14+
"cli": "deno run --sloppy-imports src/cli.ts",
15+
"cli:build": "deno compile --sloppy-imports --output dist/nina-map-cli src/cli.ts",
1616
"prepare": "husky"
1717
},
1818
"dependencies": {

schemas/map-config.schema.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"$schema": "https://json-schema.org/draft/2020-12/schema",
3-
"title": "Map Configuration Schema",
4-
"description": "Schema for nina-maps map configuration",
3+
"title": "Nina Maps Configuration",
4+
"description": "Configuration schema for a nina-maps map.",
5+
"version": "0.1.3",
56
"type": "object",
67
"properties": {
78
"title": {
@@ -1222,4 +1223,4 @@
12221223
"config"
12231224
],
12241225
"additionalProperties": false
1225-
}
1226+
}

scripts/generate-schema.ts

Lines changed: 0 additions & 43 deletions
This file was deleted.

src/cli.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env -S deno run
2+
/// <reference lib="deno.ns" />
3+
import { Command } from 'jsr:@cliffy/command@^1';
4+
import * as z from 'zod';
5+
import pkg from '../package.json' with { type: 'json' };
6+
import { MapConfigSchema } from './schemas';
7+
8+
await new Command()
9+
.name('nina-maps')
10+
.version(pkg.version)
11+
.description('CLI tools for nina-maps.')
12+
.command(
13+
'validate',
14+
new Command()
15+
.description('Validate a nina-maps JSON configuration read from stdin.')
16+
.option('--raw', 'Output the raw zod error tree as JSON instead of prettified text.')
17+
.action(async ({ raw }) => {
18+
let input: string;
19+
try {
20+
input = await new Response(Deno.stdin.readable).text();
21+
} catch (err) {
22+
const message = err instanceof Error ? err.message : String(err);
23+
console.error(`Error reading input: ${message}`);
24+
Deno.exit(1);
25+
}
26+
27+
let data: unknown;
28+
try {
29+
data = JSON.parse(input);
30+
} catch {
31+
console.error('Error: input is not valid JSON');
32+
Deno.exit(1);
33+
}
34+
35+
const result = MapConfigSchema.safeParse(data);
36+
37+
if (result.success) {
38+
console.log('Valid map configuration.');
39+
} else {
40+
const message = raw ? JSON.stringify(z.treeifyError(result.error), null, 2) : z.prettifyError(result.error);
41+
console.error(message);
42+
Deno.exit(1);
43+
}
44+
}),
45+
)
46+
.command(
47+
'schema',
48+
new Command().description('Print the JSON Schema for a nina-maps configuration.').action(() => {
49+
const schema = {
50+
$schema: 'https://json-schema.org/draft/2020-12/schema',
51+
title: 'Nina Maps Configuration',
52+
description: 'Configuration schema for a nina-maps map.',
53+
version: pkg.version,
54+
...z.toJSONSchema(MapConfigSchema),
55+
};
56+
console.log(JSON.stringify(schema, null, 2));
57+
}),
58+
)
59+
.parse(Deno.args);

src/validate.ts

Lines changed: 0 additions & 41 deletions
This file was deleted.

tsconfig.app.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@
2626
"noUncheckedSideEffectImports": true
2727
},
2828
"include": ["src"],
29-
"exclude": ["src/validate.ts"]
29+
"exclude": ["src/cli.ts"]
3030
}

0 commit comments

Comments
 (0)