2023-02-20 15:18:53 +01:00
/ * *
* /
2023-02-20 14:46:28 +01:00
async function main (
args_raw : Array < string >
) : Promise < void >
{
const outputs : Record < string , type_output > = {
"sqlite" : output_sqlite ,
"mysql" : output_mysql ,
2023-05-11 12:59:02 +02:00
"typescript" : output_typescript ,
"jsonschema" : output_jsonschema ,
2023-02-20 14:46:28 +01:00
} ;
2023-05-11 12:59:02 +02:00
const arg_handler = new lib_plankton . args . class_handler (
2023-02-20 14:46:28 +01:00
{
2023-05-11 12:59:02 +02:00
"format" : new lib_plankton . args . class_argument ( {
2023-02-20 14:46:28 +01:00
"name" : "format" ,
2023-05-11 12:59:02 +02:00
"type" : lib_plankton . args . enum_type . string ,
"kind" : lib_plankton . args . enum_kind . volatile ,
"mode" : lib_plankton . args . enum_mode . replace ,
2023-02-20 14:46:28 +01:00
"default" : "sqlite" ,
"parameters" : {
"indicators_long" : [ "format" ] ,
"indicators_short" : [ "f" ] ,
} ,
"info" : "output format" ,
} ) ,
2023-05-11 12:59:02 +02:00
"schema" : new lib_plankton . args . class_argument ( {
2023-02-20 15:18:53 +01:00
"name" : "schema" ,
2023-05-11 12:59:02 +02:00
"type" : lib_plankton . args . enum_type . boolean ,
"kind" : lib_plankton . args . enum_kind . volatile ,
"mode" : lib_plankton . args . enum_mode . replace ,
2023-02-20 15:18:53 +01:00
"default" : false ,
"parameters" : {
"indicators_long" : [ "schema" ] ,
"indicators_short" : [ "s" ] ,
} ,
"info" : "print sindri JSON schema to stdout and exit" ,
} ) ,
2023-05-11 12:59:02 +02:00
"help" : new lib_plankton . args . class_argument ( {
2023-02-20 15:18:53 +01:00
"name" : "help" ,
2023-05-11 12:59:02 +02:00
"type" : lib_plankton . args . enum_type . boolean ,
"kind" : lib_plankton . args . enum_kind . volatile ,
"mode" : lib_plankton . args . enum_mode . replace ,
2023-02-20 15:18:53 +01:00
"default" : false ,
"parameters" : {
"indicators_long" : [ "help" ] ,
"indicators_short" : [ "h" ] ,
} ,
"info" : "print help to stdout and exit" ,
} ) ,
2023-02-20 14:46:28 +01:00
}
) ;
2023-05-11 12:59:02 +02:00
const args : Record < string , any > = arg_handler . read ( lib_plankton . args . enum_environment . cli , args_raw . join ( " " ) ) ;
2023-02-20 14:46:28 +01:00
2023-02-20 15:18:53 +01:00
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" ,
}
)
) ;
2023-02-20 14:46:28 +01:00
}
else {
2023-02-20 15:18:53 +01:00
if ( args [ "schema" ] ) {
process . stdout . write (
2023-05-11 12:59:02 +02:00
JSON . stringify ( input_schema ( ) , undefined , "\t" )
2023-02-20 15:18:53 +01:00
) ;
}
else {
const input_content : string = await lib_plankton . file . read_stdin ( ) ;
2023-05-11 12:59:02 +02:00
const input_data_raw : any = lib_plankton . json . decode ( input_content ) ;
const input_data : type_input = input_normalize ( input_data_raw ) ;
2023-02-20 15:18:53 +01:00
if ( ! outputs . hasOwnProperty ( args [ "format" ] ) ) {
throw ( new Error ( "unhandled output format: " + args [ "format" ] ) ) ;
}
else {
const output_content : string = outputs [ args [ "format" ] ] . render ( input_data ) ;
process . stdout . write ( output_content ) ;
}
}
2023-02-20 14:46:28 +01:00
}
}
main ( process . argv . slice ( 2 ) ) ;