2020-03-27 20:10:52 +00:00
"use strict" ;
import fs from 'fs' ;
import path from 'path' ;
import TOML from '@iarna/toml' ;
import CliParser from 'applause-cli' ;
2021-01-25 18:24:25 +00:00
import l from '../Helpers/Log.mjs' ;
import { LOG _LEVELS } from '../Helpers/Log.mjs' ;
2020-03-27 20:10:52 +00:00
import a from '../Helpers/Ansi.mjs' ;
import settings from './settings.mjs' ;
2020-10-16 16:35:40 +00:00
import { get _version } from 'terrain50' ;
2020-03-27 20:10:52 +00:00
const _ _dirname = import . meta . url . slice ( 7 , import . meta . url . lastIndexOf ( "/" ) ) ;
const subcommand _directory = "../Subcommands" ;
async function get _actions ( ) {
let dirs = await fs . promises . readdir (
path . resolve (
_ _dirname ,
subcommand _directory
)
) ;
return dirs . map ( ( dir ) => {
let index _file = path . resolve ( _ _dirname , ` ${ subcommand _directory } / ${ dir } / ` , "index.mjs" ) ;
if ( ! fs . existsSync ( index _file ) )
return null ;
return path . basename ( path . dirname ( index _file ) ) ;
} ) . filter ( x => x !== null ) ;
}
async function get _actions _metadata ( ) {
let result = { } ;
for ( let action of await get _actions ( ) ) {
let filepath = path . resolve (
_ _dirname ,
` ${ subcommand _directory } / ${ action } / ` ,
` meta.toml `
) ;
if ( ! fs . existsSync ( filepath ) ) {
result [ action ] = {
description : ` ${ a . locol } ${ a . italics } (No description found) ${ a . reset } ` ,
arguments : [ ]
} ;
continue ;
}
result [ action ] = TOML . parse ( await fs . promises . readFile ( filepath ) ) ;
}
return result ;
}
export default async function ( ) {
let cli = new CliParser ( path . resolve ( _ _dirname , "../../package.json" ) ) ;
2021-04-20 00:34:26 +00:00
cli . set _name ( "terrain50" ) ; // The command name is actually terrain50, even though the package name is terrain50-cli
2020-10-16 16:35:40 +00:00
cli . set _description _extended ( ` With terrain50 ${ await get _version ( ) } ` ) ;
2021-01-25 18:24:25 +00:00
cli . argument ( "log-level" , "The logging level. Possible values: debug (default), info, log, warn, error, none" , "debug" , "string" ) ;
2021-01-15 17:06:42 +00:00
cli . argument ( "tolerant" , "When parsing streams of data, be more tolerant of whitespace inconsistencies and other errors at the cost of decreased performance (otherwise it is assumed a single space separates elements on a line)." , false , "boolean" ) ;
2020-03-27 20:10:52 +00:00
// Disable ansi escape codes if requested
if ( ! settings . output . ansi _colour ) {
a . enabled = false ;
a . escape _codes ( ) ;
}
let actions _meta = await get _actions _metadata ( ) ;
for ( let action in actions _meta ) {
let subcommand = cli . subcommand ( action , actions _meta [ action ] . description ) ;
if ( ! ( actions _meta [ action ] . arguments instanceof Array ) )
continue ;
for ( let argument of actions _meta [ action ] . arguments ) {
subcommand . argument (
argument . name ,
argument . description ,
argument . default _value ,
argument . type
) ;
}
}
// 2: CLI Argument Parsing
settings . cli = cli . parse ( process . argv . slice ( 2 ) ) ;
let action = cli . current _subcommand ;
if ( action == null ) {
console . error ( ` ${ a . hicol } ${ a . fred } Error: No subcommand specified (try --help for usage information). ${ a . reset } ` ) ;
return ;
}
2021-01-25 18:24:25 +00:00
if ( typeof LOG _LEVELS [ settings . cli . log _level . toUpperCase ( ) ] == "undefined" ) {
console . error ( ` ${ a . hicol } ${ a . fred } Error: Unknown log level ' ${ settings . cli . log _level } ' (possible values: debug, info, log, warn, error, none) ` ) ;
process . exit ( 1 ) ;
}
l . level = LOG _LEVELS [ settings . cli . log _level . toUpperCase ( ) ] ;
2020-03-27 20:10:52 +00:00
// 3: Environment Variable Parsing
// process.env.XYZ
// 4: Run
console . error ( ` ${ a . fgreen } ***** ${ a . hicol } ${ action } ${ a . reset } ${ a . fgreen } ***** ${ a . reset } ` ) ;
try {
await ( await import ( ` ${ subcommand _directory } / ${ action } /index.mjs ` ) ) . default ( settings ) ;
}
catch ( error ) {
console . error ( ` \n \n ` ) ;
throw error ;
}
// 5: Cleanup
}