/**
 * dtsmake command line interface
 */ import * as program from "commander" ; import { dtsmake } from "./dtsmake" ; import * as child_process from "child_process" ; var myPackage = require ( "../package.json" ) ; var srcFiles = ( val ) : string [ ] => { return val . split ( "," ) ; } ; var plugins = ( val ) : string [ ] => { return val . split ( "," ) ; } ; var defFiles = function ( val ) : string [ ] { return val . split ( "," ) ; } ; var extraFiles = function ( val ) : string [ ] { console . log ( "[EXTRA]" + val ) ; return val . split ( "," ) ; } ; var libDTSs = ( val ) : string [ ] => { return val . split ( "," ) ; } ; program . version ( myPackage . version , "-v, --version" ) . option ( "-s, --src <path>" , "[MUST] target javascript file path" , srcFiles ) . option ( "--dist [value]" , "outout d.ts file path. no need `.d.ts` file extension." ) . option ( "-n, --n [value]" , "module name" ) . option ( "-p, --plugin <names>" , "tern.js plugin. see tern.js server plugin(http://ternjs.net/doc/manual.html#plugins)" , plugins ) . option ( "-d, --def <paths>" , "tern.js def files. DEFAULT:'ecma5'" , defFiles ) . option ( "-x, --extrafiles <paths>" , "sample files for target js lib. help for ternjs type inference." , extraFiles ) . option ( "-D, --debug" , "debug output mode" ) . option ( "-A, --voidAsAny" , "force output `void` to `any`" ) . option ( "-i, --interfaceSameNameVar" , "export a namespace property same with a interface name" , true ) . option ( "-a, --annotateTypeInstance" , "annotate interface's constructor type as return type instance" , true ) . option ( "-g, --globalObject [value]" , `how to export objects that same name with JS Global Object; "remove" or "wrap" or "rename"; DEFAULT:"wrap" ` ) . option ( "-N, --NodeJSModule" , "nodejs module special replace" ) . option ( "-e, --export" , "add export statement in a bottom of d.ts file" ) . option ( "-S, --exportStyle [value]" , `if --outExport true, select export style "es6" or "legacy"` ) . option ( "-M, --exportModuleName [value]" , 'exporting module name. ex. "EXAMPLE"; usage `import example = require("EXAMPLE");`' ) . option ( "-l, --lib <paths>" , "add referrece path d.ts files. ex. `--lib path/to/ex1.d.ts,path/to/ex2.d.ts`" , libDTSs ) . parse ( process . argv ) ; if ( ! ( < any > program ) . src ) { console . warn ( "--src option is MUST." ) ; program . help ( ) ; } else if ( ( < any > program ) . exportStyle && ! ( < any > program ) . export ) { console . warn ( "--exportStyle option needs --export option." ) ; process . exit ( 1 ) ; } else if ( ( < any > program ) . exportModuleName && ! ( < any > program ) . export ) { console . warn ( "--exportModuleName option needs --export option." ) ; process . exit ( 1 ) ; } /**
 *
 * `tern/bin/condense [--name name] [--plugin name]* [--def name]* [+extrafile.js]* [file.js]+`
 */ var genCommand = ( version , path ) => { var s = "" ; const major = Number ( version . split ( "." ) [ 0 ] ) ; if ( major >= 3 ) { s = `node "${ path }/tern/bin/condense"` ; } else { s = `node "${ __dirname }/../node_modules/tern/bin/condense"` ; } s += ( < any > program ) . n ? ` --name ${ ( < any > program ) . n }` : "" ; if ( ( < any > program ) . plugin ) { for ( let i in ( < any > program ) . plugin ) { s += ` --plugin ${ ( < any > program ) . plugin [ i ] }` ; } } if ( ( < any > program ) . def ) { for ( let i in ( < any > program ) . def ) { s += ` --def ${ ( < any > program ) . def [ i ] }` ; } } s += ( < any > program ) . extrafiles ? ` +${ ( < any > program ) . extrafiles . join ( " +" ) }` : "" ; s += " " + ( < any > program ) . src . join ( " " ) ; s += " --no-spans" ; console . log ( `${ program . name ( ) } started.` ) ; console . log ( "[CMD]" + s ) ; console . log ( "[SRC]" + ( < any > program ) . src . join ( " " ) ) ; return s ; } ; function npmVersion ( cb ) { child_process . exec ( "npm -v" , { encoding : "utf8" , maxBuffer : 2048 } , ( e , stdout , stderr ) => { cb ( stdout . toString ( ) , e ) ; } ) ; } function getDirNodeModules ( cb ) { child_process . exec ( "npm root" , { encoding : "utf8" , maxBuffer : 2048 } , ( e , stdout , stderr ) => { cb ( stdout . toString ( ) . replace ( /[\r\n]/g , "" ) , e ) ; } ) ; } getDirNodeModules ( ( path , e ) => { npmVersion ( ( version , e2 ) => { console . log ( `v:${ version }` ) ; exec ( version , path ) ; } ) ; } ) ; /**
 * exec tern/bin/condense
 */ function exec ( version , path ) { var child = child_process . exec ( genCommand ( version , path ) , { maxBuffer : 1000000 * 2048 } , ( err , stdout , stderr ) => { if ( err ) { console . warn ( "[INFO] tern/condense error" ) ; console . error ( err . toString ( ) ) ; return ; } else if ( stderr ) { console . warn ( "[INFO] tern/condense error" ) ; console . error ( stderr . toString ( ) ) ; return ; } else { if ( ( < any > program ) . debug ) console . log ( stdout . toString ( ) ) ; let dg = new dtsmake . DTSMake ( ) ; let op = { isDebug : ( < any > program ) . debug , isOutVoidAsAny : ( < any > program ) . voidAsAny , isAnnotateTypeInstance : ( < any > program ) . annotateTypeInstance , isExportInterfaceSameNameVar : ( < any > program ) . interfaceSameNameVar , isNodeJsModule : ( < any > program ) . NodeJSModule , isOutExport : ( < any > program ) . export , } ; if ( ( < any > program ) . exportStyle ) op . exportStyle = ( < any > program ) . exportStyle ; if ( ( < any > program ) . exportModuleName ) op . exportModuleName = ( < any > program ) . exportModuleName ; if ( ( < any > program ) . globalObject ) op . globalObject = ( < any > program ) . globalObject ; if ( ( < any > program ) . lib ) op . lib = ( < any > program ) . lib ; dg . main ( stdout . toString ( ) , ( < any > program ) . dist ? ( < any > program ) . dist : ( < any > program ) . src . toString ( ) . replace ( /\.[a-z0-9]+$/ , "" ) , op ) ; } } ) ; }