glob在Node.js中只返回匹配(无前导path)
我需要glob ./../path/to/files/**/*.txt
而不是像这样接收匹配:
./../path/to/files/subdir/file.txt
我需要删除根目录:
subdir/file.txt
目前,我有:
oldwd = process.cwd() process.chdir(__dirname + "/../path/to/files") glob.glob("**/*.txt", function (err, matches) { process.chdir(oldwd) });
但是这有点难看,似乎也有一个竞争条件:有时候这个环球会发生在老年人身上。 所以这一定要去。
我正在考虑简单地映射matches
和剥离string操作的主要path。 由于glob返回匹配解决的dotdirs,我将不得不做同样的我的search和replacestring,我想。 这只是混乱,我不知道是否有一个更好的(内置或库?)方式来处理这个问题。
那么,在Node.js中完成一个漂亮的,整齐的和正确的方法,并且只是获得“匹配”部分? JavaScript和CoffeeScript都可以
尝试这个:
var path = require('path'); var root = '/path/to/files'; glob.glob("**/*.txt", function(err, matches) { if(err) throw err; matches = matches.map(function(match) { return path.relative(root, match); }); // use matches });
将目录传递给选项,并将所有这些混乱由glob处理。
glob.glob("**/*.txt", {cwd: '../../wherever/'}, function(err, matches) { ... });