Guest User

Untitled

a guest
Oct 21st, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. const fs = require('fs');
  2. const path = require('path');
  3. const readFile = promisify(fs.readFile);
  4. const writeFile = promisify(fs.writeFile);
  5. const glob = promisify(require('glob'));
  6.  
  7. const GLOB_PATTERN = `**/*.{md,html,js}`;
  8.  
  9. const EACH_AS_REGEX = /([^\}+]*)#each\s+([^\s]*)\s+as\s+([^\s|\}]*)/gm;
  10. const HELPER_EXPRESSION_REGEX = /(\{+)(#if|#unless|#each|#with|log|deubgger|#eq|#is|#switch|#case|#default|joinbase|#routeCurrent|routeUrl)\s+([^\}]*)/gm;
  11. const ARGUMENT_DELIMITER_REGEX = /,?\s+/gm;
  12. const ROUTE_HELPERS = /#routeCurrent|routeUrl/;
  13.  
  14.  
  15. module.exports = {
  16. getOptions: function () {
  17. return [];
  18. },
  19. run: function (directory) {
  20. return glob(GLOB_PATTERN, { cwd: directory }).then(fileNames => {
  21. let fileIndex = {};
  22. return Promise.all(fileNames.map(fileName => {
  23. let filePath = path.join(directory, fileName);
  24. return readFile(filePath, 'utf8').then(fileContent => fileIndex[filePath] = fileContent);
  25. })).then(() => fileIndex);
  26. }).then((fileIndex) => {
  27. for(let fileName in fileIndex){
  28. let newFileContents = replace(fileIndex[fileName]);
  29. if(newFileContents !== null){
  30. fileIndex[fileName] = newFileContents;
  31. }
  32. }
  33. return fileIndex;
  34. }).then((fileIndex) => {
  35. return Promise.all(Object.keys(fileIndex).map(fileName => writeFile(fileName, fileIndex[fileName], 'utf8')));
  36. });
  37. }
  38. };
  39.  
  40. function replaceAsWithHash(fileContents){
  41. let matches = EACH_AS_REGEX.exec(fileContents);
  42. if(matches && matches[0] && matches[1] && matches[2] && matches[3]){
  43. fileContents = fileContents.replace(matches[0], `${matches[1]}#each(${matches[2]}, ${matches[3]}=value)`);
  44. return replaceAsWithHash(fileContents);
  45. }
  46. return fileContents;
  47. }
  48.  
  49. function replaceCallExpression(fileContents){
  50. let matches = HELPER_EXPRESSION_REGEX.exec(fileContents);
  51. if(matches && matches[0] && matches[1] && matches[2]){
  52. let args = (matches[3] || '').trim();
  53. if(ROUTE_HELPERS.test(matches[2])){
  54. let argString = '';
  55. args = args.split(ARGUMENT_DELIMITER_REGEX);
  56. args.forEach((arg, i) => {
  57. argString += arg;
  58. let nextArg = args[i + 1];
  59. if(nextArg){
  60. if(arg === 'true' || nextArg === 'true'){
  61. argString += ',';
  62. }
  63. argString += ' ';
  64. }
  65. });
  66. args = argString;
  67. }else{
  68. args = args.replace(ARGUMENT_DELIMITER_REGEX, ', ');
  69. }
  70. fileContents = fileContents.replace(matches[0], `${matches[1]}${matches[2]}(${args})`);
  71. return replaceCallExpression(fileContents);
  72. }
  73. return fileContents;
  74. }
  75.  
  76. function replace(fileContents){
  77. let originalFileContents = fileContents;
  78. fileContents = replaceAsWithHash(fileContents);
  79. fileContents = replaceCallExpression(fileContents);
  80. return originalFileContents === fileContents ? null : fileContents;
  81. }
  82.  
  83. function promisify(fn){
  84. return function(){
  85. return new Promise((resolve, reject) => {
  86. fn.apply(fn, [...arguments, (err, result) => {
  87. if(err){
  88. return reject(err);
  89. }
  90. resolve(result);
  91. }]);
  92. });
  93. };
  94. }
Add Comment
Please, Sign In to add comment