Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

semver 和 publish 的调整 #81

Merged
merged 12 commits into from
Dec 18, 2014
7 changes: 6 additions & 1 deletion models/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ Package.prototype = {
},

delete: function() {
fs.removeSync(path.join(CONFIG.wwwroot, 'repository', this.name, this.version));
var now = moment().format('YYYY-MM-DDTHH:mm:ssZ');
this['unpublished'] = {
time: now
};
fs.outputJsonSync(this.datafile(), this);
fs.removeSync(path.join(CONFIG.wwwroot, 'repository', this.name, this.version, this.filename));
return this;
}
};
Expand Down
7 changes: 6 additions & 1 deletion models/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ Project.prototype = {
this.getVersions().forEach(function(version) {
that.remove(version);
});
fs.removeSync(path.join(CONFIG.wwwroot, 'repository', this.name));

var now = moment().format('YYYY-MM-DDTHH:mm:ssZ');
this['unpublished'] = {
time: now
};
fs.outputJsonSync(this.datafile(), this);
return this;
},

Expand Down
7 changes: 6 additions & 1 deletion routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var fs = require('fs');
var path = require('path');
var request = require('request');
var elastical = require('elastical');
var semver = require('semver');
var client = new elastical.Client();
var badge = require('../lib/badge');
var anonymous = CONFIG.authorize.type === 'anonymous';
Expand Down Expand Up @@ -153,7 +154,11 @@ exports.project = function(req, res, next) {

exports.package = function(req, res, next) {
var name = req.params.name;
var version = req.params.version;
var project = new Project({
name: req.params.name
});
var version = semver.maxSatisfying(Object.keys(project.packages), req.params.version);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个方法其实已经同时支持了 ~ ^ * 1.x >1.0.0 等各种情况吧?会不会太激进了。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

版本的问题我觉得可以让使用者自己判断,信任一个库才使用他,如果一个库使用了 * 这样的版本范围,则可以选择不用他。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

内部组件(arale, alipay, anima)可以约定下只能用 patch range ~1.1.x。另外,内部工具在构建时候可以判断下范围是否过大,然后给予提示。


var p = new Package({
name: name,
version: version
Expand Down
18 changes: 12 additions & 6 deletions routes/repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var fs = require('fs-extra');
var path = require('path');
var tempfile = require('tempfile');
var tar = require('tarball-extract');
var semver = require('semver');
var hook = require('../lib/hook');
var history = require('../lib/history');
var elastical = require('elastical');
Expand Down Expand Up @@ -68,9 +69,14 @@ exports.project = {
var Cache = {};
exports.package = {
get: function(req, res) {
var project = new Project({
name: req.params.name
});
var version = semver.maxSatisfying(Object.keys(project.packages), req.params.version);

var p = new Package({
name: req.params.name,
version: req.params.version
version: version
});
if (!p.md5) {
abortify(res, { code: 404 });
Expand Down Expand Up @@ -142,25 +148,23 @@ exports.package = {

Cache.package = new Package(data);

var force = req.headers['x-yuan-force'];
if(Cache.package.md5 && !force) {
if(Cache.package.md5) {
return abortify(res, { code: 444 });
}

var isNewProject;
Cache.project = new Project(data);
var isNewProject = !Cache.project['created_at'];
if (isNewProject) {
data.owners = [data.publisher];
}
Cache.project.update(data);
delete Cache.project.unpublished;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

创建时为什么需要删除 unpublished ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果用户在 unpublish 整个 project 后,再发布一个未发过的新版本的 package,则把 project 的 unpublished 去掉。

明白了。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Project unpublish 后重新 publish,需删除 unpublished 信息。

if (isNewProject) {
hook.emit('create:project', Cache.project, data.publisher);
}
res.send(200, {});
},
put: function(req, res) {
var data = req.body;
var package = Cache.package;
var project = Cache.project;
if (!package) {
Expand Down Expand Up @@ -361,7 +365,7 @@ function abortify(res, options) {
406: 'Not acceptable.',
415: 'Unsupported media type.',
426: 'Upgrade required.',
444: 'Force option required.'
444: 'Cannot modify pre-existing version.'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pre-existing -> existed ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

从 npm 上 copy 来的。。两个意思差不多。

};
message = options.message || msgs[code];
res.send(code, {
Expand All @@ -370,3 +374,5 @@ function abortify(res, options) {
message: message
});
}


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

多余的空行。。。