我正在使用一个大型 monorepo,并且我正在尝试编写一个脚本,该脚本需要从所有 monorepo 的 package.json 文件中获取一些信息,但 not 和嵌套在任何中的 package.json 文件
node_modules
文件夹中。除了在递归遍历整个目录(包括 node_modules
文件夹)后使用正则表达式过滤它们之外,我已经尝试了所有方法。我知道这是一个选项,但理想情况下,出于性能原因,我希望能够在搜索之前过滤这些目录。 monorepo 结构看起来像这样:
root/
node_modules/
apps/
someApp/
node_modules/
someApp2/
node_modules/
packages/
somePackage1/
node_modules/
somePackage2/
node_modules/
somePackage3/
node_modules/
...
任何帮助将不胜感激! 谢谢。
我会浏览整个文件树并跳过所有内容
node_modules
。
这比搜索所有
package.json
并按其路径过滤它们要高效得多。
from os import walk
w = walk("/path/to/your/repo")
for(dir_path, dir_names, file_names) in w:
if dir_path.endswith('node_modules'):
continue
for file in file_names:
if file == 'package.json':
yield os.path.join(dir_path, file)