-
Notifications
You must be signed in to change notification settings - Fork 0
/
oslister.js
104 lines (82 loc) · 2.56 KB
/
oslister.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
var exec = require('child_process').exec;
var fs = require('fs');
var registry = require('npm-stats')()
// constants
var excludedModules = [];
var excludedDependencies = [ ];
function endsWith(str, suffix) {
return str.indexOf(suffix, str.length - suffix.length) !== -1;
}
function formatDate( dateStr) {
if ( dateStr) {
var pos = dateStr.indexOf( "T");
return pos >= 0 ? dateStr.substring( 0, pos) : dateStr;
}
return "";
}
function processPackage( file, results) {
var data = fs.readFileSync(file, 'utf8');
var pkg = JSON.parse(data);
var repo = pkg.repository ? pkg.repository.url : "<unknown>";
repo = repo.replace( "git+http","http").replace( "git+ssh","https").replace( "git://","https://").replace( /\.git$/, "");
var license = pkg.license;
if ( !license && pkg.licenses && pkg.licenses.length > 0) {
license = pkg.licenses[0].type;
}
if ( excludedModules.indexOf( pkg.name) >= 0) {
// skip modules from the excluded list
}
else {
results.push( { 'name': pkg.name, 'version': pkg.version, 'license': license, 'repo': repo });
}
}
var walk = function(dir, level, maxLevel, results, done) {
var list = fs.readdirSync(dir)
if (!list)
return done(err);
var i = 0;
for ( i = 0; i < list.length; ++i) {
var fileName = list[i];
var file = fileName;
file = dir + '/' + file;
stat = fs.statSync(file);
if (stat && stat.isDirectory() && excludedDependencies.indexOf( fileName) < 0) {
if ( level < maxLevel) {
walk(file, level + 1, maxLevel, results, done);
}
}
else
{
if ( fileName == "package.json") {
processPackage( file, results);
}
}
}
if ( level == 0) {
done( null, results);
}
}
walk( process.argv[2], 0, process.argv[3] || 3, [], function(err, results) {
if (err)
throw err;
results.sort( function( a,b) {
return a.name ? a.name.localeCompare( b.name) : -1;
});
console.log( "Name,Version,Date,License,Repository");
var unique = [];
var uniqueFull = [];
results.forEach( function( dep) {
var data = dep.name + "," + dep.version +"," + dep.license +"," + dep.repo;
if ( unique.indexOf(data) < 0) {
unique.push( data);
registry.module(dep.name).info(function(err, info) {
var releaseDate = "";
if ( info) {
releaseDate = formatDate( info.time ? info.time[ dep.version] : "2016-01-01");
}
var dataFull = dep.name + "," + dep.version +"," + releaseDate + "," + dep.license +"," + dep.repo;
console.log( dataFull);
});
}
});
});