Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

PluginInfoProvider.js 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. /* jshint sub:true, laxcomma:true, laxbreak:true */
  18. var fs = require('fs-extra');
  19. var path = require('path');
  20. var PluginInfo = require('./PluginInfo');
  21. var events = require('../events');
  22. function PluginInfoProvider () {
  23. this._cache = {};
  24. this._getAllCache = {};
  25. }
  26. PluginInfoProvider.prototype.get = function (dirName) {
  27. var absPath = path.resolve(dirName);
  28. if (!this._cache[absPath]) {
  29. this._cache[absPath] = new PluginInfo(dirName);
  30. }
  31. return this._cache[absPath];
  32. };
  33. // Normally you don't need to put() entries, but it's used
  34. // when copying plugins, and in unit tests.
  35. PluginInfoProvider.prototype.put = function (pluginInfo) {
  36. var absPath = path.resolve(pluginInfo.dir);
  37. this._cache[absPath] = pluginInfo;
  38. };
  39. // Used for plugin search path processing.
  40. // Given a dir containing multiple plugins, create a PluginInfo object for
  41. // each of them and return as array.
  42. // Should load them all in parallel and return a promise, but not yet.
  43. PluginInfoProvider.prototype.getAllWithinSearchPath = function (dirName) {
  44. var absPath = path.resolve(dirName);
  45. if (!this._getAllCache[absPath]) {
  46. this._getAllCache[absPath] = getAllHelper(absPath, this);
  47. }
  48. return this._getAllCache[absPath];
  49. };
  50. function getAllHelper (absPath, provider) {
  51. if (!fs.existsSync(absPath)) {
  52. return [];
  53. }
  54. // If dir itself is a plugin, return it in an array with one element.
  55. if (fs.existsSync(path.join(absPath, 'plugin.xml'))) {
  56. return [provider.get(absPath)];
  57. }
  58. var subdirs = fs.readdirSync(absPath);
  59. var plugins = [];
  60. subdirs.forEach(function (subdir) {
  61. var d = path.join(absPath, subdir);
  62. if (fs.existsSync(path.join(d, 'plugin.xml'))) {
  63. try {
  64. plugins.push(provider.get(d));
  65. } catch (e) {
  66. events.emit('warn', 'Error parsing ' + path.join(d, 'plugin.xml.\n' + e.stack));
  67. }
  68. }
  69. });
  70. return plugins;
  71. }
  72. module.exports = PluginInfoProvider;