import * as yargs from "yargs" ; import { FS , getDefinitelyTyped } from "./get-definitely-typed" ; import { Options , writeDataFile } from "./lib/common" ; import { getTypingInfo } from "./lib/definition-parser" ; import { definitionParserWorkerFilename } from "./lib/definition-parser-worker" ; import { AllPackages , readNotNeededPackages , typesDataFilename , TypingsVersionsRaw } from "./lib/packages" ; import { parseNProcesses } from "./tester/test-runner" ; import { LoggerWithErrors , loggerWithErrors } from "./util/logging" ; import { assertDefined , filterNAtATimeOrdered , logUncaughtErrors , runWithChildProcesses } from "./util/util" ; if ( ! module . parent ) { const singleName = yargs . argv . single as string | undefined ; const options = Options . defaults ; logUncaughtErrors ( async ( ) => { const log = loggerWithErrors ( ) [ 0 ] ; const dt = await getDefinitelyTyped ( options , log ) ; if ( singleName ) { await single ( singleName , dt ) ; } else { await parseDefinitions ( dt , options . parseInParallel ? { nProcesses : parseNProcesses ( ) , definitelyTypedPath : assertDefined ( options . definitelyTypedPath ) } : undefined , log ) ; } } ) ; } export interface ParallelOptions { readonly nProcesses : number ; readonly definitelyTypedPath : string ; } export default async function parseDefinitions ( dt , parallel , log ) { log . info ( "Parsing definitions..." ) ; const typesFS = dt . subDir ( "types" ) ; const packageNames = await filterNAtATimeOrdered ( parallel ? parallel . nProcesses : 1 , typesFS . readdir ( ) , name => typesFS . isDirectory ( name ) ) ; log . info ( `Found ${ packageNames . length } packages.` ) ; const typings = { } ; const start = Date . now ( ) ; if ( parallel ) { log . info ( "Parsing in parallel..." ) ; await runWithChildProcesses ( { inputs : packageNames , commandLineArgs : [ `${ parallel . definitelyTypedPath }/types` ] , workerFile : definitionParserWorkerFilename , nProcesses : parallel . nProcesses , handleOutput ( { data , packageName } ) { typings [ packageName ] = data ; } , } ) ; } else { log . info ( "Parsing non-parallel..." ) ; for ( const packageName of packageNames ) { typings [ packageName ] = getTypingInfo ( packageName , typesFS . subDir ( packageName ) ) ; } } log . info ( "Parsing took " + ( ( Date . now ( ) - start ) / 1000 ) + " s" ) ; await writeDataFile ( typesDataFilename , sorted ( typings ) ) ; return AllPackages . from ( typings , readNotNeededPackages ( dt ) ) ; } function sorted < T > ( obj ) { const out = { } ; for ( const key of Object . keys ( obj ) . sort ( ) ) { out [ key ] = obj [ key ] ; } return out ; } async function single ( singleName , dt ) { const data = getTypingInfo ( singleName , dt . subDir ( "types" ) . subDir ( singleName ) ) ; const typings = { [ singleName ] : data } ; await writeDataFile ( typesDataFilename , typings ) ; console . log ( JSON . stringify ( data , undefined , 4 ) ) ; }