Skip to content

Commit

Permalink
Refactor to improve readability.
Browse files Browse the repository at this point in the history
  • Loading branch information
totherik committed Feb 23, 2015
1 parent 86669d7 commit ca7937e
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 42 deletions.
3 changes: 1 addition & 2 deletions bin/pubmail.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ socket.on('error', (err) => {
log.error(['socket'], err);
});

socket.on('message', function(type/*, identity*/, event) {
type = type.toString();
socket.on('message', function(_/*, identity*/, event) {
event = JSON.parse(event.toString());

log.info(['publish'], event.id);
Expand Down
41 changes: 6 additions & 35 deletions lib/mailer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Semver from 'semver';
import Nodemailer from 'nodemailer';
import template from './template';
import { git2http, compare } from './utils';
import Utils from './utils';


export default class Mailer {
Expand All @@ -16,47 +15,19 @@ export default class Mailer {
};
}

send(pkg, cb) {
let { id: name, doc } = pkg;
send({ id: name, doc }, cb) {
let { 'dist-tags': { latest: version } } = doc;
let {
homepage,
repository = { },
description,
author,
_npmUser: publisher,
gitHead: currentGitHead
_npmUser: publisher
} = doc.versions[version];

// Generate link to changes since last publish.
let changes = undefined;
let { url: repo = 'n/a' } = repository;

// Hamfisted way of ensuring the repo is a GitHub repo so that we generate
// correct links. These links wouldn't make sense outside GitHub anyway.
if (repository.type === 'git' && repo.indexOf('github') !== -1 && currentGitHead) {
// Do our best to convert the provided URL to an SSL git checkout URL.
repo = git2http(repo);
if (typeof repo === 'string') {
// Remove the extension to (hopefully) derive a GitHub URL.
repo = repo.replace(/\.git$/, '');

// Grab the keys of all published versions and sort using a
// semver comparator. The result *should* be a correctly ordered
// version array, from which we can grab the previous version. It
// may not make sense to grab versions across major release boundaries,
// but this can be fine-tuned in the future, if necessary.
let versions = Object.keys(doc.versions).sort(Semver.compare);
let previous = versions[versions.indexOf(version) - 1];
let { gitHead: previousGitHead } = (doc.versions[previous] || {});

if (previousGitHead && previousGitHead !== currentGitHead) {
changes = `${repo}/compare/${previousGitHead}...${currentGitHead}`;
} else {
changes = `${repo}/commit/${currentGitHead}`;
}
}
}
let { url: repo } = repository;
let gitHeads = Utils.gitHeads(version, doc.versions);
let changes = Utils.link2diff(repository, gitHeads);

let message = Object.assign({
subject: `npm publish ${name}@${version}`,
Expand Down
6 changes: 2 additions & 4 deletions lib/template.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import cool from 'cool-ascii-faces';


export default ({ name, version, description, author = {}, publisher = {}, homepage = 'n/a', repo = 'n/a', changes = '' }) => {
export default ({ name, version, description, author = {}, publisher = {}, homepage = 'Not set.', repo = 'Not set.', changes = '' }) => {

let { name: authorName = 'anonymous', email: authorEmail = '' } = author;
let { name: publisherName = 'anonymous', email: publisherEmail = '' } = publisher;
Expand All @@ -14,8 +14,6 @@ Author: ${authorName}${authorEmail && ` <${authorEmail}>`}
Publisher: ${publisherName}${publisherEmail && ` <${publisherEmail}>`}
Homepage: ${homepage}
Repository: ${repo}
${changes}
${changes ? `\n${changes}\n` : ''}
${cool()}`;
};
59 changes: 58 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,56 @@
import Url from 'url';
import Semver from 'semver';


function gitHeads(version, versions) {
if (!(version in versions)) {
return;
}

// Grab the keys of all published versions and sort using a
// semver comparator. The result *should* be a correctly ordered
// version array, from which we can grab the previous version. It
// may not make sense to grab versions across major release boundaries,
// but this can be fine-tuned in the future, if necessary.
let keys = Object.keys(versions).sort(Semver.compare);
let previous = keys[keys.indexOf(version) - 1];

let { gitHead: currentGitHead } = versions[version];
let { gitHead: previousGitHead } = (versions[previous] || {});

return { previousGitHead, currentGitHead };
}


function link2diff(repository = {}, gitHead = {}) {
let changes;

let { type = '', url = '' } = repository;
if (type !== 'git' || url.indexOf('github') === -1) {
// Hamfisted way of ensuring the repo is a GitHub repo so that we generate
// correct links. These links wouldn't make sense outside GitHub anyway.
return undefined;
}

let { currentGitHead, previousGitHead } = gitHead;
if (currentGitHead) {
// Do our best to convert the provided URL to an SSL git checkout URL.
url = git2http(url);

if (typeof url === 'string') {
// Remove the extension to (hopefully) derive a GitHub URL.
url = url.replace(/\.git$/, '');

if (previousGitHead && previousGitHead !== currentGitHead) {
changes = `${url}/compare/${previousGitHead}...${currentGitHead}`;
} else {
changes = `${url}/commit/${currentGitHead}`;
}
}
}

return changes;
}


function git2http(url) {
Expand Down Expand Up @@ -38,8 +90,13 @@ function git2http(url) {
return Url.format(parsed);
}


export default {

git2http
gitHeads,

git2http,

link2diff

};

0 comments on commit ca7937e

Please sign in to comment.