diff --git a/bin/pubmail.js b/bin/pubmail.js index 8969e34..f0521fa 100644 --- a/bin/pubmail.js +++ b/bin/pubmail.js @@ -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); diff --git a/lib/mailer.js b/lib/mailer.js index 2aa1935..6e6c5a5 100644 --- a/lib/mailer.js +++ b/lib/mailer.js @@ -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 { @@ -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}`, diff --git a/lib/template.js b/lib/template.js index f4954bf..1bd7401 100644 --- a/lib/template.js +++ b/lib/template.js @@ -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; @@ -14,8 +14,6 @@ Author: ${authorName}${authorEmail && ` <${authorEmail}>`} Publisher: ${publisherName}${publisherEmail && ` <${publisherEmail}>`} Homepage: ${homepage} Repository: ${repo} - -${changes} - +${changes ? `\n${changes}\n` : ''} ${cool()}`; }; diff --git a/lib/utils.js b/lib/utils.js index 7c4c767..911b0a2 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -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) { @@ -38,8 +90,13 @@ function git2http(url) { return Url.format(parsed); } + export default { - git2http + gitHeads, + + git2http, + + link2diff }; \ No newline at end of file