fsutil.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. /**
  2. * Created with JetBrains WebStorm.
  3. * User: Hui Xie
  4. * Date: 13-8-30
  5. * Time: 下午4:54
  6. * To change this template use File | Settings | File Templates.
  7. */
  8. var fs = require('fs'),
  9. path = require('path');
  10. module.exports = function () {
  11. var exports = {};
  12. var win32 = process.platform === 'win32';
  13. // Normalize \\ paths to / paths.
  14. unixifyPath = function (filepath) {
  15. if (win32) {
  16. return filepath.replace(/\\/g, '/');
  17. } else {
  18. return filepath;
  19. }
  20. };
  21. exports.recurse = function recurse(rootdir, callback, subdir) {
  22. var abspath = subdir ? path.join(rootdir, subdir) : rootdir;
  23. fs.readdirSync(abspath).forEach(function(filename) {
  24. var filepath = path.join(abspath, filename);
  25. if (fs.statSync(filepath).isDirectory()) {
  26. recurse(rootdir, callback, unixifyPath(path.join(subdir || '', filename || '')));
  27. } else {
  28. callback(unixifyPath(filepath), rootdir, subdir, filename);
  29. }
  30. });
  31. };
  32. return exports;
  33. };