import { readPackage } from "@rnx-kit/tools-node/package" ; import isString from "lodash/isString" ; import prompts from "prompts" ; import { checkPackageManifest } from "./check" ; import { concatVersionRanges , keysOf , modifyManifest } from "./helpers" ; import { defaultProfiles , parseProfilesString } from "./profiles" ; import type { Command , ProfileVersion } from "./types" ; function profileToChoice ( version ) { return { title : version , value : version } ; } async function parseInput ( versions ) { if ( isString ( versions ) && versions ) { return parseProfilesString ( versions ) ; } const { supportedVersions } = await prompts ( { type : "multiselect" , name : "supportedVersions" , message : "Select all supported versions of `react-native`" , choices : keysOf ( defaultProfiles ) . map ( profileToChoice ) , min : 1 } ) ; if ( ! Array . isArray ( supportedVersions ) ) { return { } ; } const targetVersion = supportedVersions . length === 1 ? supportedVersions [ 0 ] : ( await prompts ( { type : "select" , name : "targetVersion" , message : "Select development version of `react-native`" , choices : supportedVersions . map ( profileToChoice ) } ) ) . targetVersion ; if ( ! supportedVersions . includes ( targetVersion ) ) { return { } ; } return { supportedVersions : concatVersionRanges ( supportedVersions ) , targetVersion } ; } export async function makeSetVersionCommand ( versions ) { const { supportedVersions , targetVersion } = await parseInput ( versions ) ; if ( ! supportedVersions ) { return undefined ; } const checkOnly = { loose : false , write : false } ; const write = { loose : false , write : true } ; return ( manifestPath ) => { const checkReturnCode = checkPackageManifest ( manifestPath , checkOnly ) ; if ( checkReturnCode !== 0 ) { return checkReturnCode ; } const manifest = readPackage ( manifestPath ) ; const rnxKitConfig = manifest [ "rnx-kit" ] ; if ( ! rnxKitConfig ) { return 0 ; } rnxKitConfig . reactNativeVersion = supportedVersions ; if ( rnxKitConfig . kitType === "app" ) { delete rnxKitConfig . reactNativeDevVersion ; } else { rnxKitConfig . reactNativeDevVersion = targetVersion ; } modifyManifest ( manifestPath , manifest ) ; return checkPackageManifest ( manifestPath , write ) ; } ; }