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

Badge version image #429

Merged
merged 4 commits into from
Aug 20, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 3 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ cnpmjs.org
[![node version][node-image]][node-url]
[![npm download][download-image]][download-url]

[npm-image]: https://img.shields.io/npm/v/cnpmjs.org.svg?style=flat-square
[npm-url]: https://npmjs.org/package/cnpmjs.org
[npm-image]: http://cnpmjs.org/badge/v/cnpmjs.org.svg?style=flat-square
[npm-url]: http://cnpmjs.org/package/cnpmjs.org
[travis-image]: https://img.shields.io/travis/cnpm/cnpmjs.org.svg?style=flat-square
[travis-url]: https://travis-ci.org/cnpm/cnpmjs.org
[coveralls-image]: https://img.shields.io/coveralls/cnpm/cnpmjs.org.svg?style=flat-square
Expand Down Expand Up @@ -54,6 +54,7 @@ to extend `npm` with more features(`sync` command, [gzip](https://github.com/npm
And it easy to wrap for your own registry which build with `cnpmjs.org`.
* **Compatible with NPM client**: you can use the origin NPM client with `cnpmjs.org`,
only need to change the registry in config. Even include manual synchronization (through `install` command).
* **Version badge**: base on [shields.io](http://shields.io/) ![cnpm-badge](http://cnpmjs.org/badge/v/cnpmjs.org.svg?style=flat-square)

## Getting Start

Expand Down Expand Up @@ -110,24 +111,6 @@ $ node --harmony_generators dispatch.js

Tips: make sure your code is following the [node-style-guide](https://github.com/felixge/node-style-guide).

## Authors

```bash
$ git summary

project : cnpmjs.org
repo age : 4 months ago
commits : 472
active : 167 days
files : 104
authors :
272 fengmk2 57.6%
195 dead_horse 41.3%
2 4simple 0.4%
2 Stanley Zheng 0.4%
1 Alsotang 0.2%
```

## License

(The MIT License)
Expand Down
6 changes: 4 additions & 2 deletions config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ var config = {
// use for koa-limit module as storage
redis: null,

// package tarball store in qn by default
// qiniu cdn: http://www.qiniu.com/, it free for dev.
// package tarball store in local filesystem by default
nfs: require('fs-cnpm')({
dir: path.join(root, '.tmp', 'dist')
}),
Expand Down Expand Up @@ -187,6 +186,9 @@ var config = {
syncConcurrency: 1,
// sync interval, default is 10 minutes
syncInterval: '10m',

// badge subject on http://shields.io/
badgeSubject: 'cnpm',
};

// load config/config.js, everything in config.js will cover the same key in index.js
Expand Down
6 changes: 3 additions & 3 deletions controllers/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ exports.total = function (name, callback) {
name = null;
}
var end = moment();
var start = end.clone().subtract('months', 1).startOf('month');
var lastday = end.clone().subtract('days', 1).format('YYYY-MM-DD');
var lastweekStart = end.clone().subtract('weeks', 1).startOf('week');
var start = end.clone().subtract(1, 'months').startOf('month');
var lastday = end.clone().subtract(1, 'days').format('YYYY-MM-DD');
var lastweekStart = end.clone().subtract(1, 'weeks').startOf('week');
var lastweekEnd = lastweekStart.clone().endOf('week').format('YYYY-MM-DD');
var lastmonthEnd = start.clone().endOf('month').format('YYYY-MM-DD');
var thismonthStart = end.clone().startOf('month').format('YYYY-MM-DD');
Expand Down
47 changes: 47 additions & 0 deletions controllers/web/badge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**!
* cnpmjs.org - controllers/web/badge.js
*
* Copyright(c) fengmk2 and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <[email protected]> (http://fengmk2.github.com)
*/

'use strict';

/**
* Module dependencies.
*/

var config = require('../../config');
var Module = require('../../proxy/module');

exports.version = function* (next) {
var color = 'lightgrey';
var version = 'invalid';
var name = this.params[0];
var latestTag = yield* Module.getTag(name, 'latest');
if (latestTag) {
version = latestTag.version;
if (/^0\.0\./.test(version)) {
// <0.1.0 & >=0.0.0
color = 'red';
} else if (/^0\./.test(version)) {
// <1.0.0 & >=0.1.0
color = 'green';
} else {
// >=1.0.0
color = 'blue';
}
}

var url = 'https://img.shields.io/badge/' + config.badgeSubject + '-' + version + '-' + color + '.svg';
if (this.querystring) {
url += '?' + this.querystring;
} else {
url += '?style=flat-square';
}

this.redirect(url);
};
35 changes: 35 additions & 0 deletions controllers/web/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,41 @@ exports.display = function* (next) {
pkg.name = orginalName;
}

// pkg.engines = {
// "python": ">= 0.11.9",
// "node": ">= 0.11.9",
// "node1": ">= 0.8.9",
// "node2": ">= 0.10.9",
// "node3": ">= 0.6.9",
// };
if (pkg.engines) {
for (var k in pkg.engines) {
var engine = String(pkg.engines[k] || '').trim();
var color = 'blue';
if (k.indexOf('node') === 0) {
color = 'yellowgreen';
var version = /(\d+\.\d+\.\d+)/.exec(engine);
if (version) {
version = version[0];
if (/^0\.11\.\d+/.test(version)) {
color = 'red';
} else if (/^0\.10\./.test(version) ||
/^0\.12\./.test(version) ||
/^0\.14\./.test(version) ||
/^[^0]+\./.test(version)) {
color = 'brightgreen';
}
}
}
pkg.engines[k] = {
version: engine,
title: k + ': ' + engine,
badgeURL: 'https://img.shields.io/badge/' + encodeURIComponent(k) +
'-' + encodeURIComponent(engine) + '-' + color + '.svg?style=flat-square',
};
}
}

yield this.render('package', {
title: 'Package - ' + pkg.name,
package: pkg,
Expand Down
10 changes: 10 additions & 0 deletions docs/web/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ $(function () {
});
</script>

## Version Badge

Default style is `flat-square`.

Badge URL: `http://cnpmjs.org/badge/v/cnpmjs.org.svg` ![cnpmjs.org-badge](http://cnpmjs.org/badge/v/cnpmjs.org.svg)

* `<0.1.0 & >=0.0.0`: ![red-badge](https://img.shields.io/badge/cnpm-0.0.1-red.svg?style=flat-square)
* `<1.0.0 & >=0.1.0`: ![red-badge](https://img.shields.io/badge/cnpm-0.1.0-green.svg?style=flat-square)
* `>=1.0.0`: ![red-badge](https://img.shields.io/badge/cnpm-1.0.0-blue.svg?style=flat-square)

## Usage

use our npm client [cnpm](https://github.com/cnpm/cnpm)(More suitable with cnpmjs.org and gzip support), you can get our client through npm:
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"graceful": "~0.1.0",
"gravatar": "~1.0.6",
"humanize-number": "~0.0.2",
"koa": "~0.9.0",
"koa": "~0.10.0",
"koa-limit": "~1.0.2",
"koa-markdown": "~0.0.3",
"koa-middlewares": "~1.2.0",
Expand All @@ -39,11 +39,11 @@
"mkdirp": "~0.5.0",
"moment": "~2.8.1",
"ms": "~0.6.2",
"multiline": "~0.3.4",
"multiline": "~1.0.0",
"mysql": "~2.4.2",
"nodemailer": "0.7.1",
"ready": "~0.1.1",
"redis": "~0.11.0",
"redis": "~0.12.1",
"semver": "~3.0.1",
"thunkify-wrap": "~1.0.2",
"utility": "~1.0.0"
Expand Down
4 changes: 4 additions & 0 deletions proxy/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -762,3 +762,7 @@ exports.updateReadme = function* (id, readme) {
data.package.readme = readme;
return yield* exports.updatePackage(id, data.package);
};

exports.getTag = function* (name, tag) {
return yield mysql.queryOne(SELECT_TAG_SQL, [name, tag]);
};
3 changes: 3 additions & 0 deletions routes/web.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var user = require('../controllers/web/user');
var sync = require('../controllers/sync');
var total = require('../controllers/total');
var dist = require('../controllers/web/dist');
var badge = require('../controllers/web/badge');

function routes(app) {
app.get('/total', total.show);
Expand Down Expand Up @@ -51,6 +52,8 @@ function routes(app) {
app.get('/_list/search/search', pkg.rangeSearch);

app.get(/^\/dist(\/.*)?/, dist.list);

app.get(/^\/badge\/v\/([@\w\-\.\/]+)\.svg$/, badge.version);
}

module.exports = routes;
93 changes: 93 additions & 0 deletions test/controllers/web/badge.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*!
* cnpmjs.org - test/controllers/web/badge.test.js
*
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <[email protected]> (http://fengmk2.github.com)
*/

'use strict';

/**
* Module dependencies.
*/

var should = require('should');
var request = require('supertest');
var mm = require('mm');
var path = require('path');
var pedding = require('pedding');
var mysql = require('../../../common/mysql');
var app = require('../../../servers/web');
var registry = require('../../../servers/registry');
var pkg = require('../../../controllers/web/package');
var utils = require('../../utils');
var config = require('../../../config');

var fixtures = path.join(path.dirname(path.dirname(__dirname)), 'fixtures');

describe('controllers/web/badge.test.js', function () {
before(function (done) {
done = pedding(2, done);
registry = registry.listen(0, done);
app = app.listen(0, done);
});

afterEach(mm.restore);

describe('GET /badge/v/:name.svg', function () {
it('should show blue version on >=1.0.0 when package exists', function (done) {
var pkg = utils.getPackage('badge-test-module', '1.0.1', utils.admin);
request(registry)
.put('/' + pkg.name)
.set('authorization', utils.adminAuth)
.send(pkg)
.end(function (err) {
should.not.exists(err);
request(app)
.get('/badge/v/badge-test-module.svg?style=flat-square')
.expect('Location', 'https://img.shields.io/badge/cnpm-1.0.1-blue.svg?style=flat-square')
.expect(302, done);
});
});

it('should show green version on <1.0.0 & >=0.1.0 when package exists', function (done) {
var pkg = utils.getPackage('badge-test-module', '0.1.0', utils.admin);
request(registry)
.put('/' + pkg.name)
.set('authorization', utils.adminAuth)
.send(pkg)
.end(function (err) {
should.not.exists(err);
request(app)
.get('/badge/v/badge-test-module.svg?style=flat-square')
.expect('Location', 'https://img.shields.io/badge/cnpm-0.1.0-green.svg?style=flat-square')
.expect(302, done);
});
});

it('should show green version on <0.1.0 & >=0.0.0 when package exists', function (done) {
var pkg = utils.getPackage('badge-test-module', '0.0.0', utils.admin);
request(registry)
.put('/' + pkg.name)
.set('authorization', utils.adminAuth)
.send(pkg)
.end(function (err) {
should.not.exists(err);
request(app)
.get('/badge/v/badge-test-module.svg?style=flat-square')
.expect('Location', 'https://img.shields.io/badge/cnpm-0.0.0-red.svg?style=flat-square')
.expect(302, done);
});
});

it('should show invalid when package not exists', function (done) {
request(app)
.get('/badge/v/badge-test-module-not-exists.svg?style=flat')
.expect('Location', 'https://img.shields.io/badge/cnpm-invalid-lightgrey.svg?style=flat')
.expect(302, done);
});
});
});
15 changes: 7 additions & 8 deletions view/web/package.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<div id="package">
<h1>
<%= package.name %>
<img title="<%= package.version %>" src="/badge/v/<%= package.name %>.svg?style=flat-square">
<small>
<% if (package._publish_on_cnpm) { %>
(Private package)
Expand Down Expand Up @@ -76,9 +77,7 @@ <h1>
<tr>
<th>Version</th>
<td>
<b>
<%= package.version %>
</b>
<img title="<%= package.version %>" src="/badge/v/<%= package.name %>.svg?style=flat-square">
<% if (package.fromNow) { %>
last updated
<%= package.fromNow %>
Expand All @@ -89,11 +88,11 @@ <h1>
<tr>
<th>Engines</th>
<td>
<ul>
<% for (var k in package.engines) { %>
<li><%= k %>: <%= package.engines[k] %></li>
<% } %>
</ul>
<% for (var k in package.engines) {
var engine = package.engines[k];
%>
<img title="<%= engine.title %>" src="<%- engine.badgeURL %>">
<% } %>
</td>
</tr>
<% } %>
Expand Down