|
1 | 1 | const path = require('path'); |
2 | 2 |
|
| 3 | +const commonAppDir = path.resolve(__dirname, '..'); |
| 4 | + |
| 5 | +// Resolve where a dependency actually lives from an app's perspective - its own |
| 6 | +// node_modules, or the monorepo root once yarn hoists it. Using `require.resolve` |
| 7 | +// is hoist-proof, so an app that pins a divergent major (e.g. gesture-handler v3 |
| 8 | +// in the fabric example) autolinks that version rather than a guessed path. |
| 9 | +function getRootPath(moduleName, currentAppDir) { |
| 10 | + try { |
| 11 | + return path.dirname( |
| 12 | + require.resolve(`${moduleName}/package.json`, { |
| 13 | + paths: [currentAppDir, commonAppDir] |
| 14 | + }) |
| 15 | + ); |
| 16 | + } catch { |
| 17 | + return path.resolve(currentAppDir, `../../node_modules/${moduleName}`); |
| 18 | + } |
| 19 | +} |
| 20 | + |
3 | 21 | function getDependencies(currentAppDir = '.', excludeCommon = []) { |
4 | | - const commonAppDir = path.resolve(__dirname, '..'); |
5 | 22 | const commonAppPkg = require(path.resolve(commonAppDir, 'package.json')); |
6 | 23 | const currentAppPkg = require(path.resolve(currentAppDir, 'package.json')); |
7 | 24 |
|
8 | | - const excludedCommonDeps = new Set(excludeCommon); |
9 | | - |
10 | | - const allDeps = new Set([ |
11 | | - ...[ |
12 | | - ...Object.keys(commonAppPkg.dependencies ?? {}), |
13 | | - ...Object.keys(commonAppPkg.devDependencies ?? {}) |
14 | | - ].filter(dep => !excludedCommonDeps.has(dep)), |
| 25 | + const excluded = new Set(excludeCommon); |
| 26 | + const names = [ |
| 27 | + ...Object.keys(commonAppPkg.dependencies ?? {}), |
| 28 | + ...Object.keys(commonAppPkg.devDependencies ?? {}), |
15 | 29 | ...Object.keys(currentAppPkg.dependencies ?? {}), |
16 | 30 | ...Object.keys(currentAppPkg.devDependencies ?? {}) |
17 | | - ]); |
| 31 | + ].filter(name => !excluded.has(name)); |
18 | 32 |
|
19 | 33 | const result = {}; |
20 | | - |
21 | | - for (const dep of allDeps) { |
22 | | - // Find versions in both package.json files |
23 | | - const commonVersion = |
24 | | - commonAppPkg.dependencies?.[dep] ?? commonAppPkg.devDependencies?.[dep]; |
25 | | - const currentVersion = |
26 | | - currentAppPkg.dependencies?.[dep] ?? currentAppPkg.devDependencies?.[dep]; |
27 | | - |
28 | | - if (!commonVersion || !currentVersion || commonVersion === currentVersion) { |
29 | | - result[dep] = { |
30 | | - root: path.resolve(currentAppDir, `../../node_modules/${dep}`) |
31 | | - }; |
32 | | - } else { |
33 | | - // Include from the local node_modules only if the dependency is present |
34 | | - // in the current app and versions are different |
35 | | - result[dep] = { |
36 | | - root: path.resolve(currentAppDir, `node_modules/${dep}`) |
37 | | - }; |
38 | | - } |
| 34 | + for (const name of new Set(names)) { |
| 35 | + result[name] = { root: getRootPath(name, currentAppDir) }; |
39 | 36 | } |
40 | 37 |
|
41 | 38 | return result; |
|
0 commit comments