Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. import {unique} from 'lodash';
  2. import * as path from 'path';
  3.  
  4. /*
  5. * Grunt copy task that's faster by eliminating unnecessary or redundant fs calls.
  6. *
  7. * Things it does *not* do:
  8. * - set or copy file mode / permissions
  9. * - "process"ing files
  10. * - "process"ing files as raw buffers
  11. * - recursively copying directories
  12. * - tally created files and directories identically to grunt-contrib-copy
  13. * - verbose logging identical to grunt-contrib-copy
  14. * - catch case where src is directory and dest is a pre-existing file (silently succeeds instead)
  15. */
  16. grunt.registerMultiTask('copy', function() {
  17. const {target, data, files} = this;
  18. const destFiles = [];
  19. files.forEach(fileMapping => {
  20. const {src: srcArray, dest} = fileMapping;
  21. if(srcArray.length !== 1) throw new Error('Only supports one-to-one file mappings.');
  22. destFiles.push(dest);
  23. });
  24. const dirs = unique(destFiles.map(f => path.dirname(f)));
  25. addAllParentDirs(dirs);
  26. // Ensure that parent directories are always created before children
  27. dirs.sort();
  28. let createdDirectories = 0;
  29. dirs.forEach(dir => {
  30. if(mkDir(dir)) createdDirectories++;
  31. });
  32. let copiedFiles = 0;
  33. files.forEach(({src: [src], dest}) => {
  34. switch(copyFile(src, dest, true)) {
  35. case 1:
  36. copiedFiles++;
  37. break;
  38. case 2:
  39. createdDirectories++;
  40. break;
  41. }
  42. });
  43. console.log(`Created ${ createdDirectories } directories, copied ${ copiedFiles } files`);
  44. });
  45.  
  46.  
  47. /**
  48. * Given an array of paths, recursively adds paths to all containing directories.
  49. * For example, for path '/foo/bar/baz', adds '/foo/bar' and '/foo'.
  50. */
  51. function addAllParentDirs(dirs) {
  52. for(let i = 0; i < dirs.length; i++) {
  53. const dir = dirs[i];
  54. const dirOfDir = path.dirname(dir);
  55. if(dirOfDir !== '.' && dirs.indexOf(dirOfDir) === -1) dirs.push(dirOfDir);
  56. }
  57. }
  58.  
  59.  
  60. /**
  61. * Copies src to dest. If src is a directory, then creates target directory.
  62. * Returns 0 if nothing was done, 1 if file was created, 2 if directory was created.
  63. */
  64. function copyFile(src, dest, createDir = false) {
  65. let content;
  66. try {
  67. content = fs.readFileSync(src);
  68. } catch(e) {
  69. // If we tried to read a directory, that's ok. Just skip it.
  70. if(e.code === 'EISDIR') {
  71. if(createDir && mkDir(dest)) return 2;
  72. return 0;
  73. }
  74. throw e;
  75. }
  76. fs.writeFileSync(dest, content);
  77. return 1;
  78. }
  79.  
  80.  
  81. /**
  82. * Create directory at given path if a file or directory does not already exist at that path.
  83. * Does not indicate if a file existed at that path even though that might be unexpected.
  84. * Returns true if directory was created, false otherwise.
  85. */
  86. function mkDir(dir) {
  87. try {
  88. fs.mkdirSync(dir);
  89. } catch(e) {
  90. // If the directory already exists, that's ok.
  91. if(e.code !== 'EEXIST') throw e;
  92. return false;
  93. }
  94. return true;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement