64 lines
1.4 KiB
TypeScript
64 lines
1.4 KiB
TypeScript
|
|
import { promises as fs } from 'fs';
|
|
import { parse as parse_yaml } from 'yaml';
|
|
import { resolve as resolve_path, dirname } from 'path';
|
|
|
|
export async function read_config(file: string) {
|
|
const path = resolve_path(process.cwd(), file);
|
|
const yaml = await fs.readFile(path, 'utf8');
|
|
const config = parse_yaml(yaml);
|
|
validate_config(config);
|
|
resolve_paths(path, config);
|
|
return config;
|
|
}
|
|
|
|
export interface Config {
|
|
input: {
|
|
root: string;
|
|
raw?: string[];
|
|
text?: string[];
|
|
markdown?: string[];
|
|
'schema+json'?: string[];
|
|
'schema+yaml'?: string[];
|
|
'openapi+json'?: string[];
|
|
'openapi+yaml'?: string[];
|
|
// ...
|
|
};
|
|
templates?: {
|
|
layouts?: string;
|
|
partials?: string;
|
|
env?: string[];
|
|
};
|
|
output: {
|
|
root: string;
|
|
};
|
|
markdown?: {
|
|
//
|
|
};
|
|
schema?: {
|
|
//
|
|
};
|
|
openapi?: {
|
|
// ;
|
|
}
|
|
// ...
|
|
}
|
|
|
|
function validate_config(config: unknown) : asserts config is Config {
|
|
// todo: validate config
|
|
}
|
|
|
|
function resolve_paths(file_path: string, config: Config) {
|
|
const base_path = dirname(file_path);
|
|
config.input.root = resolve_path(base_path, config.input.root);
|
|
config.output.root = resolve_path(base_path, config.output.root);
|
|
|
|
if (config.templates?.layouts) {
|
|
config.templates.layouts = resolve_path(base_path, config.templates.layouts);
|
|
}
|
|
|
|
if (config.templates?.partials) {
|
|
config.templates.partials = resolve_path(base_path, config.templates.partials);
|
|
}
|
|
}
|