Nenhuma descrição

jit-grunt.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. 'use strict';
  2. var fs = require('fs');
  3. var path = require('path');
  4. var PREFIXES = ['', 'grunt-', 'grunt-contrib-'];
  5. var EXTENSIONS = ['.coffee', '.js'];
  6. var jit = {
  7. pluginsRoot: 'node_modules',
  8. mappings: {}
  9. };
  10. jit.findUp = function (cwd, iterator) {
  11. var result = iterator(cwd);
  12. if (result) {
  13. return result;
  14. }
  15. var parent = path.resolve(cwd, '..');
  16. return parent !== cwd ? jit.findUp(parent, iterator) : null;
  17. };
  18. jit.findPlugin = function (taskName) {
  19. var pluginName, taskPath;
  20. // Static Mappings
  21. if (this.mappings.hasOwnProperty(taskName)) {
  22. pluginName = this.mappings[taskName];
  23. if (pluginName.indexOf('/') >= 0 && pluginName.indexOf('@') !== 0) {
  24. taskPath = path.resolve(pluginName);
  25. if (fs.existsSync(taskPath)) {
  26. return jit.loadPlugin(taskName, taskPath, true);
  27. }
  28. } else {
  29. var dir = path.join(jit.pluginsRoot, pluginName, 'tasks');
  30. taskPath = jit.findUp(path.resolve(), function (cwd) {
  31. var findPath = path.join(cwd, dir);
  32. return fs.existsSync(findPath) ? findPath : null;
  33. });
  34. if (taskPath) {
  35. return jit.loadPlugin(pluginName, taskPath);
  36. }
  37. }
  38. }
  39. // Custom Tasks
  40. if (jit.customTasksDir) {
  41. for (var i = EXTENSIONS.length; i--;) {
  42. taskPath = path.join(jit.customTasksDir, taskName + EXTENSIONS[i]);
  43. if (fs.existsSync(taskPath)) {
  44. return jit.loadPlugin(taskName, taskPath, true);
  45. }
  46. }
  47. }
  48. // Auto Mappings
  49. var dashedName = taskName.replace(/([A-Z])/g, '-$1').replace(/_+/g, '-').toLowerCase();
  50. taskPath = jit.findUp(path.resolve(), function (cwd) {
  51. for (var p = PREFIXES.length; p--;) {
  52. pluginName = PREFIXES[p] + dashedName;
  53. var findPath = path.join(cwd, jit.pluginsRoot, pluginName, 'tasks');
  54. if (fs.existsSync(findPath)) {
  55. return findPath;
  56. }
  57. }
  58. });
  59. if (taskPath) {
  60. return jit.loadPlugin(pluginName, taskPath);
  61. }
  62. var log = jit.grunt.log.writeln;
  63. log();
  64. log('jit-grunt: Plugin for the "'.yellow + taskName.yellow + '" task not found.'.yellow);
  65. log('If you have installed the plugin already, please setting the static mapping.'.yellow);
  66. log('See'.yellow, 'https://github.com/shootaroo/jit-grunt#static-mappings'.cyan);
  67. log();
  68. };
  69. jit.loadPlugin = function (name, path, isFile) {
  70. var grunt = jit.grunt;
  71. var _write = grunt.log._write;
  72. var _nameArgs = grunt.task.current.nameArgs;
  73. grunt.task.current.nameArgs = 'loading ' + name;
  74. if (jit.hideHeader) {
  75. grunt.log._write = function () {};
  76. }
  77. grunt.log.header('Loading "' + name + '" plugin');
  78. grunt.log._write = _write;
  79. if (isFile) {
  80. var fn = require(path);
  81. if (typeof fn === 'function') {
  82. fn.call(grunt, grunt);
  83. }
  84. } else {
  85. grunt.loadTasks(path);
  86. }
  87. grunt.task.current.nameArgs = _nameArgs;
  88. };
  89. jit.proxy = function (name) {
  90. return {
  91. task: {
  92. name: name,
  93. fn: function () {
  94. var thing = jit._taskPlusArgs.call(jit.grunt.task, name);
  95. if (!thing.task) {
  96. jit.findPlugin(thing.args[0]);
  97. thing = jit._taskPlusArgs.call(jit.grunt.task, name);
  98. if (!thing.task) {
  99. return new Error('Task "' + name + '" failed.');
  100. }
  101. }
  102. this.nameArgs = thing.nameArgs;
  103. this.name = thing.task.name;
  104. this.args = thing.args;
  105. this.flags = thing.flags;
  106. return thing.task.fn.apply(this, this.args);
  107. }
  108. },
  109. nameArgs: name,
  110. args: null,
  111. flags: null
  112. };
  113. };
  114. module.exports = function factory(grunt, mappings) {
  115. if (!jit.grunt) {
  116. jit.grunt = grunt;
  117. jit.hideHeader = !grunt.option('verbose');
  118. // Override _taskPlusArgs
  119. jit._taskPlusArgs = grunt.util.task.Task.prototype._taskPlusArgs;
  120. grunt.util.task.Task.prototype._taskPlusArgs = jit.proxy;
  121. }
  122. for (var key in mappings) {
  123. if (mappings.hasOwnProperty(key)) {
  124. jit.mappings[key] = mappings[key];
  125. }
  126. }
  127. return jit;
  128. };