-
Notifications
You must be signed in to change notification settings - Fork 0
/
01.js
106 lines (91 loc) · 2.75 KB
/
01.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
/**
* Created by feiyu on 17/7/21.
*/
/**
* Created by feiyu on 17/7/20.
*/
var fs = require('fs');
var localPath = '/Users/feiyu/Documents/gitlab';
var pathArray = [];
var packageArray = [];
var packageAndVerArray = [];
var packages = 0;
function getAllPath(path) {
var files = fs.readdirSync(path);
files.forEach(function (file) {
var childPath = path + '/' + file;
if (fs.statSync(childPath).isDirectory()) {
getAllPath(childPath);
} else if (file === 'package.json') {
pathArray.push(childPath);
}
})
}
getAllPath(localPath);
function readFile(path) {
var data = fs.readFileSync(path);
//文件内容
var packageJson = JSON.parse(data.toString());
var dep = packageJson.dependencies;
var devdep = packageJson.devDependencies;
dep && Object.keys(dep).forEach(function (package) {
var packageAndVer = package + dep[package];
packageAndVerArray = isPackageExist(packageAndVerArray, packageAndVer);
packageArray = isPackageExist(packageArray, package);
packages++;
})
devdep && Object.keys(devdep).forEach(function (package) {
var packageAndVer = package + devdep[package];
packageAndVerArray = isPackageExist(packageAndVerArray, packageAndVer);
packageArray = isPackageExist(packageArray, package);
packages++;
})
}
function isPackageExist(array, package) {
var index = array.findIndex(function (item) {
return item.package === package;
});
if (index !== -1) {
array[index]['num']++;
} else {
array.push({
'package': package,
'num': 1
})
}
return array;
}
pathArray.forEach(function (item) {
readFile(item);
})
packageAndVerArray.sort(function (a, b) {
return b.num - a.num;
})
packageArray.sort(function (a, b) {
return b.num - a.num;
})
console.log("项目数:", pathArray.length);
console.log("所有包数:", packages);
console.log("packageArrayTop5:", packageArray.splice(0, 5));
console.log("packageAndVerArrayTop5:", packageAndVerArray.splice(0, 5));
var downApi = 'https://api.npmjs.org/downloads/point/last-week/${packagesArray}';
var https = require('https');
var packagesArray = packageArray.splice(0, 5).map(function (item) {
return item.package;
})
https.request({
host: 'api.npmjs.org',
path: '/downloads/point/last-week/' + packagesArray,
method: 'GET',
}, function (response) {
var data = '';
response.on('data', function (buff) {
data += buff;
});
response.on('end', function () {
console.log('done_______________________');
console.log(JSON.parse(data.toString()));
});
}).on('error', function (e) {
console.log('error:', e.message);
}).end();