-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-table.js
94 lines (74 loc) · 2.51 KB
/
generate-table.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
require('console.table');
const fs = require('fs');
const util = require('util');
const forEach = require('lodash/forEach');
const config = require('config');
const moment = require('moment');
const commandLineArgs = require('command-line-args');
const readFile = util.promisify(fs.readFile);
const readdir = util.promisify(fs.readdir);
const optionDefinitions = [
{ name: 'package', type: String, multiple: true, alias: 'p' },
{ name: 'days', type: Number, alias: 'd' },
{ name: 'showlink', type: Boolean, alias: 'l' },
{ name: 'dataPath', type: String, default: config.dataPath }
];
const args = commandLineArgs(optionDefinitions);
const showPackages = args.package || ['lodash'];
const showLinks = args.showlink || false;
const DAY_TIME = 1000 * 60 * 60 * 24;
async function loadData() {
const files = await readdir(args.dataPath);
const result = {};
for (let i = 0; i < files.length; i++) {
const projectName = files[i].split('.')[0];
result[projectName] = await readFile(`${args.dataPath}/${files[i]}`).then(JSON.parse);
}
return result;
}
async function run() {
const projectPackages = await loadData();
const table = [];
forEach(projectPackages, (data, project) => {
const { dependencies = {}, devDependencies = {}, repo, lastCommit } = data;
const item = { project };
showPackages.forEach((packageName) => {
const varsion = dependencies[packageName] || devDependencies[packageName] || '-.-.-';
item[packageName] = varsion;
});
if (showLinks) {
const href = repo[0] ? repo[0].href : '--';
item.repo = href
}
item.lastCommit = lastCommit;
table.push(item);
});
return table
.filter((item) => {
const { lastCommit } = item;
const { days } = args;
const now = new Date().getTime();
if (!days || !lastCommit) {
return true;
}
const lastTime = DAY_TIME * days;
return now - lastTime < lastCommit;
})
.map((item, index) => {
item[0] = index + 1;
if (item.lastCommit) {
item.lastCommit = moment(item.lastCommit).format('YYYY.MM.DD');
} else {
item.lastCommit = '****.**.**';
}
return item;
});
}
run()
.then((res) => {
console.table(res);
})
.catch((e) => {
console.log(`ERROR`);
console.log(e);
});