@@ -23,7 +23,7 @@ export async function question(query: string, password?: boolean) {
23
23
}
24
24
25
25
export function translateOptions ( options : Record < string , string > ) {
26
- const ret = { } ;
26
+ const ret : Record < string , string > = { } ;
27
27
for ( const key in options ) {
28
28
const v = options [ key ] ;
29
29
if ( typeof v === 'string' ) {
@@ -40,19 +40,21 @@ export function translateOptions(options: Record<string, string>) {
40
40
41
41
export function getRNVersion ( ) {
42
42
const version = JSON . parse (
43
- fs . readFileSync (
44
- require . resolve ( 'react-native/package.json' , {
45
- paths : [ process . cwd ( ) ] ,
46
- } ) ,
47
- ) ,
43
+ fs
44
+ . readFileSync (
45
+ require . resolve ( 'react-native/package.json' , {
46
+ paths : [ process . cwd ( ) ] ,
47
+ } ) ,
48
+ )
49
+ . toString ( ) ,
48
50
) . version ;
49
51
50
- // We only care about major and minor version.
51
- const match = / ^ ( \d + ) \. ( \d + ) \. / . exec ( version ) ;
52
+ const [ , major , minor ] = / ^ ( \d + ) \. ( \d + ) \. / . exec ( version ) || [ ] ;
53
+
52
54
return {
53
55
version,
54
- major : match [ 1 ] | 0 ,
55
- minor : match [ 2 ] | 0 ,
56
+ major : Number ( major ) ,
57
+ minor : Number ( minor ) ,
56
58
} ;
57
59
}
58
60
@@ -173,17 +175,22 @@ export function saveToLocal(originPath: string, destName: string) {
173
175
// fs.copyFileSync(originPath, destPath);
174
176
}
175
177
176
- async function getLatestVersion ( pkgName : string ) {
177
- return Promise . race ( [
178
- latestVersion ( pkgName )
179
- . then ( ( p ) => p . latest )
180
- . catch ( ( ) => '' ) ,
181
- new Promise ( ( resolve ) => setTimeout ( ( ) => resolve ( '' ) , 2000 ) ) ,
182
- ] ) ;
178
+ async function getLatestVersion ( pkgNames : string [ ] ) {
179
+ return latestVersion ( pkgNames , {
180
+ useCache : true ,
181
+ requestOptions : {
182
+ timeout : 2000 ,
183
+ } ,
184
+ } )
185
+ . then ( ( pkgs ) => pkgs . map ( ( pkg ) => pkg . latest ) )
186
+ . catch ( ( ) => [ ] ) ;
183
187
}
184
188
185
189
export async function printVersionCommand ( ) {
186
- let latestPushyCliVersion = await getLatestVersion ( 'react-native-update-cli' ) ;
190
+ let [ latestPushyCliVersion , latestPushyVersion ] = await getLatestVersion ( [
191
+ 'react-native-update-cli' ,
192
+ 'react-native-update' ,
193
+ ] ) ;
187
194
latestPushyCliVersion = latestPushyCliVersion
188
195
? ` (最新:${ chalk . green ( latestPushyCliVersion ) } )`
189
196
: '' ;
@@ -199,7 +206,6 @@ export async function printVersionCommand() {
199
206
} ,
200
207
) ;
201
208
pushyVersion = require ( PACKAGE_JSON_PATH ) . version ;
202
- let latestPushyVersion = await getLatestVersion ( 'react-native-update' ) ;
203
209
latestPushyVersion = latestPushyVersion
204
210
? ` (最新:${ chalk . green ( latestPushyVersion ) } )`
205
211
: '' ;
@@ -226,6 +232,4 @@ export async function printVersionCommand() {
226
232
}
227
233
}
228
234
229
-
230
-
231
235
export { checkPlugins } ;
0 commit comments