|
| 1 | +// @remove-on-eject-begin |
| 2 | +/** |
| 3 | + * Copyright (c) 2015-present, Facebook, Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | +// @remove-on-eject-end |
| 9 | +'use strict'; |
| 10 | + |
| 11 | +const path = require('path'); |
| 12 | +const fs = require('fs'); |
| 13 | + |
| 14 | +const globbySync = require('globby').sync; |
| 15 | +const loadJsonFileSync = require('load-json-file').sync; |
| 16 | +const ValidationError = require('@lerna/validation-error'); |
| 17 | +const Package = require('@lerna/package'); |
| 18 | +const PackageGraph = require('@lerna/package-graph'); |
| 19 | +const Project = require('@lerna/project'); |
| 20 | + |
| 21 | +function flattenResults(results) { |
| 22 | + return results.reduce((acc, result) => acc.concat(result), []); |
| 23 | +} |
| 24 | + |
| 25 | +// Sync version of Lerna's makeFileFinder |
| 26 | +// Heavily inspired by https://github.com/lerna/lerna/blob/62843b04e3a5a03012ceabe465519b39a09fbcc1/core/project/lib/make-file-finder.js |
| 27 | +function makeFileFinderSync(rootPath, packageConfigs) { |
| 28 | + const globOpts = { |
| 29 | + cwd: rootPath, |
| 30 | + absolute: true, |
| 31 | + followSymlinkedDirectories: false, |
| 32 | + // POSIX results always need to be normalized |
| 33 | + transform: fp => path.normalize(fp), |
| 34 | + }; |
| 35 | + |
| 36 | + if (packageConfigs.some(cfg => cfg.indexOf('**') > -1)) { |
| 37 | + if (packageConfigs.some(cfg => cfg.indexOf('node_modules') > -1)) { |
| 38 | + throw new ValidationError( |
| 39 | + 'EPKGCONFIG', |
| 40 | + 'An explicit node_modules package path does not allow globstars (**)' |
| 41 | + ); |
| 42 | + } |
| 43 | + |
| 44 | + globOpts.ignore = [ |
| 45 | + // allow globs like "packages/**", |
| 46 | + // but avoid picking up node_modules/**/package.json |
| 47 | + '**/node_modules/**', |
| 48 | + ]; |
| 49 | + } |
| 50 | + |
| 51 | + return (fileName, fileMapper, customGlobOpts) => { |
| 52 | + const options = Object.assign({}, customGlobOpts, globOpts); |
| 53 | + const packages = packageConfigs.sort().map(globPath => { |
| 54 | + const results = globbySync(path.join(globPath, fileName), options).sort(); |
| 55 | + |
| 56 | + if (fileMapper) { |
| 57 | + return fileMapper(results); |
| 58 | + } |
| 59 | + |
| 60 | + return results; |
| 61 | + }); |
| 62 | + |
| 63 | + // always flatten the results |
| 64 | + return flattenResults(packages); |
| 65 | + }; |
| 66 | +} |
| 67 | + |
| 68 | +function getPackagesSync(project) { |
| 69 | + const mapper = packageConfigPath => { |
| 70 | + const packageJson = loadJsonFileSync(packageConfigPath); |
| 71 | + return new Package( |
| 72 | + packageJson, |
| 73 | + path.dirname(packageConfigPath), |
| 74 | + project.rootPath |
| 75 | + ); |
| 76 | + }; |
| 77 | + |
| 78 | + const finder = makeFileFinderSync(project.rootPath, project.packageConfigs); |
| 79 | + |
| 80 | + return finder('package.json', filePaths => filePaths.map(mapper)); |
| 81 | +} |
| 82 | + |
| 83 | +module.exports.getAllLocalDependencies = function getAllLocalDependencies( |
| 84 | + appName |
| 85 | +) { |
| 86 | + const project = new Project(process.cwd()); |
| 87 | + const packages = getPackagesSync(project); |
| 88 | + const packageGraph = new PackageGraph( |
| 89 | + packages, |
| 90 | + 'allDependencies', |
| 91 | + 'forceLocal' |
| 92 | + ); |
| 93 | + const currentNode = packageGraph.get(appName); |
| 94 | + if (!currentNode) { |
| 95 | + return undefined; |
| 96 | + } |
| 97 | + |
| 98 | + const dependencies = new Set(currentNode.localDependencies.keys()); |
| 99 | + const dependenciesToExplore = new Set(dependencies); |
| 100 | + dependenciesToExplore.delete(appName); |
| 101 | + |
| 102 | + while (dependenciesToExplore.size > 0) { |
| 103 | + const packageName = dependenciesToExplore.values().next().value; |
| 104 | + dependenciesToExplore.delete(packageName); |
| 105 | + const node = packageGraph.get(packageName); |
| 106 | + const newDependencies = new Set(node.localDependencies.keys()); |
| 107 | + newDependencies.delete(appName); |
| 108 | + |
| 109 | + for (const d of newDependencies.values()) { |
| 110 | + if (!dependencies.has(d)) { |
| 111 | + dependenciesToExplore.add(d); |
| 112 | + dependencies.add(d); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + const additionalSrcPaths = Array.from(dependencies).map(dependencyName => { |
| 118 | + const resolvedPath = fs.realpathSync( |
| 119 | + packageGraph.get(dependencyName).location |
| 120 | + ); |
| 121 | + return path.resolve(resolvedPath, 'src'); |
| 122 | + }); |
| 123 | + return additionalSrcPaths; |
| 124 | +}; |
0 commit comments