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.

193 lines
5.4 KiB

  1. // Generated by CoffeeScript 1.6.2
  2. var PluginLoader, pathUtil, safefs, semver, util,
  3. __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
  4. pathUtil = require('path');
  5. semver = require('semver');
  6. safefs = require('safefs');
  7. util = require('util');
  8. PluginLoader = (function() {
  9. PluginLoader.prototype.docpad = null;
  10. PluginLoader.prototype.BasePlugin = null;
  11. PluginLoader.prototype.dirPath = null;
  12. PluginLoader.prototype.packagePath = null;
  13. PluginLoader.prototype.packageData = {};
  14. PluginLoader.prototype.pluginPath = null;
  15. PluginLoader.prototype.pluginClass = {};
  16. PluginLoader.prototype.pluginName = null;
  17. PluginLoader.prototype.pluginVersion = null;
  18. PluginLoader.prototype.nodeModulesPath = null;
  19. function PluginLoader(_arg) {
  20. this.docpad = _arg.docpad, this.dirPath = _arg.dirPath, this.BasePlugin = _arg.BasePlugin;
  21. docpad = this.docpad;
  22. this.pluginName = pathUtil.basename(this.dirPath).replace(/^docpad-plugin-/, '');
  23. this.pluginClass = {};
  24. this.packageData = {};
  25. this.nodeModulesPath = pathUtil.resolve(this.dirPath, 'node_modules');
  26. }
  27. PluginLoader.prototype.exists = function(next) {
  28. var failure, packagePath, success,
  29. _this = this;
  30. packagePath = this.packagePath || pathUtil.resolve(this.dirPath, "package.json");
  31. failure = function(err) {
  32. if (err == null) {
  33. err = null;
  34. }
  35. return next(err, false);
  36. };
  37. success = function() {
  38. return next(null, true);
  39. };
  40. safefs.exists(packagePath, function(exists) {
  41. if (!exists) {
  42. return failure();
  43. }
  44. _this.packagePath = packagePath;
  45. return safefs.readFile(packagePath, function(err, data) {
  46. var pluginPath, pluginVersion;
  47. if (err) {
  48. return failure(err);
  49. }
  50. try {
  51. _this.packageData = JSON.parse(data.toString());
  52. } catch (_error) {
  53. err = _error;
  54. return failure(err);
  55. } finally {
  56. if (!_this.packageData) {
  57. return failure();
  58. }
  59. }
  60. pluginVersion = _this.packageData.version;
  61. pluginPath = _this.packageData.main && pathUtil.join(_this.dirPath, _this.packageData.main);
  62. if (!pluginVersion) {
  63. return failure();
  64. }
  65. if (!pluginPath) {
  66. return failure();
  67. }
  68. _this.pluginPath = pluginVersion;
  69. _this.pluginPath = pluginPath;
  70. return success();
  71. });
  72. });
  73. return this;
  74. };
  75. PluginLoader.prototype.unsupported = function(next) {
  76. var docpad, engines, keywords, platforms, unsupported, _ref;
  77. docpad = this.docpad;
  78. unsupported = false;
  79. keywords = this.packageData.keywords || [];
  80. if (__indexOf.call(keywords, 'docpad-plugin') < 0) {
  81. unsupported = 'type';
  82. }
  83. if (this.packageData.platforms) {
  84. platforms = this.packageData.platforms || [];
  85. if (_ref = process.platform, __indexOf.call(platforms, _ref) < 0) {
  86. unsupported = 'platform';
  87. }
  88. }
  89. if (this.packageData.engines) {
  90. engines = this.packageData.engines || {};
  91. if (engines.node != null) {
  92. if (!semver.satisfies(process.version, engines.node)) {
  93. unsupported = 'engine';
  94. }
  95. }
  96. if (engines.docpad != null) {
  97. if (!semver.satisfies(docpad.version, engines.docpad)) {
  98. unsupported = 'version';
  99. }
  100. }
  101. }
  102. next(null, unsupported);
  103. return this;
  104. };
  105. PluginLoader.prototype.install = function(next) {
  106. var docpad;
  107. docpad = this.docpad;
  108. if (this.packagePath) {
  109. docpad.initNodeModules({
  110. path: this.dirPath,
  111. next: function(err, results) {
  112. return next(err);
  113. }
  114. });
  115. } else {
  116. next();
  117. }
  118. return this;
  119. };
  120. PluginLoader.prototype.load = function(next) {
  121. var docpad, err, locale, pluginPrototypeName, validPluginName, _base, _ref;
  122. docpad = this.docpad;
  123. locale = docpad.getLocale();
  124. try {
  125. this.pluginClass = require(this.pluginPath)(this.BasePlugin);
  126. if ((_ref = (_base = this.pluginClass.prototype).version) == null) {
  127. _base.version = this.pluginVersion;
  128. }
  129. pluginPrototypeName = this.pluginClass.prototype.name;
  130. if (/^[a-z0-9]+$/.test(this.pluginName) === false) {
  131. validPluginName = this.pluginName.replace(/[^a-z0-9]/, '');
  132. docpad.log('warn', util.format(locale.pluginNamingConventionInvalid, this.pluginName, validPluginName));
  133. }
  134. if (pluginPrototypeName === null) {
  135. this.pluginClass.prototype.name = this.pluginName;
  136. docpad.log('warn', util.format(locale.pluginPrototypeNameUndefined, this.pluginName));
  137. } else if (pluginPrototypeName !== this.pluginName) {
  138. docpad.log('warn', util.format(locale.pluginPrototypeNameDifferent, this.pluginName, pluginPrototypeName));
  139. }
  140. } catch (_error) {
  141. err = _error;
  142. return next(err, null);
  143. }
  144. next(null, this.pluginClass);
  145. return this;
  146. };
  147. PluginLoader.prototype.create = function(config, next) {
  148. var docpad, err, pluginInstance;
  149. try {
  150. docpad = this.docpad;
  151. pluginInstance = new this.pluginClass({
  152. docpad: docpad,
  153. config: config
  154. });
  155. } catch (_error) {
  156. err = _error;
  157. return next(err, null);
  158. }
  159. return next(null, pluginInstance);
  160. return this;
  161. };
  162. return PluginLoader;
  163. })();
  164. module.exports = PluginLoader;