utils.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* utils.coffee */
  2. (function() {
  3. var async, extend, fileExists, fileExistsSync, fs, path, pump, readJSON, readJSONSync, readdirRecursive, rfc822, stripExtension, util;
  4. util = require('util');
  5. fs = require('fs');
  6. path = require('path');
  7. async = require('async');
  8. fileExists = fs.exists || path.exists;
  9. fileExistsSync = fs.existsSync || path.existsSync;
  10. extend = function(obj, mixin) {
  11. var method, name;
  12. for (name in mixin) {
  13. method = mixin[name];
  14. obj[name] = method;
  15. }
  16. };
  17. stripExtension = function(filename) {
  18. /* Remove the file-extension from *filename* */
  19. return filename.replace(/(.+)\.[^.]+$/, '$1');
  20. };
  21. readJSON = function(filename, callback) {
  22. /* Read and try to parse *filename* as JSON, *callback* with parsed object or error on fault. */
  23. return async.waterfall([
  24. function(callback) {
  25. return fs.readFile(filename, callback);
  26. }, function(buffer, callback) {
  27. var error, rv;
  28. try {
  29. rv = JSON.parse(buffer.toString());
  30. return callback(null, rv);
  31. } catch (error1) {
  32. error = error1;
  33. error.filename = filename;
  34. error.message = "parsing " + (path.basename(filename)) + ": " + error.message;
  35. return callback(error);
  36. }
  37. }
  38. ], callback);
  39. };
  40. readJSONSync = function(filename) {
  41. /* Synchronously read and try to parse *filename* as json. */
  42. var buffer;
  43. buffer = fs.readFileSync(filename);
  44. return JSON.parse(buffer.toString());
  45. };
  46. readdirRecursive = function(directory, callback) {
  47. /* Returns an array representing *directory*, including subdirectories. */
  48. var result, walk;
  49. result = [];
  50. walk = function(dir, callback) {
  51. return async.waterfall([
  52. async.apply(fs.readdir, path.join(directory, dir)), function(filenames, callback) {
  53. return async.forEach(filenames, function(filename, callback) {
  54. var relname;
  55. relname = path.join(dir, filename);
  56. return async.waterfall([
  57. async.apply(fs.stat, path.join(directory, relname)), function(stat, callback) {
  58. if (stat.isDirectory()) {
  59. return walk(relname, callback);
  60. } else {
  61. result.push(relname);
  62. return callback();
  63. }
  64. }
  65. ], callback);
  66. }, callback);
  67. }
  68. ], callback);
  69. };
  70. return walk('', function(error) {
  71. return callback(error, result);
  72. });
  73. };
  74. pump = function(source, destination, callback) {
  75. /* Pipe *source* stream to *destination* stream calling *callback* when done */
  76. source.pipe(destination);
  77. source.on('error', function(error) {
  78. if (typeof callback === "function") {
  79. callback(error);
  80. }
  81. return callback = null;
  82. });
  83. return destination.on('finish', function() {
  84. if (typeof callback === "function") {
  85. callback();
  86. }
  87. return callback = null;
  88. });
  89. };
  90. rfc822 = function(date) {
  91. /* return a rfc822 representation of a javascript Date object
  92. http://www.w3.org/Protocols/rfc822/#z28
  93. */
  94. var days, months, pad, time, tzoffset;
  95. pad = function(i) {
  96. if (i < 10) {
  97. return '0' + i;
  98. } else {
  99. return i;
  100. }
  101. };
  102. tzoffset = function(offset) {
  103. var direction, hours, minutes;
  104. hours = Math.floor(offset / 60);
  105. minutes = Math.abs(offset % 60);
  106. direction = hours > 0 ? '-' : '+';
  107. return direction + pad(Math.abs(hours)) + pad(minutes);
  108. };
  109. months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', ' Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
  110. days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
  111. time = [pad(date.getHours()), pad(date.getMinutes()), pad(date.getSeconds())].join(':');
  112. return [days[date.getDay()] + ',', pad(date.getDate()), months[date.getMonth()], date.getFullYear(), time, tzoffset(date.getTimezoneOffset())].join(' ');
  113. };
  114. /* Exports */
  115. module.exports = {
  116. fileExists: fileExists,
  117. fileExistsSync: fileExistsSync,
  118. extend: extend,
  119. stripExtension: stripExtension,
  120. readJSON: readJSON,
  121. readJSONSync: readJSONSync,
  122. readdirRecursive: readdirRecursive,
  123. pump: pump,
  124. rfc822: rfc822
  125. };
  126. }).call(this);