/**
 * Copyright (c) 2016-present, Facebook, Inc.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */ import * as List from './list' ; import * as Logging from './logging' ; import * as Maybe from './maybe' ; import * as minimist from 'minimist' ; import * as OutputControl from './output-control' ; export interface Arguments { givenPaths : string [ ] ; adtConfigPath : string | undefined ; valueObjectConfigPath : string | undefined ; objectConfigPath : string | undefined ; interestedLoggingTypes : List . List < Logging . LoggingType > ; minimalLevel : number ; dryRun : boolean ; outputPath : string | undefined ; includes : string [ ] ; excludes : string [ ] ; outputFlags : OutputControl . OutputFlags ; } const VERBOSE_FLAG = 'verbose' ; const PERF_LOGGING_FLAG = 'perf-log' ; const DEBUG_LOGGING_FLAG = 'debug' ; const SILENT_LOGGING_FLAG = 'silent' ; const DRY_RUN_FLAG = 'dry-run' ; const OUTPUT_PATH = 'output-path' ; const INCLUDE = 'include' ; const EXCLUDE = 'exclude' ; const HEADERS_ONLY = 'headers-only' ; const IMPL_ONLY = 'implementations-only' ; const EMIT = 'emit' ; const SINGLE_FILE = 'output-single-file' ; const ONE_CPU = 'one-cpu' ; const ADT_CONFIG_PATH = 'adt-config-path' ; const VALUE_OBJECT_CONFIG_PATH = 'value-object-config-path' ; const OBJECT_CONFIG_PATH = 'object-config-path' ; function interestedTypesForArgs ( args ) { if ( args [ SILENT_LOGGING_FLAG ] ) { return List . of < Logging . LoggingType > ( ) ; } else { const baseList = List . of < Logging . LoggingType > ( ) ; const baseListIncludingPerformance = args [ PERF_LOGGING_FLAG ] ? List . cons ( Logging . LoggingType . performance , baseList ) : baseList ; const baseListIncludingDebug = args [ DEBUG_LOGGING_FLAG ] ? List . cons ( Logging . LoggingType . debug , baseListIncludingPerformance ) : baseListIncludingPerformance ; const baseListIncludingError = List . cons ( Logging . LoggingType . error , baseListIncludingDebug ) ; return List . cons ( Logging . LoggingType . info , baseListIncludingError ) ; } } function sanitizeArrayArg ( arg ) { if ( typeof arg == 'string' ) { return [ arg ] ; } else if ( Array . isArray ( arg ) ) { return arg ; } else { return [ ] ; } } function findInArray ( list , findFunc ) { var item ; for ( item of list ) { if ( findFunc ( item ) ) { return true ; } } return false ; } function sanitizeEmitOption ( arg ) { const sanitized = sanitizeArrayArg ( arg ) ; if ( findInArray ( sanitized , function ( element ) { return element . toLowerCase ( ) == 'all' ; } ) === true ) { return [ ] ; } else { return sanitized ; } } function sanitizeBooleanArg ( arg , defaultValue ) { if ( typeof arg == 'boolean' ) { return arg ; } else if ( typeof arg == 'string' ) { if ( arg . toLowerCase ( ) == 'true' || arg . toLowerCase ( ) == 'yes' || arg . toLowerCase ( ) == 'y' ) { return true ; } else { return false ; } } else if ( typeof arg == 'number' ) { return arg == 1 ; } else { return defaultValue ; } } export function parseArgs ( args ) { const opts = { boolean : [ VERBOSE_FLAG , PERF_LOGGING_FLAG , DEBUG_LOGGING_FLAG , SILENT_LOGGING_FLAG , DRY_RUN_FLAG , HEADERS_ONLY , IMPL_ONLY , SINGLE_FILE , ONE_CPU , ] , string : [ ADT_CONFIG_PATH , VALUE_OBJECT_CONFIG_PATH , OBJECT_CONFIG_PATH , INCLUDE , EXCLUDE , OUTPUT_PATH , EMIT , ] , default : { [ HEADERS_ONLY ] : false , [ IMPL_ONLY ] : false } } ; const parsedArgs = minimist ( args , opts ) ; const sanitizedHeadersOnly = sanitizeBooleanArg ( parsedArgs [ HEADERS_ONLY ] , false ) ; const sanitizedImplsOnly = sanitizeBooleanArg ( parsedArgs [ IMPL_ONLY ] , false ) ; if ( parsedArgs . _ . length === 0 ) { return null ; } else if ( sanitizedHeadersOnly && sanitizedImplsOnly ) { console . log ( 'Error: %s and %s cannot both be set simultaneously' , HEADERS_ONLY , IMPL_ONLY ) ; return null ; } else { return { givenPaths : parsedArgs . _ , adtConfigPath : parsedArgs [ ADT_CONFIG_PATH ] , valueObjectConfigPath : parsedArgs [ VALUE_OBJECT_CONFIG_PATH ] , objectConfigPath : parsedArgs [ OBJECT_CONFIG_PATH ] , interestedLoggingTypes : interestedTypesForArgs ( parsedArgs ) , minimalLevel : parsedArgs [ VERBOSE_FLAG ] ? 1 : 10 , dryRun : parsedArgs [ DRY_RUN_FLAG ] , outputPath : parsedArgs [ OUTPUT_PATH ] , includes : sanitizeArrayArg ( parsedArgs [ INCLUDE ] ) , excludes : sanitizeArrayArg ( parsedArgs [ EXCLUDE ] ) , outputFlags : { emitHeaders : ! sanitizedImplsOnly , emitImplementations : ! sanitizedHeadersOnly , outputList : sanitizeEmitOption ( parsedArgs [ EMIT ] ) , singleFile : sanitizeBooleanArg ( parsedArgs [ SINGLE_FILE ] , false ) } } ; } }