|
| 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); |
0 commit comments