import cli from "cli" ; import fs from "fs" ; import path from "path" ; import tsc from "typescript" ; import { NestiaApplication } from "../NestiaApplication" ; import { Terminal } from "../utils/Terminal" ; import { stripJsonComments } from "../utils/stripJsonComments" ; interface ICommand { out : string | null ; } async function sdk ( input , command ) { let compilerOptions = { } ; if ( fs . existsSync ( "nestia.config.ts" ) === true ) { const config = await import ( path . resolve ( "nestia.config.ts" ) ) ; compilerOptions = config . compilerOptions ; input = config . input instanceof Array ? config . input : [ config . input ] ; command . out = config . output ; } if ( command . out === null ) throw new Error ( `Output directory is not specified. Add the "--out <output_directory>" option.` ) ; const parentPath = path . resolve ( command . out + "/.." ) ; const parentStats = await fs . promises . stat ( parentPath ) ; if ( parentStats . isDirectory ( ) === false ) throw new Error ( `Unable to find parent directory of the output path: "${ parentPath }".` ) ; for ( const path of input ) { const inputStats = await fs . promises . stat ( path ) ; if ( inputStats . isDirectory ( ) === false ) throw new Error ( `Target "${ path }" is not a directory.` ) ; } if ( fs . existsSync ( "tsconfig.json" ) === true ) { const content = await fs . promises . readFile ( "tsconfig.json" , "utf8" ) ; const options = JSON . parse ( stripJsonComments ( content ) ) . compilerOptions ; compilerOptions = compilerOptions ? { ... options , ... compilerOptions } : options ; } const app = new NestiaApplication ( { output : command . out , input , compilerOptions , } ) ; await app . generate ( ) ; } async function install ( ) { await Terminal . execute ( "npm install --save nestia-fetcher" ) ; } async function main ( ) { if ( process . argv [ 2 ] === "install" ) await install ( ) ; else if ( process . argv [ 2 ] === "sdk" ) { const command = cli . parse ( { out : [ "o" , "Output path of the SDK files" , "string" , null ] , } ) ; try { const inputs = [ ] ; for ( const arg of process . argv . slice ( 3 ) ) { if ( arg [ 0 ] === "-" ) break ; inputs . push ( arg ) ; } await sdk ( inputs , command ) ; } catch ( exp ) { console . log ( exp ) ; process . exit ( - 1 ) ; } } else { console . log ( `nestia supports only two commands; install and sdk, however you typed ${ process . argv [ 2 ] }` ) ; process . exit ( - 1 ) ; } } main ( ) ;