节点模块,用于查找具有多个可能根的文件path
是否有一个节点模块,它允许您指定多个根,给它一个文件,并试图find该文件?
知道很多库/模块做这样的事情,但我的谷歌福没有为它打开一个现有的模块,这是有点令人惊讶。
澄清,基本上如果我有这样的configuration:
roots: ['node_modules', 'bower_components', 'src']
我问它find“my-file.js”,它会search以下内容:
node_modules/my-file.js bower_components/my-file.js src/my-file.js
并返回parsing到现有文件的第一个,或返回一个错误,说它无法解决它。
我也不知道任何图书馆,但你可以做几行代码:
const fs = require('fs'); const path = require('path'); const roots = ['node_modules', 'bower_components', 'src']; const myFile = 'my-file.js'; const result = roots.map(root => path.resolve(path.join(root,myFile))).map(fs.existsSync);
这会给你一个对应于roots
每个元素的真/假值的数组。 所以如果src/my-file.js
node_modules/my-file.js
和node_modules/my-file.js
存在,你会得到result
为[true, false, true]