From 0b7997e39bfe62b80f65c455740f56662dbe0b38 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Tue, 4 Mar 2014 19:59:55 +0800 Subject: [PATCH] add readmeFilename and missing created, modified time. #235 --- controllers/registry/module.js | 35 ++++++++++++++++++++---- test/controllers/registry/module.test.js | 10 +++++-- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/controllers/registry/module.js b/controllers/registry/module.js index 79249cb67..cbaf2fbdc 100644 --- a/controllers/registry/module.js +++ b/controllers/registry/module.js @@ -81,6 +81,8 @@ exports.show = function *(next) { var versions = {}; var times = {}; var attachments = {}; + var createdTime = null; + var modifiedTime = null; for (var i = 0; i < rows.length; i++) { var row = rows[i]; if (row.version === 'next') { @@ -91,13 +93,29 @@ exports.show = function *(next) { common.setDownloadURL(pkg, this); pkg._cnpm_publish_time = row.publish_time; versions[pkg.version] = pkg; - times[pkg.version] = row.publish_time ? new Date(row.publish_time) : row.gmt_modified; + var t = times[pkg.version] = row.publish_time ? new Date(row.publish_time) : row.gmt_modified; if ((!distTags.latest && !latestMod) || distTags.latest === row.version) { latestMod = row; readme = pkg.readme; } delete pkg.readme; + + if (!createdTime || t < createdTime) { + createdTime = t; + } + if (!modifiedTime || t > modifiedTime) { + modifiedTime = t; + } + } + + var ts = { + modified: modifiedTime, + created: createdTime, + }; + for (var t in times) { + ts[t] = times[t]; } + times = ts; if (!latestMod) { latestMod = nextMod || rows[0]; @@ -112,21 +130,28 @@ exports.show = function *(next) { rev = String(nextMod.id); } + var pkg = latestMod.package; + var info = { _id: name, _rev: rev, name: name, - description: latestMod.package.description, + description: pkg.description, "dist-tags": distTags, - maintainers: latestMod.package.maintainers, + maintainers: pkg.maintainers, time: times, - author: latestMod.package.author, - repository: latestMod.package.repository, + author: pkg.author, + repository: pkg.repository, versions: versions, readme: readme, _attachments: attachments, }; + info.readmeFilename = pkg.readmeFilename; + info.homepage = pkg.homepage; + info.bugs = pkg.bugs; + info.license = pkg.license; + debug('show module %s: %s, latest: %s', name, rev, latestMod.version); this.body = info; diff --git a/test/controllers/registry/module.test.js b/test/controllers/registry/module.test.js index 4871b2bb2..933ec5c64 100644 --- a/test/controllers/registry/module.test.js +++ b/test/controllers/registry/module.test.js @@ -79,9 +79,15 @@ describe('controllers/registry/module.test.js', function () { etag = res.headers.etag; res.body.should.have.keys('_id', '_rev', 'name', 'description', 'versions', 'dist-tags', 'readme', 'maintainers', - 'time', 'author', 'repository', '_attachments'); + 'time', 'author', 'repository', '_attachments', + 'readmeFilename', 'homepage', 'bugs', 'license'); res.body.name.should.equal('cnpmjs.org'); - res.body.versions[Object.keys(res.body.versions)[0]].dist.tarball.should.include('/cnpmjs.org/download'); + res.body.versions[Object.keys(res.body.versions)[0]] + .dist.tarball.should.include('/cnpmjs.org/download'); + res.body.time.should.have.property('modified'); + res.body.time.modified.should.be.a.String; + res.body.time.should.have.property('created'); + res.body.time.created.should.be.a.String; done(); }); });