|
| 1 | +import { fsa, Url, UrlFolder } from '@basemaps/shared'; |
| 2 | +import { CliInfo } from '@basemaps/shared/build/cli/info.js'; |
| 3 | +import { getLogger, logArguments } from '@basemaps/shared/build/cli/log.js'; |
| 4 | +import { command, option } from 'cmd-ts'; |
| 5 | +import { readFileSync } from 'fs'; |
| 6 | +import Mustache from 'mustache'; |
| 7 | +import { z } from 'zod'; |
| 8 | + |
| 9 | +import { zSchema } from '../schema-loader/parser.js'; |
| 10 | +import { Schema } from '../schema-loader/schema.js'; |
| 11 | + |
| 12 | +interface Property { |
| 13 | + name: string; |
| 14 | + type: string; |
| 15 | + description: string; |
| 16 | +} |
| 17 | + |
| 18 | +interface Feature { |
| 19 | + name: string; |
| 20 | + kind: string; |
| 21 | + geometry: string; |
| 22 | + minZoom: number; |
| 23 | + maxZoom: number; |
| 24 | +} |
| 25 | + |
| 26 | +interface Layer { |
| 27 | + name: string; |
| 28 | + description: string; |
| 29 | + properties: Property[]; |
| 30 | + features: Feature[]; |
| 31 | +} |
| 32 | + |
| 33 | +function pathToURLFolder(path: string): URL { |
| 34 | + const url = fsa.toUrl(path); |
| 35 | + url.search = ''; |
| 36 | + url.hash = ''; |
| 37 | + if (!url.pathname.endsWith('/')) url.pathname += '/'; |
| 38 | + return url; |
| 39 | +} |
| 40 | + |
| 41 | +export const DocsArgs = { |
| 42 | + ...logArguments, |
| 43 | + schema: option({ |
| 44 | + type: UrlFolder, |
| 45 | + long: 'schema', |
| 46 | + defaultValue: () => pathToURLFolder('schema'), |
| 47 | + description: 'Path to the directory containing the schema files from which to generate markdown docs', |
| 48 | + }), |
| 49 | + template: option({ |
| 50 | + type: Url, |
| 51 | + long: 'template', |
| 52 | + description: 'Template file', |
| 53 | + }), |
| 54 | + target: option({ |
| 55 | + type: Url, |
| 56 | + long: 'target', |
| 57 | + description: 'Target location for the result file', |
| 58 | + }), |
| 59 | +}; |
| 60 | + |
| 61 | +export const DocsCommand = command({ |
| 62 | + name: 'docs', |
| 63 | + version: CliInfo.version, |
| 64 | + description: 'Generate markdown docs from a directory of schema files.', |
| 65 | + args: DocsArgs, |
| 66 | + async handler(args) { |
| 67 | + const logger = getLogger(this, args, 'cli-vector'); |
| 68 | + logger.info('GenerateMarkdownDocs: Start'); |
| 69 | + |
| 70 | + // parse schema files |
| 71 | + const schemas: Schema[] = []; |
| 72 | + const files = await fsa.toArray(fsa.list(args.schema)); |
| 73 | + |
| 74 | + for (const file of files) { |
| 75 | + if (file.href.endsWith('.json')) { |
| 76 | + const json = await fsa.readJson(file); |
| 77 | + // Validate the json |
| 78 | + try { |
| 79 | + const parsed = zSchema.parse(json); |
| 80 | + schemas.push(parsed); |
| 81 | + } catch (e) { |
| 82 | + if (e instanceof z.ZodError) { |
| 83 | + throw new Error(`Schema ${file.href} is invalid: ${e.message}`); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + const layers: Layer[] = []; |
| 90 | + |
| 91 | + for (const schema of schemas) { |
| 92 | + const properties = schema.metadata.attributes.map((attribute) => ({ |
| 93 | + name: attribute, |
| 94 | + type: 'TBC', |
| 95 | + description: 'TBC', |
| 96 | + })); |
| 97 | + |
| 98 | + const features = schema.layers.reduce( |
| 99 | + (obj, layer) => { |
| 100 | + const kind = layer.tags['kind']; |
| 101 | + if (typeof kind !== 'string') return obj; |
| 102 | + |
| 103 | + // const zoom = |
| 104 | + // layer.style.minZoom === layer.style.maxZoom |
| 105 | + // ? layer.style.minZoom.toString() |
| 106 | + // : `${layer.style.minZoom}-${layer.style.maxZoom}`; |
| 107 | + |
| 108 | + const entry = obj[kind]; |
| 109 | + |
| 110 | + if (entry == null) { |
| 111 | + return { |
| 112 | + ...obj, |
| 113 | + [kind]: { |
| 114 | + name: kind, |
| 115 | + kind, |
| 116 | + geometry: 'TBC', |
| 117 | + minZoom: layer.style.minZoom, |
| 118 | + maxZoom: layer.style.maxZoom, |
| 119 | + } as Feature, |
| 120 | + }; |
| 121 | + } |
| 122 | + |
| 123 | + if (layer.style.minZoom < entry.minZoom) { |
| 124 | + entry.minZoom = layer.style.minZoom; |
| 125 | + } |
| 126 | + |
| 127 | + if (layer.style.maxZoom > entry.maxZoom) { |
| 128 | + entry.maxZoom = layer.style.maxZoom; |
| 129 | + } |
| 130 | + |
| 131 | + return obj; |
| 132 | + }, |
| 133 | + {} as { [key: string]: Feature }, |
| 134 | + ); |
| 135 | + |
| 136 | + layers.push({ |
| 137 | + name: schema.name, |
| 138 | + description: 'TBC', |
| 139 | + properties, |
| 140 | + features: Object.values(features), |
| 141 | + }); |
| 142 | + } |
| 143 | + |
| 144 | + const template = readFileSync(args.template).toString(); |
| 145 | + const output = Mustache.render(template, { layers }); |
| 146 | + await fsa.write(new URL('result.md', args.target), output); |
| 147 | + |
| 148 | + logger.info('GenerateMarkdownDocs: End'); |
| 149 | + }, |
| 150 | +}); |
0 commit comments