import ts from "typescript" ; import { extractParameterFlag , extractParameterValue } from "./extract" ; function reportParameterDependencyViolation ( dependent , dependee ) { throw new Error ( `--${ dependent } can only be used in conjunction with --${ dependee }` ) ; } function reportUnsupportTscCliArgument ( argName ) { throw new Error ( `tsc command-line parameter '--${ argName }' is not supported` ) ; } export type ParsedCommandLineRnTs = { platform ? : string ; platformExtensions ? : string [ ] ; disableReactNativePackageSubstitution ? : boolean ; traceReactNativeModuleResolutionErrors ? : boolean ; traceResolutionLog ? : string ; } ; export type CommandLine = { rnts : ParsedCommandLineRnTs ; ts : ts . ParsedCommandLine ; } ; export function parseCommandLineRnTs ( args ) { const argsCopy = [ ... args ] ; const platform = extractParameterValue ( argsCopy , "platform" ) ; const platformExtensions = extractParameterValue ( argsCopy , "platformExtensions" ) ?. split ( "," ) ; if ( ! platform && platformExtensions ) { reportParameterDependencyViolation ( "platformExtensions" , "platform" ) ; } const disableReactNativePackageSubstitution = extractParameterFlag ( argsCopy , "disableReactNativePackageSubstitution" ) ; if ( ! platform && disableReactNativePackageSubstitution ) { reportParameterDependencyViolation ( "disableReactNativePackageSubstitution" , "platform" ) ; } const traceReactNativeModuleResolutionErrors = extractParameterFlag ( argsCopy , "traceReactNativeModuleResolutionErrors" ) ; if ( ! platform && traceReactNativeModuleResolutionErrors ) { reportParameterDependencyViolation ( "traceReactNativeModuleResolutionErrors" , "platform" ) ; } const traceResolutionLog = extractParameterValue ( argsCopy , "traceResolutionLog" ) ; return { cmdLineRnTs : { platform , platformExtensions , disableReactNativePackageSubstitution , traceReactNativeModuleResolutionErrors , traceResolutionLog } , tsArgs : argsCopy } ; } export function parseCommandLineTs ( args ) { if ( args . length > 2 ) { if ( args [ 2 ] . toLowerCase ( ) === "--build" ) { reportUnsupportTscCliArgument ( "--build" ) ; } else if ( args [ 2 ] . toLowerCase ( ) === "-b" ) { reportUnsupportTscCliArgument ( "-b" ) ; } } const cmdLine = ts . parseCommandLine ( args . slice ( 2 ) ) ; if ( ! cmdLine ) { throw new Error ( "failed to parse TypeScript command-line options" ) ; } return cmdLine ; } export function parseCommandLine ( args ) { const { cmdLineRnTs , tsArgs : tscArgs } = parseCommandLineRnTs ( args ) ; const cmdLineTs = parseCommandLineTs ( tscArgs ) ; return { rnts : cmdLineRnTs , ts : cmdLineTs } ; }