1
1
import fs from 'fs-extra' ;
2
2
import path from 'path' ;
3
- import * as fetch from 'npm-registry-fetch' ;
3
+ import * as npmRegistryFetch from 'npm-registry-fetch' ;
4
4
import chalk from 'chalk' ;
5
5
import { prompt } from 'prompts' ;
6
6
import execa from 'execa' ;
7
7
import semver from 'semver' ;
8
- import generateFileHash from './generateFileHash' ;
9
- import { getLoader } from './loader' ;
10
- import logger from './logger' ;
11
- import { CLIError } from './errors' ;
12
-
13
- export interface DependencyData {
14
- path : string ;
15
- version : string ;
16
- peerDependencies : { [ key : string ] : string } ;
17
- duplicates ?: DependencyData [ ] ;
18
- }
19
-
20
- function isUsingYarn ( root : string ) {
21
- return fs . existsSync ( path . join ( root , 'yarn.lock' ) ) ;
22
- }
23
-
24
- async function podInstall ( ) {
25
- process . chdir ( 'ios' ) ;
26
- const loader = getLoader ( ) ;
27
- try {
28
- loader . start ( 'Installing pods...' ) ;
29
- await execa ( 'bundle' , [ 'exec' , 'pod' , 'install' ] ) ;
30
- loader . succeed ( ) ;
31
- } catch ( error ) {
32
- const stderr = ( error as any ) . stderr || ( error as any ) . stdout ;
33
- loader . fail ( ) ;
34
- logger . error ( stderr ) ;
35
-
36
- throw new CLIError (
37
- 'Could not install pods. Try running pod installation manually.' ,
38
- ) ;
39
- } finally {
40
- process . chdir ( '..' ) ;
41
- }
42
- }
43
-
44
- function writeFile ( filePath : string , content : string ) {
45
- fs . writeFileSync ( filePath , content , { encoding : 'utf8' } ) ;
46
- }
8
+ import { getLoader , logger } from '@react-native-community/cli-tools' ;
9
+ import { DependencyMap } from '@react-native-community/cli-types' ;
10
+ import { isProjectUsingYarn } from './yarn' ;
47
11
48
12
export async function fetchAvailableVersions (
49
13
packageName : string ,
50
14
) : Promise < string [ ] > {
51
- const response = await fetch . json ( `/${ packageName } ` ) ;
15
+ const response = await npmRegistryFetch . json ( `/${ packageName } ` ) ;
52
16
53
17
return Object . keys ( response . versions || { } ) ;
54
18
}
@@ -84,64 +48,9 @@ export function findDependencyPath(
84
48
return dependencyPath ;
85
49
}
86
50
87
- export function collectDependencies ( root : string ) : Map < string , DependencyData > {
88
- const dependencies = new Map < string , DependencyData > ( ) ;
89
-
90
- const checkDependency = ( dependencyPath : string ) => {
91
- const packageJsonPath = path . join ( dependencyPath , 'package.json' ) ;
92
- const packageJson = require ( packageJsonPath ) ;
93
-
94
- if ( dependencies . has ( packageJson . name ) ) {
95
- const dependency = dependencies . get ( packageJson . name ) as DependencyData ;
96
-
97
- if (
98
- dependencyPath !== dependency . path &&
99
- dependency . duplicates ?. every (
100
- ( duplicate ) => duplicate . path !== dependencyPath ,
101
- )
102
- ) {
103
- dependencies . set ( packageJson . name , {
104
- ...dependency ,
105
- duplicates : [
106
- ...dependency . duplicates ,
107
- {
108
- path : dependencyPath ,
109
- version : packageJson . version ,
110
- peerDependencies : packageJson . peerDependencies ,
111
- } ,
112
- ] ,
113
- } ) ;
114
- }
115
- return ;
116
- }
117
-
118
- dependencies . set ( packageJson . name , {
119
- path : dependencyPath ,
120
- version : packageJson . version ,
121
- peerDependencies : packageJson . peerDependencies ,
122
- duplicates : [ ] ,
123
- } ) ;
124
-
125
- for ( const dependency in {
126
- ...packageJson . dependencies ,
127
- ...( root === dependencyPath ? packageJson . devDependencies : { } ) ,
128
- } ) {
129
- const depPath = findDependencyPath ( dependency , root , dependencyPath ) ;
130
-
131
- if ( depPath ) {
132
- checkDependency ( depPath ) ;
133
- }
134
- }
135
- } ;
136
-
137
- checkDependency ( root ) ;
138
-
139
- return dependencies ;
140
- }
141
-
142
51
export function filterNativeDependencies (
143
52
root : string ,
144
- dependencies : Map < string , DependencyData > ,
53
+ dependencies : DependencyMap ,
145
54
) {
146
55
const depsWithNativePeers = new Map < string , Map < string , string > > ( ) ;
147
56
@@ -198,15 +107,15 @@ export function filterInstalledPeers(
198
107
199
108
export function findPeerDepsToInstall (
200
109
root : string ,
201
- dependencies : Map < string , DependencyData > ,
110
+ dependencies : DependencyMap ,
202
111
) {
203
112
const rootPackageJson = require ( path . join ( root , 'package.json' ) ) ;
204
113
const dependencyList = {
205
114
...rootPackageJson . dependencies ,
206
115
...rootPackageJson . devDependencies ,
207
116
} ;
208
117
const peerDependencies = new Set < string > ( ) ;
209
- dependencies . forEach ( ( value ) => {
118
+ Array . from ( dependencies . entries ( ) ) . forEach ( ( [ _ , value ] ) => {
210
119
if ( value . peerDependencies ) {
211
120
Object . keys ( value . peerDependencies ) . forEach ( ( name ) => {
212
121
if ( ! Object . keys ( dependencyList ) . includes ( name ) ) {
@@ -218,16 +127,24 @@ export function findPeerDepsToInstall(
218
127
219
128
return peerDependencies ;
220
129
}
221
- export function getMissingPeerDepsForYarn ( root : string ) {
222
- const dependencies = collectDependencies ( root ) ;
130
+ export function getMissingPeerDepsForYarn (
131
+ root : string ,
132
+ dependencies : DependencyMap ,
133
+ ) {
223
134
const depsToInstall = findPeerDepsToInstall ( root , dependencies ) ;
224
-
225
135
return depsToInstall ;
226
136
}
227
137
228
138
// install peer deps with yarn without making any changes to package.json and yarn.lock
229
- export function yarnSilentInstallPeerDeps ( root : string ) {
230
- const dependenciesToInstall = getMissingPeerDepsForYarn ( root ) ;
139
+ export function yarnSilentInstallPeerDeps (
140
+ root : string ,
141
+ missingPeerDependencies : DependencyMap ,
142
+ ) {
143
+ const dependenciesToInstall = getMissingPeerDepsForYarn (
144
+ root ,
145
+ missingPeerDependencies ,
146
+ ) ;
147
+
231
148
const packageJsonPath = path . join ( root , 'package.json' ) ;
232
149
const lockfilePath = path . join ( root , 'yarn.lock' ) ;
233
150
@@ -259,19 +176,11 @@ export function yarnSilentInstallPeerDeps(root: string) {
259
176
return ;
260
177
}
261
178
262
- writeFile ( packageJsonPath , binPackageJson ) ;
263
- writeFile ( lockfilePath , binLockfile ) ;
179
+ fs . writeFileSync ( packageJsonPath , binPackageJson , { encoding : 'utf8' } ) ;
180
+ fs . writeFileSync ( lockfilePath , binLockfile , { encoding : 'utf8' } ) ;
264
181
}
265
182
}
266
183
267
- function findPeerDepsForAutolinking ( root : string ) {
268
- const deps = collectDependencies ( root ) ;
269
- const nonEmptyPeers = filterNativeDependencies ( root , deps ) ;
270
- const nonInstalledPeers = filterInstalledPeers ( root , nonEmptyPeers ) ;
271
-
272
- return nonInstalledPeers ;
273
- }
274
-
275
184
export async function promptForMissingPeerDependencies (
276
185
dependencies : Record < string , Record < string , string > > ,
277
186
) : Promise < boolean > {
@@ -302,7 +211,7 @@ export async function promptForMissingPeerDependencies(
302
211
return install ;
303
212
}
304
213
305
- async function getPackagesVersion (
214
+ export async function getPackagesVersion (
306
215
missingDependencies : Record < string , Record < string , string > > ,
307
216
) {
308
217
const packageToRanges : { [ pkg : string ] : string [ ] } = { } ;
@@ -339,7 +248,7 @@ async function getPackagesVersion(
339
248
return workingVersions ;
340
249
}
341
250
342
- function installMissingPackages (
251
+ export function installMissingPackages (
343
252
packages : Record < string , string | null > ,
344
253
yarn = true ,
345
254
) {
@@ -359,63 +268,37 @@ function installMissingPackages(
359
268
}
360
269
loader . succeed ( ) ;
361
270
362
- return true ;
271
+ return deps ;
363
272
} catch ( error ) {
364
273
loader . fail ( ) ;
365
274
366
- return false ;
275
+ return [ ] ;
367
276
}
368
277
}
369
278
370
- export async function resolveTransitiveDeps ( root : string ) {
371
- const isYarn = isUsingYarn ( root ) ;
279
+ export async function resolveTransitiveDeps (
280
+ root : string ,
281
+ dependencyMap : DependencyMap ,
282
+ ) {
283
+ const isYarn = ! ! isProjectUsingYarn ( root ) ;
284
+
372
285
if ( isYarn ) {
373
- yarnSilentInstallPeerDeps ( root ) ;
286
+ yarnSilentInstallPeerDeps ( root , dependencyMap ) ;
374
287
}
288
+ const nonEmptyPeers = filterNativeDependencies ( root , dependencyMap ) ;
289
+ const nonInstalledPeers = filterInstalledPeers ( root , nonEmptyPeers ) ;
375
290
376
- const missingPeerDependencies = findPeerDepsForAutolinking ( root ) ;
377
- if ( Object . keys ( missingPeerDependencies ) . length > 0 ) {
291
+ if ( Object . keys ( nonInstalledPeers ) . length > 0 ) {
378
292
const installDeps = await promptForMissingPeerDependencies (
379
- missingPeerDependencies ,
293
+ nonInstalledPeers ,
380
294
) ;
381
295
382
296
if ( installDeps ) {
383
- const packagesVersions = await getPackagesVersion (
384
- missingPeerDependencies ,
385
- ) ;
297
+ const packagesVersions = await getPackagesVersion ( nonInstalledPeers ) ;
386
298
387
299
return installMissingPackages ( packagesVersions , isYarn ) ;
388
300
}
389
301
}
390
302
391
- return false ;
392
- }
393
-
394
- async function resolvePodsInstallation ( ) {
395
- const { install} = await prompt ( {
396
- type : 'confirm' ,
397
- name : 'install' ,
398
- message :
399
- 'Do you want to install pods? This will make sure your transitive dependencies are linked properly.' ,
400
- } ) ;
401
-
402
- if ( install ) {
403
- await podInstall ( ) ;
404
- }
405
- }
406
-
407
- export default async function checkTransitiveDependencies ( ) {
408
- const root = process . cwd ( ) ;
409
- const packageJsonPath = path . join ( process . cwd ( ) , 'package.json' ) ;
410
- const preInstallHash = generateFileHash ( packageJsonPath ) ;
411
- const areTransitiveDepsInstalled = await resolveTransitiveDeps ( root ) ;
412
- const postInstallHash = generateFileHash ( packageJsonPath ) ;
413
-
414
- if (
415
- process . platform === 'darwin' &&
416
- areTransitiveDepsInstalled &&
417
- preInstallHash !== postInstallHash
418
- ) {
419
- await resolvePodsInstallation ( ) ;
420
- }
303
+ return [ ] ;
421
304
}
0 commit comments