This repository has been archived by the owner on Aug 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugin-parser.js
55 lines (48 loc) · 1.74 KB
/
plugin-parser.js
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
var path = require("path");
var fs = require("fs");
var chokidar = require('chokidar'); // node fs.watch doesn't work recursively
var plugins = [];
var pluginModifications = [];
chokidar.watch(path.resolve(__dirname + path.sep + 'plugins'),
{ignored: /[\/\\]\./}).on('all', function(event, path) {
pluginModifications.push(event);
});
exports.loadPlugins = function(callback) {
var pluginFolder = path.resolve(__dirname + path.sep + 'plugins');
if (plugins.length == 0 || pluginModifications.length != 0) {
// cache miss
plugins = []; // reset in case changes made to filesystem
pluginModifications = [];
fs.readdir(pluginFolder, function(err, files) {
if (err) {
console.log(err);
return;
}
files.forEach(function(f, index, array) {
var fullFile = pluginFolder + path.sep + f;
fs.stat(fullFile, function(err, stats){
if (stats.isDirectory()) {
var mainFile = path.resolve(fullFile) + path.sep + 'main.json';
fs.access(mainFile, fs.R_OK, function(err) {
if (!err) {
var json = loadJson(mainFile);
plugins.push(json);
callback(json);
}
});
}
});
});
});
} else {
// cache hit
for (var i = 0; i < plugins.length; i++) {
callback(plugins[i]);
}
}
};
var loadJson = function(file) {
data = fs.readFileSync(file, 'utf-8');
info = JSON.parse(data);
return info;
};