Created
April 9, 2014 07:43
-
-
Save shepherdwind/10237220 to your computer and use it in GitHub Desktop.
getScreenFile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node --harmony | |
var fs = require('fs') | |
var path = require('path') | |
var co = require('co') | |
var thunkify = require('thunkify') | |
var cwd = process.cwd() | |
var file = path.join(cwd, 'templates') | |
var dir = thunkify(fs.readdir) | |
var stat = thunkify(fs.stat) | |
function getScreenFile(file){ | |
co(function*(){ | |
var dirs = yield dir(file) | |
for (var i = 0; i < dirs.length; i++) { | |
var filename = path.join(file, dirs[i]) | |
var filestat = yield stat(filename) | |
if (filestat.isDirectory()) { | |
// 如果是文件夹,并且不是隐藏文件,并且不是tile目录的情况下,递归查找 | |
if(filename.indexOf('.') !== 0 && filename.indexOf('tile') === -1) { | |
getScreenFile(filename) | |
} | |
// 找到所有文件名中又screen的文件 | |
} else if (filename.indexOf('screen') > -1) { | |
console.log(filename) | |
} | |
} | |
})() | |
} | |
getScreenFile(file) | |
/** | |
* vim: ft=javascript:tw=80: | |
* requires: co, thunkify | |
*/ |
嗯,find是要容易一些,头晕了,不记得当时怎么想的。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
一个问题,关于generator。遍历一个目录下所有子目录的文件,找到符合某种规则的文件。使用generator方式,co + thunkify,不过问题是,那些文件我想放到一个数组里面,应该怎么做,是异步操作的,不知道什么时候所有行为结束了