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.

84 lines
2.0 KiB

  1. /*
  2. * grunt-contrib-copy
  3. * http://gruntjs.com/
  4. *
  5. * Copyright (c) 2012 Chris Talkington, contributors
  6. * Licensed under the MIT license.
  7. * https://github.com/gruntjs/grunt-contrib-copy/blob/master/LICENSE-MIT
  8. */
  9. module.exports = function(grunt) {
  10. 'use strict';
  11. var path = require('path');
  12. grunt.registerMultiTask('copy', 'Copy files.', function() {
  13. var kindOf = grunt.util.kindOf;
  14. var options = this.options({
  15. processContent: false,
  16. processContentExclude: []
  17. });
  18. var copyOptions = {
  19. process: options.processContent,
  20. noProcess: options.processContentExclude
  21. };
  22. grunt.verbose.writeflags(options, 'Options');
  23. var dest;
  24. var isExpandedPair;
  25. var tally = {
  26. dirs: 0,
  27. files: 0
  28. };
  29. this.files.forEach(function(filePair) {
  30. isExpandedPair = filePair.orig.expand || false;
  31. filePair.src.forEach(function(src) {
  32. if (detectDestType(filePair.dest) === 'directory') {
  33. dest = (isExpandedPair) ? filePair.dest : unixifyPath(path.join(filePair.dest, src));
  34. } else {
  35. dest = filePair.dest;
  36. }
  37. if (grunt.file.isDir(src)) {
  38. grunt.verbose.writeln('Creating ' + dest.cyan);
  39. grunt.file.mkdir(dest);
  40. tally.dirs++;
  41. } else {
  42. grunt.verbose.writeln('Copying ' + src.cyan + ' -> ' + dest.cyan);
  43. grunt.file.copy(src, dest, copyOptions);
  44. tally.files++;
  45. }
  46. });
  47. });
  48. if (tally.dirs) {
  49. grunt.log.write('Created ' + tally.dirs.toString().cyan + ' directories');
  50. }
  51. if (tally.files) {
  52. grunt.log.write((tally.dirs ? ', copied ' : 'Copied ') + tally.files.toString().cyan + ' files');
  53. }
  54. grunt.log.writeln();
  55. });
  56. var detectDestType = function(dest) {
  57. if (grunt.util._.endsWith(dest, '/')) {
  58. return 'directory';
  59. } else {
  60. return 'file';
  61. }
  62. };
  63. var unixifyPath = function(filepath) {
  64. if (process.platform === 'win32') {
  65. return filepath.replace(/\\/g, '/');
  66. } else {
  67. return filepath;
  68. }
  69. };
  70. };