mirror of
https://github.com/status-im/react-native.git
synced 2025-01-10 17:45:59 +00:00
bce6ece5f6
Summary: Support symlinks under `node_modules` for all local-cli commands. PR https://github.com/facebook/react-native/pull/9009 only adds symlink support to the packager. But other cli commands like `react-native bundle` creates its own instance of packager that doesn't have symlinks as part of its project roots, which results in the bundler breaking since it cannot find modules that you have symlinked. This change ensures all `local-cli` commands add symlinks to its project roots. Test plan (required) 1. Create a symlink in node_modules (for instance use npm/yarn link) 2. Run `react-native bundle`. Closes https://github.com/facebook/react-native/pull/11810 Differential Revision: D4487741 fbshipit-source-id: 87fe44194134d086dca4eaca99ee5742d6eadb69
51 lines
1.3 KiB
JavaScript
51 lines
1.3 KiB
JavaScript
const path = require('path');
|
|
const fs = require('fs');
|
|
|
|
/**
|
|
* Find and resolve symlinks in `lookupFolder`.
|
|
* Ignore any descendants of the paths in `ignoredRoots`.
|
|
*/
|
|
module.exports = function findSymlinksPaths(lookupFolder, ignoredRoots) {
|
|
const timeStart = Date.now();
|
|
const folders = fs.readdirSync(lookupFolder);
|
|
|
|
const resolvedSymlinks = [];
|
|
folders.forEach(folder => {
|
|
const visited = [];
|
|
|
|
let symlink = path.resolve(lookupFolder, folder);
|
|
while (fs.lstatSync(symlink).isSymbolicLink()) {
|
|
const index = visited.indexOf(symlink);
|
|
if (index !== -1) {
|
|
throw Error(
|
|
`Infinite symlink recursion detected:\n ` +
|
|
visited.slice(index).join(`\n `)
|
|
);
|
|
}
|
|
|
|
visited.push(symlink);
|
|
symlink = path.resolve(
|
|
path.dirname(symlink),
|
|
fs.readlinkSync(symlink)
|
|
);
|
|
}
|
|
|
|
if (visited.length && !rootExists(ignoredRoots, symlink)) {
|
|
resolvedSymlinks.push(symlink);
|
|
}
|
|
});
|
|
|
|
const timeEnd = Date.now();
|
|
console.log(`Scanning ${folders.length} folders for symlinks in ${lookupFolder} (${timeEnd - timeStart}ms)`);
|
|
|
|
return resolvedSymlinks;
|
|
};
|
|
|
|
function rootExists(roots, child) {
|
|
return roots.some(root => isDescendant(root, child));
|
|
}
|
|
|
|
function isDescendant(root, child) {
|
|
return root === child || child.startsWith(root + path.sep);
|
|
}
|