This repository has been archived by the owner on Oct 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 161
/
gendistmodules.js
141 lines (129 loc) · 4.27 KB
/
gendistmodules.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const fs = require('fs');
const rimraf = require('rimraf');
const dir = './ui-license';
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
rimraf.sync(`${dir}/*`);
const modules = JSON.parse(fs.readFileSync('modules.json', 'utf8'));
const moduleMap = new Map();
const setMap = (m) => {
const key = m.alias ? m.alias : m.label;
if (!moduleMap.has(key)) {
moduleMap.set(key, { label: key, path: m.path });
}
};
modules.forEach((element) => {
const { groups } = element;
groups.forEach((group) => {
if (group.label === 'node_modules') {
group.groups.forEach((m) => {
if (m.label.startsWith('@')) {
const parentName = m.label;
m.groups.forEach(sub => setMap({ label: `${parentName}/${sub.label}`, path: sub.path, alias: `${parentName.slice(1)}-${sub.label}` }));
} else {
setMap(m);
}
});
}
});
});
const crawler = require('npm-license-crawler');
const request = require('request');
const options = {
start: ['./'],
dependencies: true,
omitVersion: false,
};
const fetchLicense = (moduleList) => {
const m = moduleList.shift();
if (!m) {
return;
}
if (lackLicenseFile(m)) {
// eslint-disable-next-line
console.log(`${m.name}, ${m.licenseUrl} ${m.repository}`);
fetchLicense(moduleList);
return;
}
request({ rejectUnauthorized: false, url: m.licenseUrl })
.on('response', (response) => {
if (response.statusCode !== 200) {
// eslint-disable-next-line
console.log(`${m.name}, ${m.licenseUrl} ${response.statusCode}`);
}
fetchLicense(moduleList);
})
.on('error', (err) => {
// eslint-disable-next-line
console.log(`${m.name} error, ${m.licenseUrl} ${err.code}`);
fetchLicense(moduleList);
})
.pipe(fs.createWriteStream(`${dir}/LICENSE-${m.name}`));
};
const lackLicenseFile = m => m.licenseUrl === m.repository;
crawler.dumpLicenses(options,
(error, res) => {
if (error) {
// eslint-disable-next-line
console.error(`Error:${error}`);
return;
}
const mappedRes = {};
Object.keys(res).forEach((k) => {
const labelArray = k.split('@');
mappedRes[labelArray[0]] = { ...res[k], version: labelArray[1] };
});
// eslint-disable-next-line
console.log(`${moduleMap.size} modules to find license.`);
const result = [];
const licenseMap = new Map();
for (const key of moduleMap.keys()) {
if (!mappedRes[key]) {
// eslint-disable-next-line
console.error(`There is no license for module ${key}`);
} else {
const m = { ...mappedRes[key], name: key };
const list = licenseMap.has(m.licenses) ? licenseMap.get(m.licenses) : [];
list.push(m);
licenseMap.set(m.licenses, list);
result.push(m);
}
}
for (const [licenses, sameLicensesModules] of licenseMap) {
console.log(licenses);
sameLicensesModules.sort((a, b) => {
const nameA = a.name.toUpperCase();
const nameB = b.name.toUpperCase();
if (nameA < nameB) {
return -1;
}
if (nameA > nameB) {
return 1;
}
return 0;
}).forEach(_ =>
console.log(`${_.name}\t${_.version}:\t${_.repository}\t${_.licenses}${lackLicenseFile(_) ? '\t, declare license in package.json only.' : ''}`));
console.log('-----------------------------');
}
// eslint-disable-next-line
console.log(`${result.length} modules haven found license`);
fetchLicense([...result]);
}
);