/
package.js
113 lines (99 loc) · 2.33 KB
/
package.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*!
* cnpmjs.org - controllers/web/package.js
*
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*
* Authors:
* dead_horse <dead_horse@qq.com> (http://deadhorse.me)
*/
'use strict';
/**
* Module dependencies.
*/
var Module = require('../../proxy/module');
var eventproxy = require('eventproxy');
var semver = require('semver');
var marked = require('marked');
exports.display = function (req, res, next) {
var params = req.params;
var name = params.name;
var tag = params.version;
var ep = eventproxy.create();
ep.fail(next);
if (tag) {
var version = semver.valid(tag);
if (version) {
Module.get(name, version, ep.done('pkg'));
} else {
Module.getByTag(name, tag, ep.done('pkg'));
}
} else {
Module.getByTag(name, 'latest', ep.done('pkg'));
}
ep.once('pkg', function (pkg) {
if (!pkg || !pkg.package) {
return next();
}
pkg = pkg.package;
pkg.readme = marked(pkg.readme || '');
setLicense(pkg);
res.render('package', {
title: pkg.name,
package: pkg
});
});
};
function setLicense (pkg) {
var license;
license = pkg.license || pkg.licenses || pkg.licence || pkg.licences;
if (!license) {
return ;
}
if (Array.isArray(license)) {
license = license[0];
}
if (typeof license === 'object') {
pkg.license = {
name: license.name || license.type,
url: license.url
};
}
if (typeof license === 'string') {
if (license.match(/(http|https)(:\/\/)/ig)) {
pkg.license = {
name: license,
url: license
};
} else {
pkg.license = {
url: getOssLicenseUrlFromName(license),
name: license
};
}
}
}
function getOssLicenseUrlFromName (name) {
var base = 'http://opensource.org/licenses/';
var licenseMap = {
'bsd': 'BSD-2-Clause',
'mit': 'MIT',
'x11': 'MIT',
'mit/x11': 'MIT',
'apache 2.0': 'Apache-2.0',
'apache2': 'Apache-2.0',
'apache 2': 'Apache-2.0',
'apache-2': 'Apache-2.0',
'apache': 'Apache-2.0',
'gpl': 'GPL-3.0',
'gplv3': 'GPL-3.0',
'gplv2': 'GPL-2.0',
'gpl3': 'GPL-3.0',
'gpl2': 'GPL-2.0',
'lgpl': 'LGPL-2.1',
'lgplv2.1': 'LGPL-2.1',
'lgplv2': 'LGPL-2.1'
};
return licenseMap[name.toLowerCase()] ?
base + licenseMap[name.toLowerCase()] : base + name;
}