You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

823 lines
28 KiB

  1. // Generated by CoffeeScript 1.4.0
  2. (function() {
  3. var balUtilFlow, balUtilPaths, balUtilTypes, fsUtil, pathUtil, _ref, _ref1, _ref2, _ref3, _ref4, _ref5,
  4. __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; },
  5. __slice = [].slice,
  6. __hasProp = {}.hasOwnProperty;
  7. fsUtil = require('fs');
  8. pathUtil = require('path');
  9. balUtilFlow = require(__dirname + '/flow');
  10. balUtilTypes = require(__dirname + '/types');
  11. if ((_ref = global.numberOfOpenFiles) == null) {
  12. global.numberOfOpenFiles = 0;
  13. }
  14. if ((_ref1 = global.maxNumberOfOpenFiles) == null) {
  15. global.maxNumberOfOpenFiles = (_ref2 = process.env.NODE_MAX_OPEN_FILES) != null ? _ref2 : 100;
  16. }
  17. if ((_ref3 = global.waitingToOpenFileDelay) == null) {
  18. global.waitingToOpenFileDelay = 100;
  19. }
  20. balUtilPaths = {
  21. ignoreCommonPatterns: (_ref4 = process.env.NODE_IGNORE_COMMON_PATTERNS) != null ? _ref4 : /^((~|\.\#).*|.*(\.swp)|\.(svn|git|hg|DS_Store)|node_modules|CVS|thumbs\.db|desktop\.ini)$/i,
  22. ignoreCustomPatterns: (_ref5 = process.env.NODE_IGNORE_CUSTOM_PATTERNS) != null ? _ref5 : null,
  23. textExtensions: ['c', 'coffee', 'coffeekup', 'cson', 'css', 'eco', 'haml', 'hbs', 'htaccess', 'htm', 'html', 'jade', 'js', 'json', 'less', 'md', 'php', 'phtml', 'py', 'rb', 'rtf', 'sass', 'scss', 'styl', 'stylus', 'text', 'txt', 'xml', 'yaml'].concat((process.env.TEXT_EXTENSIONS || '').split(/[\s,]+/)),
  24. binaryExtensions: ['dds', 'eot', 'gif', 'ico', 'jar', 'jpeg', 'jpg', 'pdf', 'png', 'swf', 'tga', 'ttf', 'zip'].concat((process.env.BINARY_EXTENSIONS || '').split(/[\s,]+/)),
  25. openFile: function(next) {
  26. if (global.numberOfOpenFiles < 0) {
  27. throw new Error("balUtilPaths.openFile: the numberOfOpenFiles is [" + global.numberOfOpenFiles + "] which should be impossible...");
  28. }
  29. if (global.numberOfOpenFiles >= global.maxNumberOfOpenFiles) {
  30. setTimeout(function() {
  31. return balUtilPaths.openFile(next);
  32. }, global.waitingToOpenFileDelay);
  33. } else {
  34. ++global.numberOfOpenFiles;
  35. next();
  36. }
  37. return this;
  38. },
  39. closeFile: function(next) {
  40. --global.numberOfOpenFiles;
  41. if (typeof next === "function") {
  42. next();
  43. }
  44. return this;
  45. },
  46. readFile: function(path, encoding, next) {
  47. if (next == null) {
  48. next = encoding;
  49. encoding = null;
  50. }
  51. balUtilPaths.openFile(function() {
  52. return fsUtil.readFile(path, encoding, function(err, data) {
  53. balUtilPaths.closeFile();
  54. return next(err, data);
  55. });
  56. });
  57. return this;
  58. },
  59. writeFile: function(path, data, encoding, next) {
  60. if (next == null) {
  61. next = encoding;
  62. encoding = null;
  63. }
  64. balUtilPaths.ensurePath(pathUtil.dirname(path), function(err) {
  65. if (err) {
  66. return next(err);
  67. }
  68. return balUtilPaths.openFile(function() {
  69. return fsUtil.writeFile(path, data, encoding, function(err) {
  70. balUtilPaths.closeFile();
  71. return next(err);
  72. });
  73. });
  74. });
  75. return this;
  76. },
  77. mkdir: function(path, mode, next) {
  78. if (next == null) {
  79. next = mode;
  80. mode = null;
  81. }
  82. balUtilPaths.openFile(function() {
  83. return fsUtil.mkdir(path, mode, function(err) {
  84. balUtilPaths.closeFile();
  85. return next(err);
  86. });
  87. });
  88. return this;
  89. },
  90. stat: function(path, next) {
  91. balUtilPaths.openFile(function() {
  92. return fsUtil.stat(path, function(err, stat) {
  93. balUtilPaths.closeFile();
  94. return next(err, stat);
  95. });
  96. });
  97. return this;
  98. },
  99. readdir: function(path, next) {
  100. balUtilPaths.openFile(function() {
  101. return fsUtil.readdir(path, function(err, files) {
  102. balUtilPaths.closeFile();
  103. return next(err, files);
  104. });
  105. });
  106. return this;
  107. },
  108. unlink: function(path, next) {
  109. balUtilPaths.openFile(function() {
  110. return fsUtil.unlink(path, function(err) {
  111. balUtilPaths.closeFile();
  112. return next(err);
  113. });
  114. });
  115. return this;
  116. },
  117. rmdir: function(path, next) {
  118. balUtilPaths.openFile(function() {
  119. return fsUtil.rmdir(path, function(err) {
  120. balUtilPaths.closeFile();
  121. return next(err);
  122. });
  123. });
  124. return this;
  125. },
  126. exists: function(path, next) {
  127. var exists;
  128. exists = fsUtil.exists || pathUtil.exists;
  129. balUtilPaths.openFile(function() {
  130. return exists(path, function(exists) {
  131. balUtilPaths.closeFile();
  132. return next(exists);
  133. });
  134. });
  135. return this;
  136. },
  137. existsSync: function(path) {
  138. var existsSync, result;
  139. existsSync = fsUtil.existsSync || pathUtil.existsSync;
  140. result = existsSync(path);
  141. return result;
  142. },
  143. isTextSync: function(filename, buffer) {
  144. var extension, isText, _i, _len;
  145. isText = null;
  146. if (filename) {
  147. filename = pathUtil.basename(filename).split('.');
  148. for (_i = 0, _len = filename.length; _i < _len; _i++) {
  149. extension = filename[_i];
  150. if (__indexOf.call(balUtilPaths.textExtensions, extension) >= 0) {
  151. isText = true;
  152. break;
  153. }
  154. if (__indexOf.call(balUtilPaths.binaryExtensions, extension) >= 0) {
  155. isText = false;
  156. break;
  157. }
  158. }
  159. }
  160. if (buffer && isText === null) {
  161. isText = balUtilPaths.getEncodingSync(buffer) === 'utf8';
  162. }
  163. return isText;
  164. },
  165. isText: function(filename, buffer, next) {
  166. var result;
  167. result = this.isTextSync(filename, buffer);
  168. if (result instanceof Error) {
  169. next(err);
  170. } else {
  171. next(null, result);
  172. }
  173. return this;
  174. },
  175. getEncodingSync: function(buffer, opts) {
  176. var binaryEncoding, charCode, chunkBegin, chunkEnd, chunkLength, contentChunkUTF8, encoding, i, textEncoding, _i, _ref6;
  177. textEncoding = 'utf8';
  178. binaryEncoding = 'binary';
  179. if (opts == null) {
  180. chunkLength = 24;
  181. encoding = balUtilPaths.getEncodingSync(buffer, {
  182. chunkLength: chunkLength,
  183. chunkBegin: chunkBegin
  184. });
  185. if (encoding === textEncoding) {
  186. chunkBegin = Math.max(0, Math.floor(buffer.length / 2) - chunkLength);
  187. encoding = balUtilPaths.getEncodingSync(buffer, {
  188. chunkLength: chunkLength,
  189. chunkBegin: chunkBegin
  190. });
  191. if (encoding === textEncoding) {
  192. chunkBegin = Math.max(0, buffer.length - chunkLength);
  193. encoding = balUtilPaths.getEncodingSync(buffer, {
  194. chunkLength: chunkLength,
  195. chunkBegin: chunkBegin
  196. });
  197. }
  198. }
  199. } else {
  200. chunkLength = opts.chunkLength, chunkBegin = opts.chunkBegin;
  201. if (chunkLength == null) {
  202. chunkLength = 24;
  203. }
  204. if (chunkBegin == null) {
  205. chunkBegin = 0;
  206. }
  207. chunkEnd = Math.min(buffer.length, chunkBegin + chunkLength);
  208. contentChunkUTF8 = buffer.toString(textEncoding, chunkBegin, chunkEnd);
  209. encoding = textEncoding;
  210. for (i = _i = 0, _ref6 = contentChunkUTF8.length; 0 <= _ref6 ? _i < _ref6 : _i > _ref6; i = 0 <= _ref6 ? ++_i : --_i) {
  211. charCode = contentChunkUTF8.charCodeAt(i);
  212. if (charCode === 65533 || charCode <= 8) {
  213. encoding = binaryEncoding;
  214. break;
  215. }
  216. }
  217. }
  218. return encoding;
  219. },
  220. getEncoding: function(buffer, opts, next) {
  221. var result;
  222. result = this.getEncodingSync(buffer, opts);
  223. if (result instanceof Error) {
  224. next(err);
  225. } else {
  226. next(null, result);
  227. }
  228. return this;
  229. },
  230. cp: function(src, dst, next) {
  231. balUtilPaths.readFile(src, 'binary', function(err, data) {
  232. if (err) {
  233. console.log("balUtilPaths.cp: cp failed on: " + src);
  234. return next(err);
  235. }
  236. return balUtilPaths.writeFile(dst, data, 'binary', function(err) {
  237. if (err) {
  238. console.log("balUtilPaths.cp: writeFile failed on: " + dst);
  239. }
  240. return next(err);
  241. });
  242. });
  243. return this;
  244. },
  245. getParentPathSync: function(p) {
  246. var parentPath;
  247. parentPath = p.replace(/[\/\\][^\/\\]+$/, '');
  248. return parentPath;
  249. },
  250. ensurePath: function(path, next) {
  251. path = path.replace(/[\/\\]$/, '');
  252. balUtilPaths.exists(path, function(exists) {
  253. var parentPath;
  254. if (exists) {
  255. return next(null, true);
  256. }
  257. parentPath = balUtilPaths.getParentPathSync(path);
  258. return balUtilPaths.ensurePath(parentPath, function(err) {
  259. if (err) {
  260. console.log("balUtilPaths.ensurePath: failed to ensure the path: " + parentPath);
  261. return next(err, false);
  262. }
  263. return balUtilPaths.mkdir(path, '700', function(err) {
  264. return balUtilPaths.exists(path, function(exists) {
  265. if (!exists) {
  266. console.log("balUtilPaths.ensurePath: failed to create the directory: " + path);
  267. return next(new Error("Failed to create the directory: " + path));
  268. }
  269. return next(null, false);
  270. });
  271. });
  272. });
  273. });
  274. return this;
  275. },
  276. prefixPathSync: function(path, parentPath) {
  277. path = path.replace(/[\/\\]$/, '');
  278. if (/^([a-zA-Z]\:|\/)/.test(path) === false) {
  279. path = pathUtil.join(parentPath, path);
  280. }
  281. return path;
  282. },
  283. isDirectory: function(path, next) {
  284. if ((path != null ? path.isDirectory : void 0) != null) {
  285. return next(null, path.isDirectory(), path);
  286. } else {
  287. balUtilPaths.stat(path, function(err, stat) {
  288. if (err) {
  289. console.log("balUtilPaths.isDirectory: stat failed on: " + path);
  290. return next(err);
  291. }
  292. return next(null, stat.isDirectory(), stat);
  293. });
  294. }
  295. return this;
  296. },
  297. generateSlugSync: function(path) {
  298. var result;
  299. result = path.replace(/[^a-zA-Z0-9]/g, '-').replace(/^-/, '').replace(/-+/, '-');
  300. return result;
  301. },
  302. scanlist: function(path, next) {
  303. balUtilPaths.scandir({
  304. path: path,
  305. readFiles: true,
  306. ignoreHiddenFiles: true,
  307. next: function(err, list) {
  308. return next(err, list);
  309. }
  310. });
  311. return this;
  312. },
  313. scantree: function(path, next) {
  314. balUtilPaths.scandir({
  315. path: path,
  316. readFiles: true,
  317. ignoreHiddenFiles: true,
  318. next: function(err, list, tree) {
  319. return next(err, tree);
  320. }
  321. });
  322. return this;
  323. },
  324. testIgnorePatterns: function() {
  325. var args;
  326. args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  327. return this.isIgnoredPath.apply(this, args);
  328. },
  329. isIgnoredPath: function(path, opts) {
  330. var basename, ignorePath, result, _i, _len, _ref10, _ref6, _ref7, _ref8, _ref9;
  331. if (opts == null) {
  332. opts = {};
  333. }
  334. result = false;
  335. basename = pathUtil.basename(path);
  336. if ((_ref6 = opts.ignorePaths) == null) {
  337. opts.ignorePaths = false;
  338. }
  339. if ((_ref7 = opts.ignoreHiddenFiles) == null) {
  340. opts.ignoreHiddenFiles = false;
  341. }
  342. if ((_ref8 = opts.ignoreCommonPatterns) == null) {
  343. opts.ignoreCommonPatterns = true;
  344. }
  345. if ((_ref9 = opts.ignoreCustomPatterns) == null) {
  346. opts.ignoreCustomPatterns = false;
  347. }
  348. if (opts.ignoreCommonPatterns === true) {
  349. opts.ignoreCommonPatterns = balUtilPaths.ignoreCommonPatterns;
  350. }
  351. if (opts.ignorePaths) {
  352. _ref10 = opts.ignorePaths;
  353. for (_i = 0, _len = _ref10.length; _i < _len; _i++) {
  354. ignorePath = _ref10[_i];
  355. if (path.indexOf(ignorePath) === 0) {
  356. result = true;
  357. break;
  358. }
  359. }
  360. }
  361. result = result || (opts.ignoreHiddenFiles && /^\./.test(basename)) || (opts.ignoreCommonPatterns && opts.ignoreCommonPatterns.test(basename)) || (opts.ignoreCustomPatterns && opts.ignoreCustomPatterns.test(basename)) || false;
  362. return result;
  363. },
  364. scandir: function() {
  365. var args, err, list, opts, tasks, tree, _ref10, _ref11, _ref12, _ref6, _ref7, _ref8, _ref9;
  366. args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  367. list = {};
  368. tree = {};
  369. if (args.length === 1) {
  370. opts = args[0];
  371. } else if (args.length >= 4) {
  372. opts = {
  373. path: args[0],
  374. fileAction: args[1] || null,
  375. dirAction: args[2] || null,
  376. next: args[3] || null
  377. };
  378. } else {
  379. err = new Error('balUtilPaths.scandir: unsupported arguments');
  380. if (next) {
  381. return next(err);
  382. } else {
  383. throw err;
  384. }
  385. }
  386. if ((_ref6 = opts.recurse) == null) {
  387. opts.recurse = true;
  388. }
  389. if ((_ref7 = opts.readFiles) == null) {
  390. opts.readFiles = false;
  391. }
  392. if ((_ref8 = opts.ignorePaths) == null) {
  393. opts.ignorePaths = false;
  394. }
  395. if ((_ref9 = opts.ignoreHiddenFiles) == null) {
  396. opts.ignoreHiddenFiles = false;
  397. }
  398. if ((_ref10 = opts.ignoreCommonPatterns) == null) {
  399. opts.ignoreCommonPatterns = false;
  400. }
  401. if (opts.action != null) {
  402. if ((_ref11 = opts.fileAction) == null) {
  403. opts.fileAction = opts.action;
  404. }
  405. if ((_ref12 = opts.dirAction) == null) {
  406. opts.dirAction = opts.action;
  407. }
  408. }
  409. if (opts.parentPath && !opts.path) {
  410. opts.path = opts.parentPath;
  411. }
  412. if (!opts.path) {
  413. err = new Error('balUtilPaths.scandir: path is needed');
  414. if (next) {
  415. return next(err);
  416. } else {
  417. throw err;
  418. }
  419. }
  420. tasks = new balUtilFlow.Group(function(err) {
  421. return opts.next(err, list, tree);
  422. });
  423. balUtilPaths.readdir(opts.path, function(err, files) {
  424. if (tasks.exited) {
  425. return;
  426. } else if (err) {
  427. console.log('balUtilPaths.scandir: readdir has failed on:', opts.path);
  428. return tasks.exit(err);
  429. }
  430. tasks.total += files.length;
  431. if (!files.length) {
  432. return tasks.exit();
  433. } else {
  434. return files.forEach(function(file) {
  435. var fileFullPath, fileRelativePath, isIgnoredFile;
  436. fileFullPath = pathUtil.join(opts.path, file);
  437. fileRelativePath = opts.relativePath ? pathUtil.join(opts.relativePath, file) : file;
  438. isIgnoredFile = balUtilPaths.isIgnoredPath(fileFullPath, {
  439. ignorePaths: opts.ignorePaths,
  440. ignoreHiddenFiles: opts.ignoreHiddenFiles,
  441. ignoreCommonPatterns: opts.ignoreCommonPatterns,
  442. ignoreCustomPatterns: opts.ignoreCustomPatterns
  443. });
  444. if (isIgnoredFile) {
  445. return tasks.complete();
  446. }
  447. return balUtilPaths.isDirectory(fileFullPath, function(err, isDirectory, fileStat) {
  448. var complete;
  449. if (tasks.exited) {
  450. } else if (err) {
  451. console.log('balUtilPaths.scandir: isDirectory has failed on:', fileFullPath);
  452. return tasks.exit(err);
  453. } else if (isDirectory) {
  454. complete = function(err, skip, subtreeCallback) {
  455. if (err) {
  456. return tasks.exit(err);
  457. }
  458. if (tasks.exited) {
  459. return tasks.exit();
  460. }
  461. if (skip !== true) {
  462. list[fileRelativePath] = 'dir';
  463. tree[file] = {};
  464. if (!opts.recurse) {
  465. return tasks.complete();
  466. } else {
  467. return balUtilPaths.scandir({
  468. path: fileFullPath,
  469. relativePath: fileRelativePath,
  470. fileAction: opts.fileAction,
  471. dirAction: opts.dirAction,
  472. readFiles: opts.readFiles,
  473. ignorePaths: opts.ignorePaths,
  474. ignoreHiddenFiles: opts.ignoreHiddenFiles,
  475. ignoreCommonPatterns: opts.ignoreCommonPatterns,
  476. ignoreCustomPatterns: opts.ignoreCustomPatterns,
  477. recurse: opts.recurse,
  478. stat: opts.fileStat,
  479. next: function(err, _list, _tree) {
  480. var filePath, fileType;
  481. tree[file] = _tree;
  482. for (filePath in _list) {
  483. if (!__hasProp.call(_list, filePath)) continue;
  484. fileType = _list[filePath];
  485. list[filePath] = fileType;
  486. }
  487. if (tasks.exited) {
  488. return tasks.exit();
  489. } else if (err) {
  490. console.log('balUtilPaths.scandir: has failed on:', fileFullPath);
  491. return tasks.exit(err);
  492. } else if (subtreeCallback) {
  493. return subtreeCallback(tasks.completer());
  494. } else {
  495. return tasks.complete();
  496. }
  497. }
  498. });
  499. }
  500. } else {
  501. return tasks.complete();
  502. }
  503. };
  504. if (opts.dirAction) {
  505. return opts.dirAction(fileFullPath, fileRelativePath, complete, fileStat);
  506. } else if (opts.dirAction === false) {
  507. return complete(err, true);
  508. } else {
  509. return complete(err, false);
  510. }
  511. } else {
  512. complete = function(err, skip) {
  513. if (err) {
  514. return tasks.exit(err);
  515. }
  516. if (tasks.exited) {
  517. return tasks.exit();
  518. }
  519. if (skip) {
  520. return tasks.complete();
  521. } else {
  522. if (opts.readFiles) {
  523. return balUtilPaths.readFile(fileFullPath, function(err, data) {
  524. var dataString;
  525. if (err) {
  526. return tasks.exit(err);
  527. }
  528. dataString = data.toString();
  529. list[fileRelativePath] = dataString;
  530. tree[file] = dataString;
  531. return tasks.complete();
  532. });
  533. } else {
  534. list[fileRelativePath] = 'file';
  535. tree[file] = true;
  536. return tasks.complete();
  537. }
  538. }
  539. };
  540. if (opts.fileAction) {
  541. return opts.fileAction(fileFullPath, fileRelativePath, complete, fileStat);
  542. } else if (opts.fileAction === false) {
  543. return complete(err, true);
  544. } else {
  545. return complete(err, false);
  546. }
  547. }
  548. });
  549. });
  550. }
  551. });
  552. return this;
  553. },
  554. cpdir: function() {
  555. var args, err, next, opt, opts, outPath, scandirOpts, srcPath, _i, _len, _ref6, _ref7;
  556. args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  557. opts = {};
  558. if (args.length === 1) {
  559. _ref6 = opts = args[0], srcPath = _ref6.srcPath, outPath = _ref6.outPath, next = _ref6.next;
  560. } else if (args.length >= 3) {
  561. srcPath = args[0], outPath = args[1], next = args[2];
  562. } else {
  563. err = new Error('balUtilPaths.cpdir: unknown arguments');
  564. if (next) {
  565. return next(err);
  566. } else {
  567. throw err;
  568. }
  569. }
  570. scandirOpts = {
  571. path: srcPath,
  572. fileAction: function(fileSrcPath, fileRelativePath, next) {
  573. var fileOutPath;
  574. fileOutPath = pathUtil.join(outPath, fileRelativePath);
  575. return balUtilPaths.ensurePath(pathUtil.dirname(fileOutPath), function(err) {
  576. if (err) {
  577. console.log('balUtilPaths.cpdir: failed to create the path for the file:', fileSrcPath);
  578. return next(err);
  579. }
  580. return balUtilPaths.cp(fileSrcPath, fileOutPath, function(err) {
  581. if (err) {
  582. console.log('balUtilPaths.cpdir: failed to copy the child file:', fileSrcPath);
  583. }
  584. return next(err);
  585. });
  586. });
  587. },
  588. next: next
  589. };
  590. _ref7 = ['ignorePaths', 'ignoreHiddenFiles', 'ignoreCommonPatterns', 'ignoreCustomPatterns'];
  591. for (_i = 0, _len = _ref7.length; _i < _len; _i++) {
  592. opt = _ref7[_i];
  593. scandirOpts[opt] = opts[opt];
  594. }
  595. balUtilPaths.scandir(scandirOpts);
  596. return this;
  597. },
  598. rpdir: function() {
  599. var args, err, next, opt, opts, outPath, scandirOpts, srcPath, _i, _len, _ref6, _ref7;
  600. args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  601. opts = {};
  602. if (args.length === 1) {
  603. _ref6 = opts = args[0], srcPath = _ref6.srcPath, outPath = _ref6.outPath, next = _ref6.next;
  604. } else if (args.length >= 3) {
  605. srcPath = args[0], outPath = args[1], next = args[2];
  606. } else {
  607. err = new Error('balUtilPaths.cpdir: unknown arguments');
  608. if (next) {
  609. return next(err);
  610. } else {
  611. throw err;
  612. }
  613. }
  614. scandirOpts = {
  615. path: srcPath,
  616. fileAction: function(fileSrcPath, fileRelativePath, next) {
  617. var fileOutPath;
  618. fileOutPath = pathUtil.join(outPath, fileRelativePath);
  619. return balUtilPaths.ensurePath(pathUtil.dirname(fileOutPath), function(err) {
  620. if (err) {
  621. console.log('balUtilPaths.rpdir: failed to create the path for the file:', fileSrcPath);
  622. return next(err);
  623. }
  624. return balUtilPaths.isPathOlderThan(fileOutPath, fileSrcPath, function(err, older) {
  625. if (older === true || older === null) {
  626. return balUtilPaths.cp(fileSrcPath, fileOutPath, function(err) {
  627. if (err) {
  628. console.log('balUtilPaths.rpdir: failed to copy the child file:', fileSrcPath);
  629. }
  630. return next(err);
  631. });
  632. } else {
  633. return next();
  634. }
  635. });
  636. });
  637. },
  638. next: next
  639. };
  640. _ref7 = ['ignorePaths', 'ignoreHiddenFiles', 'ignoreCommonPatterns', 'ignoreCustomPatterns'];
  641. for (_i = 0, _len = _ref7.length; _i < _len; _i++) {
  642. opt = _ref7[_i];
  643. scandirOpts[opt] = opts[opt];
  644. }
  645. balUtilPaths.scandir(scandirOpts);
  646. return this;
  647. },
  648. rmdirDeep: function(parentPath, next) {
  649. balUtilPaths.exists(parentPath, function(exists) {
  650. if (!exists) {
  651. return next();
  652. }
  653. return balUtilPaths.scandir(parentPath, function(fileFullPath, fileRelativePath, next) {
  654. return balUtilPaths.unlink(fileFullPath, function(err) {
  655. if (err) {
  656. console.log('balUtilPaths.rmdirDeep: failed to remove the child file:', fileFullPath);
  657. }
  658. return next(err);
  659. });
  660. }, function(fileFullPath, fileRelativePath, next) {
  661. return next(null, false, function(next) {
  662. return balUtilPaths.rmdirDeep(fileFullPath, function(err) {
  663. if (err) {
  664. console.log('balUtilPaths.rmdirDeep: failed to remove the child directory:', fileFullPath);
  665. }
  666. return next(err);
  667. });
  668. });
  669. }, function(err, list, tree) {
  670. if (err) {
  671. return next(err, list, tree);
  672. }
  673. return balUtilPaths.rmdir(parentPath, function(err) {
  674. if (err) {
  675. console.log('balUtilPaths.rmdirDeep: failed to remove the parent directory:', parentPath);
  676. }
  677. return next(err, list, tree);
  678. });
  679. });
  680. });
  681. return this;
  682. },
  683. writetree: function(dstPath, tree, next) {
  684. var tasks;
  685. tasks = new balUtilFlow.Group(function(err) {
  686. return next(err);
  687. });
  688. balUtilPaths.ensurePath(dstPath, function(err) {
  689. var fileFullPath, fileRelativePath, value;
  690. if (err) {
  691. return tasks.exit(err);
  692. }
  693. for (fileRelativePath in tree) {
  694. if (!__hasProp.call(tree, fileRelativePath)) continue;
  695. value = tree[fileRelativePath];
  696. ++tasks.total;
  697. fileFullPath = pathUtil.join(dstPath, fileRelativePath.replace(/^\/+/, ''));
  698. if (balUtilTypes.isObject(value)) {
  699. balUtilPaths.writetree(fileFullPath, value, tasks.completer());
  700. } else {
  701. balUtilPaths.writeFile(fileFullPath, value, function(err) {
  702. if (err) {
  703. console.log('balUtilPaths.writetree: writeFile failed on:', fileFullPath);
  704. }
  705. return tasks.complete(err);
  706. });
  707. }
  708. }
  709. if (tasks.total === 0) {
  710. tasks.exit();
  711. }
  712. });
  713. return this;
  714. },
  715. readPath: function(filePath, next) {
  716. var http, requestOpts;
  717. if (/^http/.test(filePath)) {
  718. requestOpts = require('url').parse(filePath);
  719. http = requestOpts.protocol === 'https:' ? require('https') : require('http');
  720. http.get(requestOpts, function(res) {
  721. var data;
  722. data = '';
  723. res.on('data', function(chunk) {
  724. return data += chunk;
  725. });
  726. return res.on('end', function() {
  727. var locationHeader, _ref6;
  728. locationHeader = ((_ref6 = res.headers) != null ? _ref6.location : void 0) || null;
  729. if (locationHeader && locationHeader !== requestOpts.href) {
  730. return balUtilPaths.readPath(locationHeader, next);
  731. } else {
  732. return next(null, data);
  733. }
  734. });
  735. }).on('error', function(err) {
  736. return next(err);
  737. });
  738. } else {
  739. balUtilPaths.readFile(filePath, function(err, data) {
  740. if (err) {
  741. return next(err);
  742. }
  743. return next(null, data);
  744. });
  745. }
  746. return this;
  747. },
  748. empty: function(filePath, next) {
  749. balUtilPaths.exists(filePath, function(exists) {
  750. if (!exists) {
  751. return next(null, true);
  752. }
  753. return balUtilPaths.stat(filePath, function(err, stat) {
  754. if (err) {
  755. return next(err);
  756. }
  757. return next(null, stat.size === 0);
  758. });
  759. });
  760. return this;
  761. },
  762. isPathOlderThan: function(aPath, bInput, next) {
  763. var bMtime, bPath, mode;
  764. bMtime = null;
  765. if (balUtilTypes.isNumber(bInput)) {
  766. mode = 'time';
  767. bMtime = new Date(new Date() - bInput);
  768. } else {
  769. mode = 'path';
  770. bPath = bInput;
  771. }
  772. balUtilPaths.empty(aPath, function(err, empty) {
  773. if (empty || err) {
  774. return next(err, null);
  775. }
  776. return balUtilPaths.stat(aPath, function(err, aStat) {
  777. var compare;
  778. if (err) {
  779. return next(err);
  780. }
  781. compare = function() {
  782. var older;
  783. if (aStat.mtime < bMtime) {
  784. older = true;
  785. } else {
  786. older = false;
  787. }
  788. return next(null, older);
  789. };
  790. if (mode === 'path') {
  791. return balUtilPaths.empty(bPath, function(err, empty) {
  792. if (empty || err) {
  793. return next(err, null);
  794. }
  795. return balUtilPaths.stat(bPath, function(err, bStat) {
  796. if (err) {
  797. return next(err);
  798. }
  799. bMtime = bStat.mtime;
  800. return compare();
  801. });
  802. });
  803. } else {
  804. return compare();
  805. }
  806. });
  807. });
  808. return this;
  809. }
  810. };
  811. module.exports = balUtilPaths;
  812. }).call(this);