136 lines
3.8 KiB
TypeScript
136 lines
3.8 KiB
TypeScript
namespace _sindri
|
|
{
|
|
|
|
/**
|
|
*/
|
|
export async function main(
|
|
args_raw : Array<string>
|
|
) : Promise<void>
|
|
{
|
|
const arg_handler = new lib_plankton.args.class_handler(
|
|
{
|
|
"format": new lib_plankton.args.class_argument({
|
|
"name": "format",
|
|
"type": lib_plankton.args.enum_type.string,
|
|
"kind": lib_plankton.args.enum_kind.volatile,
|
|
"mode": lib_plankton.args.enum_mode.replace,
|
|
"default": "database:sqlite",
|
|
"parameters": {
|
|
"indicators_long": ["format"],
|
|
"indicators_short": ["f"],
|
|
},
|
|
"info": "output format",
|
|
}),
|
|
"list": new lib_plankton.args.class_argument({
|
|
"name": "list",
|
|
"type": lib_plankton.args.enum_type.boolean,
|
|
"kind": lib_plankton.args.enum_kind.volatile,
|
|
"mode": lib_plankton.args.enum_mode.replace,
|
|
"default": false,
|
|
"parameters": {
|
|
"indicators_long": ["list"],
|
|
"indicators_short": ["l"],
|
|
},
|
|
"info": "list available output formats",
|
|
}),
|
|
"schema": new lib_plankton.args.class_argument({
|
|
"name": "schema",
|
|
"type": lib_plankton.args.enum_type.boolean,
|
|
"kind": lib_plankton.args.enum_kind.volatile,
|
|
"mode": lib_plankton.args.enum_mode.replace,
|
|
"default": false,
|
|
"parameters": {
|
|
"indicators_long": ["schema"],
|
|
"indicators_short": ["s"],
|
|
},
|
|
"info": "print sindri JSON schema to stdout and exit",
|
|
}),
|
|
"help": new lib_plankton.args.class_argument({
|
|
"name": "help",
|
|
"type": lib_plankton.args.enum_type.boolean,
|
|
"kind": lib_plankton.args.enum_kind.volatile,
|
|
"mode": lib_plankton.args.enum_mode.replace,
|
|
"default": false,
|
|
"parameters": {
|
|
"indicators_long": ["help"],
|
|
"indicators_short": ["h"],
|
|
},
|
|
"info": "print help to stdout and exit",
|
|
}),
|
|
}
|
|
);
|
|
const args : Record<string, any> = arg_handler.read(lib_plankton.args.enum_environment.cli, args_raw.join(" "));
|
|
|
|
if (args["help"]) {
|
|
process.stdout.write(
|
|
arg_handler.generate_help(
|
|
{
|
|
"programname": "sindri",
|
|
"author": "Christian Fraß <frass@greenscale.de>",
|
|
"description": "create data model scripts in different output formats (MySQL, SQLite, …) on basis of an abstract description; feed with .sindri.json file via stdin!",
|
|
"executable": "sindri",
|
|
}
|
|
)
|
|
);
|
|
}
|
|
else {
|
|
if (args["schema"]) {
|
|
process.stdout.write(
|
|
JSON.stringify(_sindri.input_schema(), undefined, "\t")
|
|
);
|
|
}
|
|
else {
|
|
if (args["list"]) {
|
|
process.stdout.write(
|
|
_sindri.list_outputs()
|
|
.map(
|
|
entry => lib_plankton.string.coin(
|
|
"{{realm}}:{{implementation}}\n",
|
|
{
|
|
"realm": entry.realm,
|
|
"implementation": entry.implementation,
|
|
}
|
|
)
|
|
)
|
|
.join("")
|
|
);
|
|
}
|
|
else {
|
|
const input_content : string = await lib_plankton.file.read_stdin();
|
|
const input_data_raw : any = lib_plankton.json.decode(input_content);
|
|
const input_data : type_input = _sindri.input_normalize(input_data_raw);
|
|
|
|
const format_parts : Array<string> = args["format"].split(":");
|
|
const realm_encoded : string = format_parts[0];
|
|
const realm : _sindri.enum_realm = {
|
|
"database": _sindri.enum_realm.database,
|
|
"backend": _sindri.enum_realm.backend,
|
|
"frontend": _sindri.enum_realm.frontend,
|
|
"other": _sindri.enum_realm.other,
|
|
}[realm_encoded];
|
|
const name : string = format_parts.slice(1).join(":");
|
|
|
|
let output : (null | _sindri.type_output);
|
|
try {
|
|
output = _sindri.get_output(realm, name);
|
|
}
|
|
catch (error) {
|
|
output = null;
|
|
}
|
|
if (output === null) {
|
|
throw (new Error("unhandled output format: " + args["format"]));
|
|
}
|
|
else {
|
|
const output_content : string = await output.render(input_data);
|
|
process.stdout.write(output_content);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
_sindri.main(process.argv.slice(2));
|