Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
module.exports = function() {
// install predefined plugins
return Promise.each(predefinedPlugins, function(pluginName) {
return db.plugins.exists(pluginName)
.then(function(exists) {
if (!exists) {
console.log('INSTALLING ' + pluginName);
return plugins.install(pluginName); }
});
})
// get all registered plugins
.then(db.plugins.all)
// aggregate template hooks and template dirs
.each(function(dbPlugin) {
var plugin = plugins.load(dbPlugin.name);
var hooks = plugin.templateHooks;
var templatePath = path.join(pluginsPath, dbPlugin.name, 'templates');
var resourcePath = path.join(pluginsPath, dbPlugin.name, 'resources');
var discover = function(paths, matcher, logger) {
var discovered = [];
return Promise.each(paths, function(location){
// Recurse through the api directory and collect the models
return dive(location, matcher, logger)
.then(function(results){
logger.log("debug","Flattening results");
return _.flatten(results, true);
})
.filter(function(value){
return value !== false
})
.then(function(results){
logger.log("debug","Assigning filtered results to discover: "+results);
return discovered = results;
})
.catch(function(err) {
logger.log("error", err);
cacheContributors(files, commitAuthors) {
return Promise.each(files, file => {
return execute(`git --no-pager log --follow --no-merges --format="%H" "${file.path}"`, {
cwd: this.path, stdout: null,
}).then(stdout => {
const output = stdout.toString().replace(/\n$/, '');
const shas = output.split('\n').reverse();
const contributors = [];
for (const sha of shas) {
const author = commitAuthors[sha];
if (author && !contributors.find(contributor => contributor.login === author.login)) {
contributors.push(author);
}
}
file.contributors = contributors;
});
});
}
.then(function() {
return Promise.each(data.groups, function(group) {
user.groups().attach(group);
});
});
})
this.sub.on('message', function(channel, msg){
var event = channel.slice(self.config.prefix.length);
try{
msg = JSON.parse(msg);
}
catch(err){
self.logger.error('invalid message %s', msg);
}
if(!self.eventListeners.hasOwnProperty(event)){
return P.resolve();
}
P.each(self.eventListeners[event], function(listener){
return P.try(function(){
self.logger.debug('onEvent %s %j', event, msg);
return listener.apply(null, msg);
})
.catch(function(err){
self.logger.error(err.stack);
});
});
});
.then(function (names) {
config.out.status();
return Promise.each(names, function (name) {
config.out.status("Reading files", name);
var fileLocation = URL.resolve(config.location || '', Location.fromPath(name));
var relativeLocation = URL.relative(location, fileLocation);
return config.fs.read(name)
.then(function (content) {
hashBuilder.update(relativeLocation);
hashBuilder.update(content);
var file = new File({
fs: config.fs,
location: fileLocation,
relativeLocation: relativeLocation,
package: package,
path: name
});
package.files[relativeLocation] = file;
config.files[fileLocation] = file;
function buildAllColorStops() {
return Promise.each(colorStopNames, function(colorStop) {
return compile(colorStop)
.then(function(cssString) {
postprocess(cssString, colorStop)
})
})
}
stopForwarders: () => {
if (!_servers.length) return Promise.resolve()
output.success('Halting all port forwarding')
return Promise.each(_servers, server => {
return new Promise(resolve => {
server.unref()
try {
server.close(resolve)
} catch (e) {
resolve()
}
})
})
.then(() => _servers = [])
}
return run(this, function * () {
const sockets = yield user.userState.getSocketsToInstance()
yield Promise.each(_.toPairs(sockets), ([socket, instance]) => {
if (instance === this.instanceUID) {
if (!this.transport.getSocket(socket)) {
return user.userState.removeSocket(socket)
}
}
})
const data = yield user.userState.getSocketsToRooms()
const args = _.values(data)
const rooms = _.intersection(...args)
return Promise.each(rooms, roomName => {
return this.state.getRoom(roomName)
.then(room => room.roomState.hasInList('userlist', userName))
.then(isPresent => isPresent ? null : user.removeFromRoom(roomName))
.catchReturn()
})
})
}
callHook(hook) {
if (!includes(ServiceManager.allowedHooks, hook)) {
throw new Error(`Hook ${hook} does not exist.`);
}
let args = toArray(arguments).slice(1);
let hooks = this.hooks[hook] || {};
return Promise.each(Object.keys(hooks), (serviceName) => {
let fn = hooks[serviceName];
return fn.apply(this.services[serviceName], args);
});
}