summaryrefslogtreecommitdiff
path: root/src/static/js/pluginfw/installer.js
blob: e560261676ce865276a3d948a8ece9b44a2cc117 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
var plugins = require("ep_etherpad-lite/static/js/pluginfw/plugins");
var hooks = require("ep_etherpad-lite/static/js/pluginfw/hooks");
var npm = require("npm");

var npmIsLoaded = false;
var withNpm = function (npmfn) {
  if(npmIsLoaded) return npmfn();
  npm.load({}, function (er) {
    if (er) return npmfn(er);
    npmIsLoaded = true;
    npm.on("log", function (message) {
      console.log('npm: ',message)
    });
    npmfn();
  });
}

var tasks = 0
function wrapTaskCb(cb) {
  tasks++
  return function() {
    cb && cb.apply(this, arguments);
    tasks--;
    if(tasks == 0) onAllTasksFinished();
  }
}
function onAllTasksFinished() {
  hooks.aCallAll("restartServer", {}, function () {});
}

exports.uninstall = function(plugin_name, cb) {
  cb = wrapTaskCb(cb);
  withNpm(function (er) {
    if (er) return cb && cb(er);
    npm.commands.uninstall([plugin_name], function (er) {
      if (er) return cb && cb(er);
      hooks.aCallAll("pluginUninstall", {plugin_name: plugin_name}, function (er, data) {
        if (er) return cb(er);
        plugins.update(cb);
      });
    });
  });
};

exports.install = function(plugin_name, cb) {
  cb = wrapTaskCb(cb)
  withNpm(function (er) {
    if (er) return cb && cb(er);
    npm.commands.install([plugin_name], function (er) {
      if (er) return cb && cb(er);
      hooks.aCallAll("pluginInstall", {plugin_name: plugin_name}, function (er, data) {
        if (er) return cb(er);
        plugins.update(cb);
      });
    });
  });
};

exports.availablePlugins = null;
var cacheTimestamp = 0;

exports.getAvailablePlugins = function(maxCacheAge, cb) {
  withNpm(function (er) {
    if (er) return cb && cb(er);
    if(exports.availablePlugins && maxCacheAge && Math.round(+new Date/1000)-cacheTimestamp <= maxCacheAge) {
      return cb && cb(null, exports.availablePlugins)
    }
    npm.commands.search(['ep_'], /*silent?*/true, function(er, results) {
      if(er) return cb && cb(er);
      exports.availablePlugins = results;
      cacheTimestamp = Math.round(+new Date/1000);
      cb && cb(null, results)
    })
  });
};


exports.search = function(searchTerm, maxCacheAge, cb) {
  exports.getAvailablePlugins(maxCacheAge, function(er, results) {
    if(er) return cb && cb(er);
    var res = {};
    if (searchTerm)
      searchTerm = searchTerm.toLowerCase();
    for (var pluginName in results) { // for every available plugin
      if (pluginName.indexOf(plugins.prefix) != 0) continue; // TODO: Also search in keywords here!

      if(searchTerm && !~pluginName.toLowerCase().indexOf(searchTerm) && !~results[pluginName].description.toLowerCase().indexOf(searchTerm)) continue;
      res[pluginName] = results[pluginName];
    }
    cb && cb(null, res)
  })
};