Skip to content
This repository has been archived by the owner on Jun 2, 2024. It is now read-only.

Commit

Permalink
Merge pull request #369 from cnpm/publish-with-tag
Browse files Browse the repository at this point in the history
support `npm publish --tag beta`. fix #366
  • Loading branch information
dead-horse committed Jul 10, 2014
2 parents 0b59547 + a6ac632 commit 74585f0
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 11 deletions.
49 changes: 40 additions & 9 deletions controllers/registry/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,23 @@ var downloadAsReadStream = require('../utils').downloadAsReadStream;
*/
exports.show = function* (next) {
var name = this.params.name;
var modifiedTime = yield* Module.getLastModified(name);
debug('show %s, last modified: %s', name, modifiedTime);
var rs = yield [
Module.getLastModified(name),
Module.listTags(name)
];
var modifiedTime = rs[0];
var tags = rs[1];
debug('show %s, last modified: %s, tags: %j', name, modifiedTime, tags);
if (modifiedTime) {
// find out the latest modfied time
// because update tags only modfied tag, wont change module gmt_modified
for (var i = 0; i < tags.length; i++) {
var tag = tags[i];
if (tag.gmt_modified > modifiedTime) {
modifiedTime = tag.gmt_modified;
}
}

// use modifiedTime as etag
this.set('ETag', '"' + modifiedTime.getTime() + '"');

Expand All @@ -63,15 +77,13 @@ exports.show = function* (next) {
}

var r = yield [
Module.listTags(name),
Module.listByName(name),
ModuleStar.listUsers(name),
packageService.listMaintainers(name),
];
var tags = r[0];
var rows = r[1];
var users = r[2];
var maintainers = r[3];
var rows = r[0];
var users = r[1];
var maintainers = r[2];

debug('show %s got %d rows, %d tags, %d star users, maintainers: %j',
name, rows.length, tags.length, users.length, maintainers);
Expand Down Expand Up @@ -578,8 +590,17 @@ exports.addPackageAndDist = function *(next) {
tags.push([t, distTags[t]]);
}

debug('%s addPackageAndDist %s:%s, attachment size: %s, maintainers: %j',
username, name, version, attachment.length, versionPackage.maintainers);
if (tags.length === 0) {
this.status = 400;
this.body = {
error: 'invalid',
reason: 'dist-tags should not be empty'
};
return;
}

debug('%s addPackageAndDist %s:%s, attachment size: %s, maintainers: %j, distTags: %j',
username, name, version, attachment.length, versionPackage.maintainers, distTags);

var exists = yield Module.get(name, version);
var shasum;
Expand Down Expand Up @@ -617,6 +638,16 @@ exports.addPackageAndDist = function *(next) {
return;
}

if (!distTags.latest) {
// need to check if latest tag exists or not
var latest = yield Module.getByTag(name, version);
if (!latest) {
// auto add latest
tags.push(['latest', tags[0][1]]);
debug('auto add latest tag: %j', tags);
}
}

shasum = crypto.createHash('sha1');
shasum.update(tarballBuffer);
shasum = shasum.digest('hex');
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cnpmjs.org",
"version": "0.7.0",
"version": "0.7.1",
"description": "Private npm registry and web for Enterprise, base on MySQL and Simple Store Service",
"main": "index.js",
"scripts": {
Expand Down
57 changes: 56 additions & 1 deletion test/controllers/registry/module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -687,8 +687,10 @@ describe('controllers/registry/module.test.js', function () {
});
});

var pkgJSON = fs.readFileSync(path.join(fixtures, 'package_and_tgz.json'), 'utf8');

it('should version_error when versions missing', function (done) {
var pkg = require(path.join(fixtures, 'package_and_tgz.json'));
var pkg = JSON.parse(pkgJSON);
delete pkg.versions;
request(app)
.put('/' + pkg.name)
Expand All @@ -703,6 +705,59 @@ describe('controllers/registry/module.test.js', function () {
done();
});
});

it('should 400 when dist-tags empty', function (done) {
var pkg = JSON.parse(pkgJSON);
pkg['dist-tags'] = {};
request(app)
.put('/' + pkg.name)
.set('authorization', baseauth)
.send(pkg)
.expect(400, function (err, res) {
should.not.exist(err);
res.body.should.eql({
error: 'invalid',
reason: 'dist-tags should not be empty'
});
done();
});
});

it('should publish with beta tag addPackageAndDist()', function (done) {
var pkg = JSON.parse(pkgJSON);
pkg.name = 'publish-with-beta-tag';
var version = Object.keys(pkg.versions)[0];
pkg['dist-tags'] = {
beta: version
};
request(app)
.del('/' + pkg.name + '/-rev/1')
.set('authorization', baseauth)
.end(function (err, res) {
should.not.exist(err);

request(app)
.put('/' + pkg.name)
.set('authorization', baseauth)
.send(pkg)
.expect(201, function (err, res) {
should.not.exist(err);
res.body.should.have.keys('ok', 'rev');
res.body.ok.should.equal(true);
// should auto set latest
request(app)
.get('/' + pkg.name)
.expect(200, function (err, res) {
should.not.exist(err);
res.body['dist-tags'].should.eql({
beta: version,
latest: version
});
done();
});
});
});
});
});

describe('GET /-/all', function () {
Expand Down

0 comments on commit 74585f0

Please sign in to comment.