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

Commit

Permalink
refactor remove module, fixed #186
Browse files Browse the repository at this point in the history
1. list all the modules, find which versions to be removed and which to
be remained.
2. remove all the modules need to be removed
3. list all the tags, find which tags need to be removed and remove
them.
4. if the latest tag removed, need to generate a new latest tag.
  • Loading branch information
dead-horse committed Feb 13, 2014
1 parent 9d2a649 commit 46eba8d
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 7 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cnpmjs.org
=======

[![Build Status](https://secure.travis-ci.org/cnpm/cnpmjs.org.png)](http://travis-ci.org/cnpm/cnpmjs.org) [![Coverage Status](https://coveralls.io/repos/cnpm/cnpmjs.org/badge.png)](https://coveralls.io/r/cnpm/cnpmjs.org)[![Dependency Status](https://gemnasium.com/cnpm/cnpmjs.org.png)](https://gemnasium.com/cnpm/cnpmjs.org)
[![Build Status](https://secure.travis-ci.org/cnpm/cnpmjs.org.png)](http://travis-ci.org/cnpm/cnpmjs.org) [![Coverage Status](https://coveralls.io/repos/cnpm/cnpmjs.org/badge.png)](https://coveralls.io/r/cnpm/cnpmjs.org) [![Dependency Status](https://gemnasium.com/cnpm/cnpmjs.org.png)](https://gemnasium.com/cnpm/cnpmjs.org)

[![NPM](https://nodei.co/npm/cnpmjs.org.png?downloads=true&stars=true)](https://nodei.co/npm/cnpmjs.org/)

Expand All @@ -28,6 +28,11 @@ $ npm install
$ node dispatch.js
```

## Guide

* [How to deploy cnpmjs.org](https://github.com/cnpm/cnpmjs.org/wiki/Deploy)
* [NFS guide](https://github.com/cnpm/cnpmjs.org/wiki/NFS-Guide)

## Authors

```bash
Expand Down
70 changes: 65 additions & 5 deletions controllers/registry/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -665,12 +665,14 @@ exports.removeWithVersions = function (req, res, next) {
var ep = eventproxy.create();
ep.fail(next);

// step1: list all the versions
Module.listByName(name, ep.doneLater('list'));
ep.once('list', function (mods) {
if (!mods || !mods.length) {
return next();
}

// step2: check permission
var firstMod = mods[0];
if (!common.isMaintainer(req, firstMod.package.maintainers) || firstMod.name !== name) {
return res.json(403, {
Expand All @@ -679,20 +681,78 @@ exports.removeWithVersions = function (req, res, next) {
});
}

// step3: calculate which versions need to remove and
// which versions need to remain
var removeVersions = [];
var removeVersionMaps = {};
var remainVersions = [];

for (var i = 0; i < mods.length; i++) {
var v = mods[i].version;
if (v !== 'next' && !versions[v]) {
if (v === 'next') {
continue;
}
if (!versions[v]) {
removeVersions.push(v);
removeVersionMaps[v] = true;
} else {
remainVersions.push(v);
}
}

if (!removeVersions.length) {
return res.json(201, {ok: true});
}
Module.removeByNameAndVersions(name, removeVersions, ep.done(function () {
res.json(201, {ok: true});
debug('no versions need to remove');
return res.json(201, { ok: true });
}
debug('remove versions: %j, remain versions: %j', removeVersions, remainVersions);

// step 4: remove all the versions which need to remove
Module.removeByNameAndVersions(name, removeVersions, ep.done('removeModules'));

// step5: list all the tags
Module.listTags(name, ep.done(function (tags) {
var removeTags = [];
var latestRemoved = false;
tags.forEach(function (tag) {
// this tag need be removed
if (removeVersionMaps[tag.version]) {
removeTags.push(tag.id);
if (tag.tag === 'latest') {
latestRemoved = true;
}
}
});

if (!removeTags.length) {
debug('no tag need be remove');
ep.emit('removeTags');
ep.emit('latest');
return;
}

debug('remove tags: %j', removeTags);
// step 6: remove all the tags which need to remove
Module.removeTagsByIds(removeTags, ep.done('removeTags'));

// step 7: check if latest tag being removed.
// need generate a new latest tag
if (!latestRemoved) {
ep.emit('latest');
} else {
debug('latest tags removed, generate a new latest tag with new version: %s',
remainVersions[0]);
ep.emit('newLatestVersion', remainVersions[0]);
}
}));
});

ep.all('newLatestVersion', 'removeTags', function (version) {
Module.addTag(name, 'latest', version, ep.done('latest'));
});

ep.all('removeModules', 'removeTags', 'latest', function () {
return res.json(201, { ok: true });
});
};


Expand Down
7 changes: 6 additions & 1 deletion proxy/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,12 @@ exports.removeTags = function (name, callback) {
mysql.query(DELETE_TAGS_SQL, [name], callback);
};

var SELECT_ALL_TAGS_SQL = 'SELECT tag, version, gmt_modified, module_id FROM tag WHERE name=?;';
var DELETE_TAGS_BY_IDS_SQL = 'DELETE FROM tag WHERE id in (?)';
exports.removeTagsByIds = function (ids, callback) {
mysql.query(DELETE_TAGS_BY_IDS_SQL, [ids], callback);
};

var SELECT_ALL_TAGS_SQL = 'SELECT id, tag, version, gmt_modified, module_id FROM tag WHERE name=?;';

exports.listTags = function (name, callback) {
mysql.query(SELECT_ALL_TAGS_SQL, [name], callback);
Expand Down

0 comments on commit 46eba8d

Please sign in to comment.