forked from 3rd-Eden/licenses
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregistry.js
118 lines (102 loc) · 2.76 KB
/
registry.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
114
115
116
117
118
'use strict';
var debug = require('debug')('licenses::npm');
/**
* Parser for npm based license information.
*
* @constructor
* @api public
*/
module.exports = require('./parser').extend({
/**
* The name of this parser.
*
* @type {String}
* @private
*/
name: 'npm',
/**
* Parse the npm license information from the package.
*
* @param {Object} data The package.json or npm package contents.
* @param {Object} options Optional options.
* @param {Function} next Continuation.
* @api public
*/
parse: function parse(data, options, next) {
data = this.get(data);
if ('function' === typeof options) {
next = options;
options = {};
}
//
// We cannot detect a license so we call the callback without any arguments
// which symbolises a failed attempt.
//
if (!data) return next();
debug('found %s in the package contents', data);
// @TODO handle the edge case where people give us an URL instead of an
// actual license.
next(undefined, this.normalize(data));
},
/**
* Return the possible location of license information.
*
* @param {Object} data The object that should contain the license.
* @returns {String}
* @api private
*/
license: function licenses(data) {
if ('string' === typeof data && data) return data;
if ('type' in data && data.type) return data.type;
//
// Common typo's
//
if ('type:' in data && data['type:']) return data['type:'];
return;
},
/**
* Is npm based license detection an option for this package.
*
* @param {Object} data The package.json or npm package contents.
* @returns {Boolean}
* @api public
*/
supported: function supported(data) {
return !!this.get(data);
},
/**
* Retrieve the possible locations of the license information.
*
* @param {Object} data The package.json or npm package contents.
* @returns {Array}
* @api private
*/
get: function get(data) {
var parser = this
, matches = [];
//
// Another npm oddity, it allows licenses to be specified in to different
// properties. Because why the fuck not?
//
['license', 'licenses'].forEach(function each(key) {
if ('string' === typeof data[key]) {
return matches.push(data[key]);
}
if (Array.isArray(data[key])) {
return Array.prototype.push.apply(
matches,
data[key].map(function map(item) {
return parser.license(item);
}).filter(Boolean)
);
}
if ('object' === typeof data[key] && parser.license(data[key])) {
return Array.prototype.push.apply(
matches,
[parser.license(data[key])]
);
}
});
if (matches.length) return matches;
}
});