All files findNativeDependencies.js

0% Statements 0/20
0% Branches 0/15
0% Functions 0/3
0% Lines 0/20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58                                                                                                                   
// @flow
 
import Dependency from './Dependency'
import readDir from 'fs-readdir-recursive'
import _ from 'lodash'
import path from 'path'
 
const NPM_SCOPED_MODULE_RE = /@(.*)\/(.*)/
const API_PATH_RE = /^win/.test(process.platform) ? /react-native-.+-api\\/ : /react-native-.+-api\//
 
export default function findNativeDependencies (p: string) : Array<Dependency> {
  let result = []
 
  const nativeDependenciesNames = new Set()
 
  const nodeModulesDirectoriesWithNativeCode = readDir(p)
          .filter(a =>
            a.includes('build.gradle') ||
            a.includes('.pbxproj') ||
            API_PATH_RE.test(a))
 
  // By convention we only assume react native plugins to be in directories
  // which names are starting with 'react-native' (excluding scope)
  const nativeDepsDirectories = _.filter(nodeModulesDirectoriesWithNativeCode,
          d => d.includes('react-native') && !/sample|demo|example/i.test(d))
 
  for (const nativeDepsDirectory of nativeDepsDirectories) {
    const pathSegments = nativeDepsDirectory.split(path.sep)
    if (pathSegments[0].startsWith('@')) {
      nativeDependenciesNames.add(`${pathSegments[0]}/${pathSegments[1]}`)
    } else {
      nativeDependenciesNames.add(pathSegments[0])
    }
  }
 
  // Get associated versions
  for (const nativeDependencyName of nativeDependenciesNames) {
    const pathToNativeDependencyPackageJson = path.join(p, nativeDependencyName, 'package.json')
    const nativeDepPackageJson = require(pathToNativeDependencyPackageJson)
    if (NPM_SCOPED_MODULE_RE.test(nativeDependencyName)) {
      result.push(new Dependency(NPM_SCOPED_MODULE_RE.exec(nativeDependencyName)[2], {
        scope: NPM_SCOPED_MODULE_RE.exec(nativeDependencyName)[1],
        version: nativeDepPackageJson.version.startsWith('v')
                            ? nativeDepPackageJson.version.slice(1)
                            : nativeDepPackageJson.version
      }))
    } else {
      result.push(new Dependency(nativeDependencyName, {
        version: nativeDepPackageJson.version.startsWith('v')
                            ? nativeDepPackageJson.version.slice(1)
                            : nativeDepPackageJson.version
      }))
    }
  }
 
  return result
}