replace.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * grunt-replace
  3. *
  4. * Copyright (c) 2014 outaTiME
  5. * Licensed under the MIT license.
  6. * https://github.com/outaTiME/grunt-replace/blob/master/LICENSE-MIT
  7. */
  8. 'use strict';
  9. // plugin
  10. module.exports = function (grunt) {
  11. var path = require('path');
  12. var fs = require('fs');
  13. var chalk = require('chalk');
  14. var Applause = require('applause');
  15. grunt.registerMultiTask('replace', 'Replace text patterns with applause.', function () {
  16. // took options
  17. var options = this.options({
  18. encoding: grunt.file.defaultEncoding,
  19. mode: false,
  20. processContentExclude: [],
  21. patterns: [],
  22. excludeBuiltins: false,
  23. force: true,
  24. verbose: true
  25. });
  26. // attach builtins
  27. var patterns = options.patterns;
  28. if (options.excludeBuiltins !== true) {
  29. patterns.push({
  30. match: '__SOURCE_FILE__',
  31. replacement: function (match, offset, string, source, target) {
  32. return source;
  33. }
  34. }, {
  35. match: '__SOURCE_PATH__',
  36. replacement: function (match, offset, string, source, target) {
  37. return path.dirname(source);
  38. }
  39. }, {
  40. match: '__SOURCE_FILENAME__',
  41. replacement: function (match, offset, string, source, target) {
  42. return path.basename(source);
  43. }
  44. }, {
  45. match: '__TARGET_FILE__',
  46. replacement: function (match, offset, string, source, target) {
  47. return target;
  48. }
  49. }, {
  50. match: '__TARGET_PATH__',
  51. replacement: function (match, offset, string, source, target) {
  52. return path.dirname(target);
  53. }
  54. }, {
  55. match: '__TARGET_FILENAME__',
  56. replacement: function (match, offset, string, source, target) {
  57. return path.basename(target);
  58. }
  59. });
  60. }
  61. // create applause instance
  62. var applause = Applause.create(options);
  63. // took code from copy task
  64. var dest;
  65. var isExpandedPair;
  66. this.files.forEach(function (filePair) {
  67. isExpandedPair = filePair.orig.expand || false;
  68. filePair.src.forEach(function (src) {
  69. if (detectDestType(filePair.dest) === 'directory') {
  70. dest = (isExpandedPair) ? filePair.dest : unixifyPath(path.join(filePair.dest, src));
  71. } else {
  72. dest = filePair.dest;
  73. }
  74. if (grunt.file.isDir(src)) {
  75. grunt.file.mkdir(dest);
  76. } else {
  77. replace(src, dest, options, applause);
  78. if (options.mode !== false) {
  79. fs.chmodSync(dest, (options.mode === true) ? fs.lstatSync(src).mode : options.mode);
  80. }
  81. }
  82. });
  83. });
  84. });
  85. var detectDestType = function (dest) {
  86. var lastChar = dest.slice(-1);
  87. if (lastChar === '/') {
  88. return 'directory';
  89. } else {
  90. return 'file';
  91. }
  92. };
  93. var unixifyPath = function (filepath) {
  94. if (process.platform === 'win32') {
  95. return filepath.replace(/\\/g, '/');
  96. } else {
  97. return filepath;
  98. }
  99. };
  100. var replace = function (source, target, options, applause) {
  101. grunt.file.copy(source, target, {
  102. encoding: options.encoding,
  103. process: function (contents) {
  104. var result = applause.replace(contents, [source, target]);
  105. // force contents
  106. if (result === false && options.force === true) {
  107. result = contents;
  108. }
  109. if (result !== false && options.verbose === true) {
  110. grunt.log.writeln('Replace ' + chalk.cyan(source) + ' → ' +
  111. chalk.green(target));
  112. }
  113. return result;
  114. },
  115. noProcess: options.noProcess || options.processContentExclude
  116. });
  117. };
  118. };