132 lines
3.5 KiB
TypeScript
132 lines
3.5 KiB
TypeScript
|
|
import { glob } from 'glob';
|
|
import { BuildState } from './state';
|
|
import { read_json, write_text, read_yaml } from '../fs';
|
|
import { build_markdown_from_json_schema } from '@doc-utils/jsonschema2markdown';
|
|
import { stringify as to_yaml } from 'yaml';
|
|
import { build_partials, map_input_file_to_output_file, map_output_file_to_url, render_page } from './helpers';
|
|
|
|
export async function render_json_schema_files(state: BuildState) {
|
|
const promises: Promise<any>[] = [ ];
|
|
const json_files = await glob(state.conf.input['schema+json'], {
|
|
absolute: true,
|
|
cwd: state.conf.input.root,
|
|
});
|
|
const yaml_files = await glob(state.conf.input['schema+yaml'], {
|
|
absolute: true,
|
|
cwd: state.conf.input.root,
|
|
});
|
|
|
|
if (! state.partials) {
|
|
await build_partials(state);
|
|
}
|
|
|
|
for (const in_file of json_files) {
|
|
if (state.seen_files.has(in_file)) {
|
|
continue;
|
|
}
|
|
|
|
state.seen_files.add(in_file);
|
|
|
|
promises.push(
|
|
handle_json_schema_json_file(state, in_file)
|
|
);
|
|
}
|
|
|
|
for (const in_file of yaml_files) {
|
|
if (state.seen_files.has(in_file)) {
|
|
continue;
|
|
}
|
|
|
|
state.seen_files.add(in_file);
|
|
|
|
promises.push(
|
|
handle_json_schema_yaml_file(state, in_file)
|
|
);
|
|
}
|
|
|
|
await Promise.all(promises);
|
|
}
|
|
|
|
export async function handle_json_schema_json_file(state: BuildState, in_file: string) {
|
|
const promises: Promise<any>[] = [ ];
|
|
|
|
const out_file = map_input_file_to_output_file(state, in_file, [ '.json' ], '.html');
|
|
const { frontmatter, parsed, json, hash } = await read_json(in_file, true);
|
|
|
|
promises.push(
|
|
render_json_schema(state, parsed, in_file, await out_file, hash, frontmatter)
|
|
);
|
|
|
|
if (state.conf.output.include_inputs?.includes('schema+json')) {
|
|
const json_file = await map_input_file_to_output_file(state, in_file);
|
|
|
|
promises.push(
|
|
write_text(json_file, json)
|
|
);
|
|
|
|
if (state.conf.output.include_yaml_and_json) {
|
|
const yaml_file = await map_input_file_to_output_file(state, in_file, [ '.json' ], '.yaml');
|
|
|
|
promises.push(
|
|
write_text(yaml_file, to_yaml(parsed))
|
|
);
|
|
}
|
|
}
|
|
|
|
await Promise.all(promises);
|
|
}
|
|
|
|
export async function handle_json_schema_yaml_file(state: BuildState, in_file: string) {
|
|
const promises: Promise<any>[] = [ ];
|
|
|
|
const out_file = map_input_file_to_output_file(state, in_file, [ '.yaml', '.yml' ], '.html');
|
|
const { frontmatter, parsed, yaml, hash } = await read_yaml(in_file, true);
|
|
|
|
promises.push(
|
|
render_json_schema(state, parsed, in_file, await out_file, hash, frontmatter)
|
|
);
|
|
|
|
if (state.conf.output.include_inputs?.includes('schema+yaml')) {
|
|
const yaml_file = await map_input_file_to_output_file(state, in_file);
|
|
|
|
promises.push(
|
|
write_text(yaml_file, yaml)
|
|
);
|
|
|
|
if (state.conf.output.include_yaml_and_json) {
|
|
const json_file = await map_input_file_to_output_file(state, in_file, [ '.yaml', '.yml' ], '.json');
|
|
|
|
promises.push(
|
|
write_text(json_file, JSON.stringify(parsed, null, ' '))
|
|
);
|
|
}
|
|
}
|
|
|
|
await Promise.all(promises);
|
|
}
|
|
|
|
export async function render_json_schema(state: BuildState, schema: unknown, in_file: string, out_file: string, hash: string, frontmatter?: any) {
|
|
if (frontmatter?.skip) {
|
|
return;
|
|
}
|
|
|
|
const out_url = map_output_file_to_url(state, out_file);
|
|
|
|
const promises: Promise<any>[] = [ ];
|
|
const markdown = build_markdown_from_json_schema(schema);
|
|
|
|
if (state.conf.output.include_intermediate_markdown) {
|
|
promises.push(
|
|
map_input_file_to_output_file(state, out_file, [ '.html' ], '.md')
|
|
.then((md_file) => write_text(md_file, markdown))
|
|
);
|
|
}
|
|
|
|
promises.push(
|
|
render_page(state, in_file, out_file, out_url, markdown, true, hash, frontmatter)
|
|
);
|
|
|
|
await Promise.all(promises);
|
|
}
|