0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-30 22:34:10 -05:00

Update code style to ES6

This commit is contained in:
Ryan Graham 2017-04-26 13:38:51 +01:00
parent 373c5cb348
commit 977946a319

View file

@ -1,68 +1,66 @@
var Handlebars = require('handlebars') const Handlebars = require('handlebars');
var request = require('request') const request = require('request');
var Logger = require('./logger') const Logger = require('./logger');
var handleNotify = function(metadata, notifyEntry) { const handleNotify = function(metadata, notifyEntry) {
var regex let regex;
if (metadata.name && notifyEntry.packagePattern) { if (metadata.name && notifyEntry.packagePattern) {
regex = new RegExp(notifyEntry.packagePattern, notifyEntry.packagePatternFlags || '') regex = new RegExp(notifyEntry.packagePattern, notifyEntry.packagePatternFlags || '');
if (!regex.test(metadata.name)) { if (!regex.test(metadata.name)) {
return return;
} }
} }
var template = Handlebars.compile(notifyEntry.content) const template = Handlebars.compile(notifyEntry.content);
var content = template( metadata ) const content = template( metadata );
var options = { const options = {body: content};
body: content
}
// provides fallback support, it's accept an Object {} and Array of {} // provides fallback support, it's accept an Object {} and Array of {}
if (notifyEntry.headers && Array.isArray(notifyEntry.headers)) { if (notifyEntry.headers && Array.isArray(notifyEntry.headers)) {
var header = {} const header = {};
notifyEntry.headers.map(function(item) { notifyEntry.headers.map(function(item) {
if (Object.is(item, item)) { if (Object.is(item, item)) {
for (var key in item) { for (const key in item) {
header[key] = item[key] if (item.hasOwnProperty(key)) {
header[key] = item[key];
} }
} }
}) }
options.headers = header; });
} else if (Object.is(notifyEntry.headers, notifyEntry.headers)) { options.headers = header;
options.headers = notifyEntry.headers } else if (Object.is(notifyEntry.headers, notifyEntry.headers)) {
} options.headers = notifyEntry.headers;
}
options.method = notifyEntry.method options.method = notifyEntry.method;
if ( notifyEntry.endpoint ) { if ( notifyEntry.endpoint ) {
options.url = notifyEntry.endpoint options.url = notifyEntry.endpoint;
} }
request(options, function(err, response, body) { request(options, function(err, response, body) {
if (err) { if (err) {
Logger.logger.error( { err: err }, ' notify error: @{err.message}' ) Logger.logger.error({err: err}, ' notify error: @{err.message}' );
} else { } else {
Logger.logger.info({ content: content}, 'A notification has been shipped: @{content}') Logger.logger.info({content: content}, 'A notification has been shipped: @{content}');
if (body) { if (body) {
Logger.logger.debug( { body: body }, ' body: @{body}' ) Logger.logger.debug({body: body}, ' body: @{body}' );
}
} }
}) }
} });
};
module.exports.notify = function(metadata, config) { module.exports.notify = function(metadata, config) {
if (config.notify) { if (config.notify) {
if (config.notify.content) { if (config.notify.content) {
handleNotify(metadata, config.notify) handleNotify(metadata, config.notify);
} } else {
else { for (const key in config.notify) {
for (var key in config.notify) {
if (config.notify.hasOwnProperty(key)) { if (config.notify.hasOwnProperty(key)) {
handleNotify(metadata, config.notify[key]) handleNotify(metadata, config.notify[key]);
} }
} }
} }
} }
} };