|
| 1 | +import * as fs from '@leawind/inventory/fs' |
| 2 | +import { Path } from '@leawind/inventory/fs' |
| 3 | +import { INPUT, OUTPUT, OUTPUT_KEEP } from './env.ts' |
| 4 | +import { Schema } from 'ts-json-schema-generator' |
| 5 | +import { SchemaUtils } from './lib/schemas.ts' |
| 6 | + |
| 7 | +// Clean output directory |
| 8 | +if (await fs.exists(OUTPUT)) { |
| 9 | + const toRemove = OUTPUT.listSync() |
| 10 | + .filter((item) => { |
| 11 | + const rel = item.relative(OUTPUT).str |
| 12 | + for (const keep of OUTPUT_KEEP) { |
| 13 | + if (keep instanceof RegExp) { |
| 14 | + if (keep.test(rel)) { |
| 15 | + return false |
| 16 | + } |
| 17 | + } else if (keep === rel) { |
| 18 | + return false |
| 19 | + } |
| 20 | + } |
| 21 | + return true |
| 22 | + }) |
| 23 | + toRemove.forEach((item) => item.removeSync({ recursive: true })) |
| 24 | +} |
| 25 | + |
| 26 | +// Copy static files |
| 27 | +{ |
| 28 | + fs.copySync(INPUT.join('copied'), OUTPUT) |
| 29 | +} |
| 30 | + |
| 31 | +// Build schemas |
| 32 | +{ |
| 33 | + const inputDir = INPUT.join('schemas') |
| 34 | + const outputDir = OUTPUT.join('schema') |
| 35 | + |
| 36 | + const schemas: Record<string, Schema> = {} |
| 37 | + |
| 38 | + // Parse schemas |
| 39 | + for await (const file of fs.walkFile(inputDir.str, /.*\.ts$/)) { |
| 40 | + const src = fs.Path.from(file) |
| 41 | + |
| 42 | + const relative = src.relative(inputDir).noExt |
| 43 | + schemas[relative] = SchemaUtils.parse(src, inputDir) |
| 44 | + } |
| 45 | + |
| 46 | + // Write schemas |
| 47 | + for (const [relative, schema] of Object.entries(schemas)) { |
| 48 | + const dst = outputDir.join(relative + '.json') |
| 49 | + Path.from(dst).write(JSON.stringify(schema, null, 2)) |
| 50 | + } |
| 51 | + |
| 52 | + // Write index |
| 53 | + { |
| 54 | + const indexFile = outputDir.join('index.html') |
| 55 | + const html = `<!DOCTYPE html> |
| 56 | +<html lang="en"> |
| 57 | +<head> |
| 58 | + <meta charset="UTF-8"> |
| 59 | + <title>Schemas</title> |
| 60 | +</head> |
| 61 | +<body> |
| 62 | + <h1>Schemas</h1> |
| 63 | + <ul> |
| 64 | + ${ |
| 65 | + Object.keys(schemas).map((rel) => { |
| 66 | + rel += '.json' |
| 67 | + return `<li><a href="${rel}">${rel}</a></li>` |
| 68 | + }).join('\n') |
| 69 | + } |
| 70 | + </ul> |
| 71 | +</body> |
| 72 | +</html> |
| 73 | +` |
| 74 | + indexFile.write(html) |
| 75 | + } |
| 76 | +} |
0 commit comments