import * as ts from "typescript" ; import { emitAndGetCliSignature } from "./emitAndGetCliSignature" ; const defaultOptions = { target : ts . ScriptTarget . ES5 , module : ts . ModuleKind . CommonJS } ; function getConfigSearchPath ( argv ) { if ( argv [ 3 ] === "--project" || argv [ 3 ] === "-p" ) { if ( argv [ 4 ] == null ) { throw new Error ( `Missing value for option ${ argv [ 3 ] }` ) ; } return argv [ 4 ] ; } return ( < any > ts ) . normalizePath ( ts . sys . getCurrentDirectory ( ) ) ; } function getConfig ( argv ) { const configFileName = ts . findConfigFile ( getConfigSearchPath ( argv ) , ts . sys . fileExists ) ; if ( configFileName === undefined ) { return defaultOptions ; } const host = < any > ts . sys ; const parsedCommandLine = ts . getParsedCommandLineOfConfigFile ( configFileName , { } , host ) ; if ( parsedCommandLine === undefined ) { return defaultOptions ; } return parsedCommandLine . options ; } async function generateCli ( argv ) { const fileName = argv [ 2 ] ; if ( fileName === undefined ) { throw new Error ( "Typescript file path is missing" ) ; } const options = getConfig ( argv ) ; let program = ts . createProgram ( [ fileName ] , options ) ; await emitAndGetCliSignature ( fileName , program ) ; } if ( process . argv . length < 2 ) { process . stderr . write ( "Typescript file path is missing\n" ) ; process . exit ( 1 ) ; } try { generateCli ( process . argv ) . catch ( err => { process . stderr . write ( err . message + "\n" ) ; process . exit ( 1 ) ; } ) ; } catch ( err ) { process . stderr . write ( err . message + "\n" ) ; process . exit ( 1 ) ; }