|
| 1 | +import { readFile } from "node:fs/promises"; |
| 2 | +import { extname } from "node:path"; |
| 3 | +import YAML from "yaml"; |
1 | 4 | import { getKeyword } from "@hyperjump/json-schema/experimental"; |
2 | 5 | import { parseIri, toAbsoluteIri } from "@hyperjump/uri"; |
3 | | -import { getNodeFromPointer } from "./json-util.js"; |
| 6 | +import { registerSchema as register } from "./json-schema.js"; |
| 7 | +import { fromJson, fromYaml, getNodeFromPointer } from "./json-util.js"; |
4 | 8 |
|
5 | 9 | /** |
6 | 10 | * @import { Position } from "unist" |
7 | | - * @import { CompiledSchema } from "@hyperjump/json-schema/experimental" |
8 | | - * @import { CoverageMapData, FileCoverageData, Range } from "istanbul-lib-coverage" |
9 | | - * @import { JsonNode } from "./jsonast.js" |
| 11 | + * @import { FileCoverageData, Range } from "istanbul-lib-coverage" |
| 12 | + * @import { SchemaObject } from "@hyperjump/json-schema" |
| 13 | + * @import { JsonNode } from "./jsonast.d.ts" |
| 14 | + * @import * as API from "./coverage-util.d.ts" |
10 | 15 | */ |
11 | 16 |
|
12 | | -/** @type (compiledSchema: CompiledSchema, schemaPath: string, schemaNodes: Record<string, JsonNode>) => CoverageMapData */ |
| 17 | +/** @type API.astToCoverageMap */ |
13 | 18 | export const astToCoverageMap = (compiledSchema, schemaPath, schemaNodes) => { |
14 | 19 | /** @type FileCoverageData */ |
15 | 20 | const fileCoverage = { |
@@ -109,3 +114,43 @@ const annotationKeywords = new Set([ |
109 | 114 | "https://json-schema.org/keyword/examples", |
110 | 115 | "https://json-schema.org/keyword/format" |
111 | 116 | ]); |
| 117 | + |
| 118 | +/** @type API.registerSchema */ |
| 119 | +export const registerSchema = async (schemaPath) => { |
| 120 | + const text = await readFile(schemaPath, "utf-8"); |
| 121 | + const extension = extname(schemaPath); |
| 122 | + |
| 123 | + /** @type SchemaObject | boolean */ |
| 124 | + let schema; |
| 125 | + switch (extension) { |
| 126 | + case ".json": |
| 127 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment |
| 128 | + schema = JSON.parse(text); |
| 129 | + break; |
| 130 | + case ".yaml": |
| 131 | + case ".yml": |
| 132 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment |
| 133 | + schema = YAML.parse(text); |
| 134 | + break; |
| 135 | + default: |
| 136 | + throw Error(`File of type '${extension}' is not supported.`); |
| 137 | + } |
| 138 | + |
| 139 | + register(schema); |
| 140 | +}; |
| 141 | + |
| 142 | +/** @type API.parseToAst */ |
| 143 | +export const parseToAst = async (schemaPath) => { |
| 144 | + const text = await readFile(schemaPath, "utf-8"); |
| 145 | + const extension = extname(schemaPath); |
| 146 | + |
| 147 | + switch (extension) { |
| 148 | + case ".json": |
| 149 | + return fromJson(text); |
| 150 | + case ".yaml": |
| 151 | + case ".yml": |
| 152 | + return fromYaml(text); |
| 153 | + default: |
| 154 | + throw Error(`File of type '${extension}' is not supported.`); |
| 155 | + } |
| 156 | +}; |
0 commit comments