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.

56 lines
1.4 KiB

  1. var _ = require('lodash');
  2. var Emitter = require('events').EventEmitter;
  3. var wrench = require('wrench');
  4. var path = require('path');
  5. var grunt = require('grunt');
  6. var fs = require('fs');
  7. var Copier = function(assets, options, report) {
  8. this.assets = assets;
  9. this.options = options;
  10. this.report = report;
  11. };
  12. Copier.prototype = Object.create(Emitter.prototype);
  13. Copier.prototype.constructor = Copier;
  14. Copier.prototype.copy = function() {
  15. var error;
  16. _(this.assets).each(function(typedAssets, type) {
  17. try {
  18. this.copyAssets(type, typedAssets);
  19. } catch (err) {
  20. error = err;
  21. this.emit('error', err);
  22. return false;
  23. }
  24. }, this);
  25. if (!error) {
  26. this.emit('copied');
  27. }
  28. return this;
  29. };
  30. Copier.prototype.copyAssets = function(type, assets) {
  31. _(assets).each(function(sources, pkg) {
  32. _(sources).each(function(source) {
  33. var destination;
  34. var isFile = fs.statSync(source).isFile();
  35. var destinationDir = path.join(this.options.targetDir, this.options.layout(type, pkg));
  36. grunt.file.mkdir(destinationDir);
  37. if (isFile) {
  38. destination = path.join(destinationDir, path.basename(source));
  39. grunt.file.copy(source, destination);
  40. } else {
  41. destination = destinationDir;
  42. wrench.copyDirSyncRecursive(source, destination);
  43. }
  44. this.report(source, destination, isFile);
  45. }, this);
  46. }, this);
  47. };
  48. module.exports = Copier;