mirror of
https://github.com/avwo/whistle.git
synced 2026-10-18 06:30:45 +08:00
152 lines
4.5 KiB
JavaScript
152 lines
4.5 KiB
JavaScript
var path = require('path');
|
|
var pluginUtil = require('./util');
|
|
var mp = require('./module-paths');
|
|
var config = require('../config');
|
|
var common = require('../util/common');
|
|
|
|
var CUSTOM_PLUGIN_PATH = config.CUSTOM_PLUGIN_PATH;
|
|
var customPluginPaths = config.customPluginPaths || [];
|
|
var notUninstallPluginPaths = config.notUninstallPluginPaths || [];
|
|
var projectPluginPaths = config.projectPluginPaths || [];
|
|
var accountPluginsPath = config.accountPluginsPath || [];
|
|
var PLUGIN_DIR = pluginUtil.PLUGIN_DIR;
|
|
|
|
function readPluginRootList(dir, callback) {
|
|
var roots = [];
|
|
pluginUtil.readDir(dir, function (_, list) {
|
|
if (!list || !list.length) {
|
|
return callback(roots);
|
|
}
|
|
var handleCallback = function () {
|
|
var orgRoots = [];
|
|
roots = roots.filter(function (obj) {
|
|
if (!Array.isArray(obj)) {
|
|
return obj;
|
|
}
|
|
orgRoots.push.apply(orgRoots, obj);
|
|
});
|
|
callback(orgRoots.concat(roots));
|
|
};
|
|
var count = 0;
|
|
list.forEach(function (name, i) {
|
|
if (pluginUtil.isWhistleModule(name)) {
|
|
roots[i] = {
|
|
name: name,
|
|
[PLUGIN_DIR]: dir
|
|
};
|
|
} else if (pluginUtil.isOrgModule(name)) {
|
|
++count;
|
|
pluginUtil.readDir(path.join(dir, name), function (_, list2) {
|
|
if (list2 && list2.length) {
|
|
var orgList;
|
|
list2.forEach(function (pluginName) {
|
|
if (pluginUtil.isWhistleModule(pluginName)) {
|
|
orgList = orgList || [];
|
|
orgList.push({
|
|
org: name,
|
|
name: pluginName,
|
|
[PLUGIN_DIR]: dir
|
|
});
|
|
}
|
|
});
|
|
roots[i] = orgList;
|
|
}
|
|
if (--count === 0) {
|
|
count = -1;
|
|
handleCallback(roots);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
if (count === 0) {
|
|
count = -1;
|
|
handleCallback(roots);
|
|
}
|
|
});
|
|
}
|
|
|
|
function readPluginModules(dir, callback, plugins, isSys) {
|
|
if (typeof dir !== 'string') {
|
|
return callback(plugins);
|
|
}
|
|
readPluginRootList(dir, function (list) {
|
|
var len = list.length;
|
|
if (!len) {
|
|
return callback(plugins);
|
|
}
|
|
list.forEach(function (obj) {
|
|
var dir = obj[PLUGIN_DIR];
|
|
var dirName = obj.org ? obj.org + '/' + obj.name : obj.name;
|
|
pluginUtil.getSysPath(dir, dirName, isSys, function(root) {
|
|
common.getStat(path.join(root, 'package.json'), function (_, stats) {
|
|
if (stats && stats.isFile()) {
|
|
obj.root = root;
|
|
obj.mtime = stats.mtime.getTime();
|
|
}
|
|
if (--len === 0) {
|
|
list.forEach(function (item) {
|
|
var name = pluginUtil.getPluginName(item.name);
|
|
if (item.root && pluginUtil.compparePlugin(dir, item.mtime, plugins[name])) {
|
|
plugins[name] = item;
|
|
}
|
|
});
|
|
callback(plugins);
|
|
}
|
|
});
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
module.exports = function (callback) {
|
|
pluginUtil.readDevPlugins(function(plugins) {
|
|
var result = {};
|
|
var paths = mp.getPaths();
|
|
var count = paths.length;
|
|
if (!count && !Object.keys(plugins).length) {
|
|
return callback(result);
|
|
}
|
|
|
|
var loadPlugins = function (dir, cb) {
|
|
var isAccount = accountPluginsPath.indexOf(dir) !== -1;
|
|
var account = isAccount ? config.account : undefined;
|
|
var isSys = isAccount || CUSTOM_PLUGIN_PATH === dir || customPluginPaths.indexOf(dir) !== -1;
|
|
var isProj = projectPluginPaths.indexOf(dir) !== -1;
|
|
var notUn = notUninstallPluginPaths.indexOf(dir) !== -1;
|
|
|
|
readPluginModules(dir, function () {
|
|
Object.keys(plugins).forEach(function (name) {
|
|
if (pluginUtil.excludePlugin(name)) {
|
|
return;
|
|
}
|
|
var obj = plugins[name];
|
|
var dir = obj[PLUGIN_DIR];
|
|
if (pluginUtil.compparePlugin(dir, obj.mtime, result[name + ':'])) {
|
|
result[name + ':'] = {
|
|
[PLUGIN_DIR]: dir,
|
|
account: account,
|
|
dir: config.whistleName,
|
|
isSys: isSys,
|
|
isProj: isProj || obj.isProj,
|
|
notUn: notUn,
|
|
isDev: obj.isDev,
|
|
path: obj.root,
|
|
mtime: obj.mtime
|
|
};
|
|
}
|
|
});
|
|
cb();
|
|
}, plugins, isSys);
|
|
};
|
|
var index = 0;
|
|
var callbackHandler = function () {
|
|
var dir = paths[++index];
|
|
if (dir) {
|
|
return loadPlugins(dir, callbackHandler);
|
|
}
|
|
callback(result);
|
|
};
|
|
loadPlugins(paths[index], callbackHandler);
|
|
});
|
|
};
|