@@ -45,46 +45,10 @@ const getInstalledVersion = memo(() => {
4545} ) ;
4646
4747/**
48- * Returns the desired `react-native` version.
49- *
50- * Checks the following in order:
51- *
52- * - Command line flag, e.g. `--version 0.81`
53- * - Currently installed `react-native` version
54- * - Latest version from npm
55- *
5648 * @param {import("./types.js").Platform[] } platforms
5749 * @returns {Promise<string> }
5850 */
59- async function getVersion ( platforms ) {
60- const index = process . argv . lastIndexOf ( "--version" ) ;
61- if ( index >= 0 ) {
62- const m = process . argv [ index + 1 ] . match ( / ( \d + \. \d + [ - . 0 - 9 a - z ] * ) / ) ;
63- if ( ! m ) {
64- throw new Error (
65- "Expected version number of the form <major>.<minor>.<patch>-<prerelease> (where patch and prerelease are optional)"
66- ) ;
67- }
68- return m [ 1 ] ;
69- }
70-
71- /** @type {(version: string, reason: string) => void } */
72- const logVersion = ( version , reason ) => {
73- const bVersionFlag = colors . bold ( "--version" ) ;
74- const bTarget = colors . bold ( version ) ;
75- console . log (
76- `Using ${ bTarget } because ${ reason } (use ${ bVersionFlag } to specify another version)`
77- ) ;
78- } ;
79-
80- const version = getInstalledVersion ( ) ;
81- if ( version ) {
82- logVersion ( version , "the current project uses it" ) ;
83- return version ;
84- }
85-
86- console . log ( "No version was specified; fetching available versions..." ) ;
87-
51+ async function getLatestCommonVersion ( platforms ) {
8852 let maxSupportedVersion = Number . MAX_VALUE ;
8953 for ( const p of platforms ) {
9054 const pkgName = getDefaultPlatformPackageName ( p ) ;
@@ -103,7 +67,58 @@ async function getVersion(platforms) {
10367 const major = Math . trunc ( maxSupportedVersion / 1000 ) ;
10468 const minor = maxSupportedVersion % 1000 ;
10569
106- const target = major + "." + minor ;
70+ return major + "." + minor ;
71+ }
72+
73+ /**
74+ * @param {string } version
75+ * @param {string } reason
76+ * @returns {void }
77+ */
78+ function logVersion ( version , reason ) {
79+ const bVersionFlag = colors . bold ( "--version" ) ;
80+ const bTarget = colors . bold ( version ) ;
81+ console . log (
82+ `Using ${ bTarget } because ${ reason } (use ${ bVersionFlag } to specify another version)`
83+ ) ;
84+ }
85+
86+ /**
87+ * Returns the desired `react-native` version.
88+ *
89+ * Checks the following in order:
90+ *
91+ * - Command line flag, e.g. `--version 0.81`
92+ * - Currently installed `react-native` version
93+ * - Latest version from npm
94+ *
95+ * @param {import("./types.js").Platform[] } platforms
96+ * @returns {Promise<string> }
97+ */
98+ async function getVersion ( platforms ) {
99+ const index = process . argv . lastIndexOf ( "--version" ) ;
100+ const input = index >= 0 && process . argv [ index + 1 ] ;
101+ if ( input !== "highest" ) {
102+ if ( input ) {
103+ const m = input . match ( / ( \d + \. \d + [ - . 0 - 9 a - z ] * ) / ) ;
104+ if ( ! m ) {
105+ throw new Error (
106+ "Expected version number of the form <major>.<minor>.<patch>-<prerelease> (where patch and prerelease are optional)"
107+ ) ;
108+ }
109+ return m [ 1 ] ;
110+ } else {
111+ const installedVersion = getInstalledVersion ( ) ;
112+ if ( installedVersion ) {
113+ logVersion ( installedVersion , "the current project uses it" ) ;
114+ return installedVersion ;
115+ }
116+ }
117+
118+ console . log ( "No version was specified; fetching available versions..." ) ;
119+ }
120+
121+ const target = await getLatestCommonVersion ( platforms ) ;
107122 logVersion ( target , "it supports all specified platforms" ) ;
108123
109124 return target ;
0 commit comments