-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
212 lines (199 loc) · 8.03 KB
/
index.ts
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*******************************************************
* Copyright (C) 2018 Marcelwinh https://github.com/marcelwinh
*
* This file is part of license-crawler.
*******************************************************/
import * as fs from 'fs';
const licenseFormattedList = {};
let licenses = {};
let alreadyKnownDependencies = {};
let depth = 0;
export function crawlLicenses(args: {
input: string,
out: string,
production: boolean, // if true don't check devDependencies
statistics: boolean, // generate statistics
exclude: string[],
sorted: string, // 'license' or 'package'
format: string, // 'json' or 'txt'
}, callback?) {
const defaultOptions = {
input: './', // input folder which contains package.json
out: './reportLicenses.json', // output file
production: true, // if true don't check devDependencies
statistics: true, // generate statistics
exclude: [],
sorted: 'license', // 'license' or 'package'
format: 'json', // 'json' or 'txt'
};
const options = {
input: args.input !== undefined ? args.input : defaultOptions.input,
out: args.out !== undefined ? args.out : defaultOptions.out,
production: args.production !== undefined ? args.production : defaultOptions.production,
statistics: args.statistics !== undefined ? args.statistics : defaultOptions.statistics,
exclude: args.exclude !== undefined ? args.exclude : defaultOptions.exclude,
sorted: args.sorted !== undefined ? args.sorted : defaultOptions.sorted,
format: args.format !== undefined ? args.format : defaultOptions.format,
};
if (options.input[options.input.length] !== '/' || options.input[options.input.length] !== '\\') options.input += '/';
if (fs.existsSync(options.input + 'package.json')) {
const name = JSON.parse(fs.readFileSync(options.input + 'package.json', 'utf8')).name;
const dependencies = checkDependencies(options.input + 'package.json', name, options);
let totalLicenses = 0;
for (const license in licenses) {
totalLicenses += licenses[license];
}
const licensesObject = {};
for (const license in licenses) {
licensesObject[license] = {
count: licenses[license],
percentage: (Math.round(licenses[license] / totalLicenses * 100 * 100) / 100) + ' %',
};
licenseFormattedList[license].total = licenses[license];
// tslint:disable-next-line:max-line-length
licenseFormattedList[license].percentage = (Math.round(licenses[license] / totalLicenses * 100 * 100) / 100) + ' %';
}
let report = {};
if (options.sorted === 'license') {
for (const license in licenses) {
licenseFormattedList[license].total = licenses[license];
// tslint:disable-next-line:max-line-length
licenseFormattedList[license].percentage = (Math.round(licenses[license] / totalLicenses * 100 * 100) / 100) + ' %';
}
report = licenseFormattedList;
} else if (options.sorted === 'package') {
if (options.statistics) {
report['statistics'] = licensesObject;
}
report['report'] = dependencies;
} else {
console.error('wrong sorted chosen');
}
if (options.format === 'json') {
report = JSON.stringify(report, null, 2);
} else if (options.format === 'txt') {
// convert to text
let tmp: string = '';
if (options.sorted === 'license') {
for (const license in report) {
tmp += '├─ license:' + license + ':\n';
for (const curPackage in report[license].packages) {
tmp += '│ ├─ package: ' + report[license].packages[curPackage].name + '\n';
tmp += '│ │ ├─ path: ' + report[license].packages[curPackage].path + '\n';
}
}
report = tmp;
} else if (options.sorted === 'package') {
for (const key in report.report) {
tmp += '├─' + key + ':\n';
tmp += '│ ├─ license: ' + report.report[key].license + '\n';
tmp += checkChilds(report['report'][key].childs, 1);
}
report = tmp;
}
} else {
console.log('wrong output format');
}
fs.writeFileSync(options.out, report);
}
if (callback) {
callback();
}
}
function checkChilds(childs: any, dept: number): string {
depth = dept;
let tmp: string = '';
for (const child in childs) {
if (childs[child].error === undefined) {
// tslint:disable-next-line:no-increment-decrement
for (let i = 0; i < depth; i++) {
tmp += '│ ';
}
tmp += '├─' + child + ':\n';
// tslint:disable-next-line:no-increment-decrement
for (let i = 0; i < depth; i++) {
tmp += '│ ';
}
tmp += '│ ├─ license: ' + childs[child].license + '\n';
if (childs[child].childs !== undefined) {
tmp += checkChilds(childs[child].childs, depth + 1);
depth = dept - 1;
}
}
}
return tmp;
}
function checkDependencies(packageJson: string, parent: string, options: {
input: string,
out: string,
production: boolean, // if true don't check devDependencies
statistics: boolean, // generate statistics
exclude: string[],
sorted: string, // 'license' or 'package'
format: string, // 'json' or 'txt'
}): Object {
let tmp = {};
if (fs.existsSync(packageJson)) {
let dependencies = JSON.parse(fs.readFileSync(packageJson, 'utf8')).dependencies;
if (dependencies === undefined) {
dependencies = {};
}
if (options && !options.production) {
const devDependencies = JSON.parse(fs.readFileSync(packageJson, 'utf8')).devDependencies;
if (devDependencies) {
for (const dependency in devDependencies) {
dependencies[dependency] = devDependencies[dependency];
}
}
}
// get license
for (const npmPackage in dependencies) {
if (fs.existsSync(options.input + 'node_modules/' + npmPackage + '/package.json')) {
// tslint:disable-next-line:max-line-length
let license = JSON.parse(fs.readFileSync(options.input + 'node_modules/' + npmPackage + '/package.json', 'utf8')).license;
if (typeof license === 'object') {
license = license.type;
}
license = license ? license : 'UNKNOWN';
if (options.exclude.indexOf(license) === -1) {
const childPackageJson = options.input + 'node_modules/' + npmPackage + '/package.json';
const childsParent = parent + '/' + npmPackage;
if (alreadyKnownDependencies[npmPackage + '@' + dependencies[npmPackage]] === undefined) {
alreadyKnownDependencies[npmPackage + '@' + dependencies[npmPackage]] = '';
const childs = checkDependencies(childPackageJson, childsParent, options);
if (licenses[license] !== undefined) {
licenses[license] = licenses[license] + 1;
} else {
licenses[license] = 1;
}
tmp[npmPackage + '@' + dependencies[npmPackage]] = {
license,
path: parent,
};
if (licenseFormattedList[license]) {
// tslint:disable-next-line:max-line-length
licenseFormattedList[license].packages.push({ name: npmPackage + '@' + dependencies[npmPackage], path: parent });
} else {
licenseFormattedList[license] = {
total: 1,
percentage: '',
packages: [{
name: npmPackage + '@' + dependencies[npmPackage],
path: parent,
}],
};
}
if (Object.keys(childs).length !== 0) {
tmp[npmPackage + '@' + dependencies[npmPackage]].childs = childs;
}
}
}
} else {
tmp[options.input + 'node_modules/' + npmPackage + '/package.json'] = { parent, error: 'file not found' };
}
}
} else {
tmp[packageJson] = 'file not found';
}
return tmp;
}