import fs from "fs" ; import path from "path" ; import { CommandBuildElements } from "../src/lib/commandBuilder" ; interface Command { command : string ; subcommands : CommandBuildElements [ ] ; } interface CommandElement extends CommandBuildElements { markdown ? : string ; } const getAllDecorators = ( curDir ) : CommandBuildElements [ ] => { const allFiles = fs . readdirSync ( curDir ) ; const jsonFiles = allFiles . filter ( ( f ) => f . endsWith ( ".json" ) ) ; const arrJson = [ ] ; jsonFiles . forEach ( ( fileName ) => { const json = require ( path . join ( curDir , fileName ) ) as CommandElement ; if ( ! json . disabled ) { const mdPath = path . join ( curDir , fileName . replace ( /decorator\.json$/ , "md" ) ) ; if ( fs . existsSync ( mdPath ) ) { json . markdown = fs . readFileSync ( mdPath , "utf8" ) ; } arrJson . push ( json ) ; } } ) ; return arrJson ; } ; const getSubDirectories = ( curDir ) : string [ ] => { return fs . readdirSync ( curDir ) . map ( ( f ) => path . join ( curDir , f ) ) . filter ( ( p ) => fs . lstatSync ( p ) . isDirectory ( ) ) ; } ; const listCommands = ( allCommands ) : { [ key ] : CommandBuildElements ; } => { const mainCommands = { } ; allCommands . forEach ( ( cmd ) => { let level1 = cmd . command ; if ( level1 === "commands" ) { level1 = "" ; } else { level1 = level1 + " " ; } cmd . subcommands . forEach ( ( c ) => { const key = `${ level1 }${ c . command . replace ( / .+/ , "" ) }` ; mainCommands [ key ] = c ; } ) ; } ) ; return mainCommands ; } ; const dir = path . join ( process . cwd ( ) , "src" , "commands" ) ; const commandDirs = getSubDirectories ( dir ) ; commandDirs . unshift ( dir ) ; const commands = commandDirs . map ( ( d ) => { return { command : path . basename ( d ) , subcommands : getAllDecorators ( d ) , } ; } ) . filter ( ( item ) => item . subcommands . length > 0 ) ; const mapCommands = listCommands ( commands ) ; fs . writeFileSync ( path . join ( process . cwd ( ) , "docs" , "commands" , "data.json" ) , JSON . stringify ( mapCommands , null , 2 ) ) ;