我需要使用node.js获取名称高于1.0.0
的所有文件夹
如何使用下面的结构实现这一点。
|---version
|--1.0.0
|--1.2.0
|--0.9.0
谢谢,我对 Node 很陌生。
最佳答案
如果目录的名称是有效的 semver 字符串(如您的),最简单的方法是使用 semver模块并使用 gt
函数。像这样的事情:
var greater = function (dir, cb){
var max = null;
fs.readdir (dir, function (error, entries){
if (error) return cb (error);
if (!entries.length) return cb ();
entries.forEach (function (entry){
//Suppose there're no files in the directory
if (!max) return max = entry;
if (semver.gt (entry, max)) max = entry;
});
cb (null, max)
});
};
greater ("dir", function (error, dir){
if (error) return handleError (error);
if (dir){
//dir is the greater
}else{
//No directories
}
});
关于javascript - 使用 Node 搜索高于 X 的文件夹名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21233316/