2023-08-08 14:39:21 +02:00
namespace _sindri
2023-02-20 14:46:28 +01:00
{
2023-08-08 14:39:21 +02:00
/ * *
* /
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" ,
} ) ,
}
2023-02-20 15:18:53 +01:00
) ;
2023-08-08 14:39:21 +02:00
const args : Record < string , any > = arg_handler . read ( lib_plankton . args . enum_environment . cli , args_raw . join ( " " ) ) ;
if ( args [ "help" ] ) {
2023-02-20 15:18:53 +01:00
process . stdout . write (
2023-08-08 14:39:21 +02:00
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 15:18:53 +01:00
) ;
}
else {
2023-08-08 14:39:21 +02:00
if ( args [ "schema" ] ) {
process . stdout . write (
JSON . stringify ( _sindri . input_schema ( ) , undefined , "\t" )
) ;
2023-02-20 15:18:53 +01:00
}
else {
2023-08-08 14:39:21 +02:00
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 ) ;
}
}
2023-02-20 15:18:53 +01:00
}
}
2023-02-20 14:46:28 +01:00
}
2023-08-08 14:39:21 +02:00
2023-02-20 14:46:28 +01:00
}
2023-08-08 14:39:21 +02:00
_sindri . main ( process . argv . slice ( 2 ) ) ;