-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
707 lines (612 loc) · 23.5 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
import async from 'async';
import * as cliProgress from 'cli-progress';
import colors from 'colors/safe';
import * as fs from 'fs-extra';
import packageJson from 'package-json';
import * as path from 'path';
import { PackageJson } from 'type-fest';
import * as yamljs from 'yamljs';
import yargs from 'yargs';
import fetch from 'node-fetch';
import { PubspecFile, PubspecFileError } from './DartDeps';
import { noop, npmDepsToPaths, ping, prettyGitURL, safeURL, wait } from './Utils';
import { Octokit } from '@octokit/rest';
import { createTokenAuth } from '@octokit/auth-token';
import { glob } from 'glob';
import resolvePackagePath from 'resolve-package-path';
import YAML from 'yaml';
import ignore from 'ignore';
import {URL} from 'url';
const IgnoreFileName = '.findlignore';
const result: QueueItem[] = [];
let verbose: boolean = false;
let progressBar: cliProgress.SingleBar | Pick<cliProgress.SingleBar, 'start' | 'stop' | 'update'>;
let cwd: string = process.cwd();
let outPath: string;
type QueueItem = {
// Name from the package.json
name: string;
// Parent folder on the disk
parent: string;
/**
* The main repository url.
*/
repositoryURL: string | null;
license?:string;
description?: string;
licenseUrl: string | null;
licenseUrlIsValid: boolean | null;
missingLicenseReason?: 'no-local' | 'no-web' | 'missing-repo';
};
const LicenseTypes = [
'AFL-3.0',
'Apache-2.0',
'Artistic-2.0',
'BSL-1.0',
'BSD-2-Clause',
'BSD-3-Clause',
'BSD-3-Clause-Clear',
'BSD-4-Clause',
'0BSD',
'CC',
'CC0-1.0',
'CC-BY-4.0',
'CC-BY-SA-4.0',
'WTFPL',
'ECL-2.0',
'EPL-1.0',
'EPL-2.0',
'EUPL-1.1',
'AGPL-3.0',
'GPL',
'GPL-2.0',
'GPL-3.0',
'LGPL',
'LGPL-2.1',
'LGPL-3.0',
'ISC',
'LPPL-1.3c',
'MS-PL',
'MIT',
'MPL-2.0',
'OSL-3.0',
'PostgreSQL',
'OFL-1.1',
'NCSA',
'Unlicense',
'Zlib',
"Standard 'no charge'"
];
// Some packages include long strings instead of the actual license.
// Try to filter those out.
const InvalidLicenseCharacters = [
'\/',
"'",
'"',
':',
];
const LicenseFileNames = [
'LICENSE',
'LICENSE.txt',
'license',
'License',
'license.md',
'License.md',
'LICENSE.md',
'LICENSE-MIT.txt',
'license-mit',
];
const PrimaryBranchNames = ['main', 'master'];
const isAValidUrl = (s:string | null) => {
if (s === null) {
return false;
}
try {
new URL(s);
return true;
} catch (err) {
return false;
}
};
const wrapInMarkdownUrl = (url: string | null, text?: string) => {
if (isAValidUrl(url)) {
return `[${text ?? url}](${url})`;
}
return url;
};
const validateLicenseName = (value: string | undefined) => {
const l = LicenseTypes.some(l => value?.includes(l))?value:undefined;
if (!l && InvalidLicenseCharacters.some(c => value?.includes(c))) {
return undefined;
}
return l;
};
let octokit: Octokit;
const postCommandLog: string[] = [];
let useGithubAPI: boolean;
const getRepoLicense = async (repo: string) => {
let license, licenseUrl;
if (repo && useGithubAPI) {
const repoPathMatch = repo.match(/^(?:https:\/\/github.com\/|github:)([^/]+\/[^/]+)/);
const repoPath = repoPathMatch ? repoPathMatch[1] : null;
if (repoPath) {
try {
const lastSlash = repoPath.lastIndexOf('/');
const repoName = repoPath.substring(lastSlash+1);
const owner = repoPath.substr(0, lastSlash);
const licenseResult = await octokit.rest.licenses.getForRepo({owner, repo: repoName}).catch(e => {
return e;
});
const licenseResultJSON = licenseResult?.data;
if ('message' in licenseResult) {
const message = licenseResult.message;
if (!postCommandLog.includes(message)) {
postCommandLog.push(message);
}
useGithubAPI = false;
} else if (licenseResultJSON && licenseResultJSON.license) {
licenseUrl = licenseResultJSON.download_url;
if (licenseResultJSON.license.spdx_id !== 'NOASSERTION') {
license = licenseResultJSON.license.spdx_id;
}
}
} catch (e) {
// Oh well.
}
}
}
return { license, licenseUrl };
};
const processPubspecQueue = async (queueItem: QueueItem, cb: () => void) => {
let pubspecFile: PubspecFile | null = null;
if (queueItem.repositoryURL?.indexOf('http') !== 0) {
pubspecFile = await fetch(`https://pub.dev/api/packages/${queueItem.name}`)
.then((response) => response.json() as PromiseLike<PubspecFile | PubspecFileError>)
.then((json) => {
if ('error' in json) {
return null;
}
return json;
});
} else if (queueItem.repositoryURL === 'flutter') {
queueItem.repositoryURL = 'https://github.com/flutter/flutter/';
}
const pubspec = pubspecFile?.latest.pubspec;
if (pubspec) {
queueItem.repositoryURL = pubspec.repository ?? pubspec.homepage ?? queueItem.repositoryURL ?? null;
queueItem.name = pubspec.name;
queueItem.description = pubspec.description;
}
if (queueItem.repositoryURL) {
const licenseData = await getRepoLicense(queueItem.repositoryURL);
queueItem.license = licenseData.license ?? undefined;
queueItem.licenseUrl = licenseData.licenseUrl ?? queueItem.licenseUrl ?? null;
// The API never gave us a URL, so look for one.
if (!queueItem.licenseUrl) {
for (let i = 0; i < LicenseFileNames.length; i++) {
const license = LicenseFileNames[i];
if (await validateLicenseURL(queueItem, license)) {
break;
}
}
} else {
queueItem.licenseUrlIsValid = true;
}
}
// Some repos don't have their licenses setup correctly (so the API can load it), so try to scrape the pub.dev page to find it.
if (!queueItem.license) {
const html = await fetch(`https://pub.dev/packages/${queueItem.name}`).catch(e => null);
if (html) {
const licensesToCheck = [
'MIT',
'BSD-3-Clause',
'BSD-2-Clause',
'Apache-2.0',
'AGPL-3.0-or-later',
'AGPL-3.0-only',
'GPL-3.0-or-later',
'GPL-3.0-only',
'LGPL-3.0-or-later',
'LGPL-3.0-only',
'MPL-2.0',
'BSL-1.0',
'Unlicense'
];
const htmlString = await html.text();
for (let i = 0; i < licensesToCheck.length; i++) {
const license = licensesToCheck[i];
if (new RegExp(`\\b${license}\\b`, 'g').test(htmlString)) {
queueItem.license = license;
break;
}
}
}
}
result.push(queueItem);
progressBar.update(result.length);
cb();
};
const processNPMQueue = async (queueItem: QueueItem, cb: () => void) => {
const packageJsonPath = `${queueItem.parent}/package.json`;
if (!(await fs.pathExists(packageJsonPath))) {
queueItem.licenseUrlIsValid = false;
queueItem.missingLicenseReason = 'no-local';
} else {
const packageJSON: PackageJson = await fs.readJSON(packageJsonPath);
queueItem.description = packageJSON.description;
queueItem.license = validateLicenseName(packageJSON.license);
queueItem.repositoryURL = packageJSON.repository
? prettyGitURL(
typeof packageJSON.repository === 'string' ? packageJSON.repository : packageJSON.repository.url
)
: null;
// Monorepos might include a "directory" value, it points to the actual location of the source.
const directory = typeof packageJSON?.repository !== 'string'?packageJSON?.repository?.directory:null;
if (directory) {
// Main repo, according to the package.json.
queueItem.repositoryURL = queueItem.repositoryURL + '/blob/main/' + directory;
}
const repoPieces = safeURL(queueItem.repositoryURL ?? '');
// Missing the repo url, so try to load its details from npm.
if (repoPieces.protocol === null) {
const npmData = await packageJson(queueItem.name, { fullMetadata: true }).catch(e => null);
if (npmData?.repository) {
queueItem.repositoryURL = prettyGitURL(npmData.repository.url);
}
}
if (queueItem.repositoryURL) {
// Case matters here. So we manually do a strict equality check to see if the file exists.
const parentContents = await fs.readdirSync(queueItem.parent);
for (let i = 0; i < LicenseFileNames.length; i++) {
const license = LicenseFileNames[i];
const pathExists = parentContents.some((p) => p === license);
if (pathExists) {
if (!(await validateLicenseURL(queueItem, license))) {
// Couldn't validate the url, so just point to the local one.
queueItem.licenseUrl = path.join(path.relative(cwd, queueItem.parent), license);
}
break;
}
}
// No local license was found :/, so check the web.
if (queueItem.licenseUrlIsValid === null) {
log(queueItem, 'No local license was found. checking the web.');
const repoLicense = await getRepoLicense(queueItem.repositoryURL);
if (repoLicense.license && repoLicense.licenseUrl) {
queueItem.licenseUrlIsValid = true;
queueItem.licenseUrl = repoLicense.licenseUrl;
queueItem.license = repoLicense.license;
}
if (!queueItem.licenseUrlIsValid) {
for (let i = 0; i < LicenseFileNames.length; i++) {
const license = LicenseFileNames[i];
// Some urls will have not protocol, so add on https as needed.
queueItem.licenseUrl?.indexOf('https://') === 0 || queueItem.licenseUrl?.indexOf('node_modules') === 0
? queueItem.licenseUrl
: 'https://' + queueItem.licenseUrl;
if (await validateLicenseURL(queueItem, license)) {
break;
}
}
}
// Still haven't found a license.
if (queueItem.licenseUrlIsValid === null) {
queueItem.licenseUrlIsValid = false;
queueItem.missingLicenseReason = 'no-web';
log(queueItem, 'No local license was found.');
}
}
} else {
queueItem.licenseUrlIsValid = false;
queueItem.missingLicenseReason = 'missing-repo';
}
}
result.push(queueItem);
progressBar.update(result.length);
cb();
};
const packageHash: Record<string, QueueItem> = {};
const pingLicenseURL = async (urlToCheck: string, tryCount=0):Promise<string | boolean> => {
const {result, status} = await ping(urlToCheck);
if (typeof result === 'string') {
return pingLicenseURL(result, tryCount+1);
} else if (result === false && status === 429 && tryCount < 3) {
await wait(2000);
return pingLicenseURL(urlToCheck, tryCount+1);
}
return result;
};
const validateLicenseURL = async (queueItem: QueueItem, license: string) => {
if (queueItem.licenseUrlIsValid !== null) {
return queueItem.licenseUrlIsValid;
}
for (let i = 0; i < PrimaryBranchNames.length; i++) {
const primaryBranch = PrimaryBranchNames[i];
const repoURL = queueItem.repositoryURL;
const urlToCheck = PrimaryBranchNames.find((b) => repoURL?.includes(`/${b}/`)) !== undefined
? `${repoURL}/${license}`
: // GitLab and GitHub both use blob, whereas bitbucket uses src.
`${repoURL}/${
repoURL?.includes('bitbucket.org') ? 'src' : 'blob'
}/${primaryBranch}/${license}`;
const urlExists = await pingLicenseURL(urlToCheck);
log(queueItem, `Checking url: ${urlToCheck} Exists: ${urlExists}`);
if (urlExists !== false) {
const licenseUrl = typeof urlExists === 'string' ? urlExists : urlToCheck;
queueItem.licenseUrl = licenseUrl ?? license;
return true;
}
// Wait a bit before trying again. Or Github will rate limit us.
await wait(200);
}
return false;
};
const formatMissingReason = (item: QueueItem) => {
switch (item.missingLicenseReason) {
case 'missing-repo':
return ' -> Cannot find valid git repository path.';
case 'no-local':
return ' -> Cannot find a license on your disk.';
case 'no-web':
return ' -> Cannot find a license on the web.';
}
};
const log = (item: QueueItem | Error, value: string = '') => {
if (verbose !== true) {
return;
}
console.log('\n');
if (item instanceof Error) {
console.error(item.name, item.message);
} else {
console.log(`${item.name}: ${value}`);
}
};
enum DependencyType {
npm = 'npm',
yarnBerry = 'yarnBerry',
dart = 'dart',
}
const findProjectType = async () => {
const supportedTypes = [
{
type: DependencyType.yarnBerry,
anchorFile: '.yarnrc.yml',
createDependencyList: getYarnBerryDeps,
processor: processNPMQueue,
},
{
type: DependencyType.npm,
anchorFile: 'package.json',
createDependencyList: getNPMdeps,
processor: processNPMQueue,
},
{
type: DependencyType.dart,
anchorFile: 'pubspec.yaml',
createDependencyList: getDartDeps,
processor: processPubspecQueue,
},
];
for (let i = 0; i < supportedTypes.length; i++) {
const element = supportedTypes[i];
if (await fs.pathExists(path.join(cwd, element.anchorFile))) {
return element;
}
}
return null;
};
const loadIgnoreFile = () => {
const ignoreFile = path.join(cwd, IgnoreFileName);
const ig = ignore();
if (fs.existsSync(ignoreFile)) {
const ignoreFileContents = fs.readFileSync(ignoreFile, 'utf8');
const ignoreFileLines = ignoreFileContents.split('\n');
ig.add(ignoreFileLines);
return ig;
}
return ig;
};
export const run = async () => {
const argv = await yargs(process.argv.slice(2)).options({
deep: { type: 'boolean', default: false },
verbose: { type: 'boolean', default: false },
cwd: { type: 'string', default: process.cwd() },
}).argv;
const logDeep = argv.deep === true;
verbose = argv.verbose === true;
cwd = argv.cwd;
outPath = path.join(cwd, 'installed-packages.md');
const projectType = await findProjectType();
if (projectType === null) {
console.log(colors.bold(colors.red('No supported dependencies file found. Exiting.')));
return;
}
console.log(colors.bold(colors.green(`Found a ${projectType.type} project.`)));
const ignorePatterns = loadIgnoreFile();
const GITHUB_TOKEN = process.env.GITHUB_TOKEN;
if (GITHUB_TOKEN) {
const auth = createTokenAuth(GITHUB_TOKEN);
const authentication = await auth();
octokit = new Octokit({auth: authentication.token});
} else {
octokit = new Octokit();
}
let requestLimit = await octokit.rateLimit.get().catch(e => {
console.log(colors.bold(colors.yellow('*** Invalid GITHUB_TOKEN. Ignoring...')));
return null;
});
if (requestLimit === null) {
octokit = new Octokit();
requestLimit = await octokit.rateLimit.get();
}
if (requestLimit.status === 200) {
const requestData = requestLimit.data;
const refreshDate = new Date(requestData.rate.reset + Date.now());
useGithubAPI = requestData.rate.remaining > 0;
console.log(colors.yellow(`You have ${requestData.rate.remaining} Github api requests left. They'll reset to ${requestData.rate.limit} at ${refreshDate.toLocaleDateString()} ${refreshDate.toLocaleTimeString()}`));
if (!GITHUB_TOKEN) {
console.log(colors.white("Hint: If you set a GITHUB_TOKEN env. You'll get more requests (and more accurate results)."));
}
} else {
useGithubAPI = false;
console.log(colors.red('Error connecting to the GitHub api.'));
}
const processingQueue: async.QueueObject<QueueItem> = async.queue(projectType.processor, 4);
processingQueue.drain(() => {
progressBar.stop();
fs.writeFile(
outPath,
result
.sort((a, b) => {
return a.name < b.name ? -1 : 1;
})
.map((q) => {
const hasBrackets = q.license?.indexOf('(') === 0;
return `##### **${q.name} ${!hasBrackets?'(':''}${q.license ?? 'no license found'}${!hasBrackets?')':''}**\n${[
q.description,
wrapInMarkdownUrl(q.repositoryURL),
wrapInMarkdownUrl(q.licenseUrl),
]
.filter((l) => l !== null && l !== undefined && l !== '')
.join('\n')}`;
})
.join('\n\n')
);
const successful = result.filter((q) => q.license);
const unsuccessful = result.filter((q) => !q.license);
const successfulCount = successful.length;
const unsuccessfulCount = unsuccessful.length;
const total = successfulCount + unsuccessfulCount;
if (postCommandLog.length > 0) {
console.log(colors.yellow(postCommandLog.join('\n')));
}
console.log(colors.bold(colors.green(`\nSaved to: ${outPath}`)));
console.log(colors.green(`Processed ${total} packages.`));
if (total !== successfulCount) {
console.log(colors.yellow(`Found ${successfulCount} licenses.`));
} else {
console.log(colors.green('Found licenses for all the packages.'));
}
if (unsuccessfulCount > 0) {
console.log(colors.bold(colors.red(`Can\'t find ${unsuccessfulCount} licenses!`)));
const missingPackages = '\t' +
unsuccessful
.map((l) => `${l.name}${l.missingLicenseReason ? formatMissingReason(l) : ''}\n\t\trepo url: ${l.repositoryURL}\n\t\tlicense url: ${l.licenseUrl}`)
.join('\n\t');
console.log(missingPackages);
}
});
progressBar = verbose
? { start: noop, update: noop, stop: noop }
: new cliProgress.SingleBar({ clearOnComplete: true }, cliProgress.Presets.shades_classic);
const ignored:string[] = [];
const deps: QueueItem[] = (await projectType.createDependencyList(logDeep)).filter(qi => {
if (ignorePatterns.ignores(qi.name)) {
ignored.push(qi.name);
return false;
}
return true;
});
if (ignore.length > 0) {
console.log(colors.yellow(`Ignoring ${ignored.length} packages:\n\t${ignored.join('\n\t')}`));
}
if (deps && deps.length > 0) {
processingQueue.push(deps);
console.log(colors.yellow(`Processing ${deps.length} packages.`));
progressBar.start(deps.length, 0);
} else {
console.log(colors.bold(colors.red('No dependencies found. Exiting.')));
}
};
const getDartDeps = async () => {
return new Promise<QueueItem[]>((resolve, reject) => {
const queueItems: QueueItem[] = [];
yamljs.load(path.join(cwd, 'pubspec.yaml'), (pubspec) => {
const dependencies: Record<string, any> = {};
for (const key in pubspec.builders) {
dependencies[key] = pubspec.builders[key];
}
for (const key in pubspec.dependencies) {
dependencies[key] = pubspec.dependencies[key];
}
for (const key in pubspec.dev_dependencies) {
dependencies[key] = pubspec.dev_dependencies[key];
}
const dependenciesList = Object.entries(dependencies);
dependenciesList.forEach(([key, value]) => {
const queueItem: QueueItem = {
name: key,
parent: '',
repositoryURL: value?.git ? prettyGitURL(value.git.url ?? value.git) : key,
licenseUrl: null,
licenseUrlIsValid: null,
};
if (!(queueItem.name in packageHash)) {
queueItems.push(queueItem);
packageHash[queueItem.name] = queueItem;
}
});
resolve(queueItems);
});
});
};
const getNPMdeps = async (logDeep: boolean) => {
const deps: string[] | null = await npmDepsToPaths(cwd, logDeep);
if (deps === null) {
return [];
}
const queueItems: QueueItem[] = [];
deps.forEach((item) => {
const queueItem: QueueItem = {
name: item,
parent: path.join(cwd, 'node_modules', item),
repositoryURL: null,
licenseUrl: null,
licenseUrlIsValid: null,
};
if (!(queueItem.name in packageHash)) {
queueItems.push(queueItem);
packageHash[queueItem.name] = queueItem;
}
});
return queueItems;
};
// Only support node_modules for now.
const getYarnBerryDeps = async () => {
const yarnRC = YAML.parse(fs.readFileSync(path.join(cwd, '.yarnrc.yml'), 'utf8'));
if (yarnRC.nodeLinker !== 'node-modules') {
console.log(colors.bold(colors.red('** We only support node-modules.')));
return [];
}
const queueItems: QueueItem[] = [];
// Package name id & versions.
const currentPackages = new Map<string, Set<string>>();
const packageJsons = await glob('**/package.json', { ignore: '**/node_modules/**' });
packageJsons.forEach((jsonPath: string) => {
// Parse out dependencies.
const json = JSON.parse(fs.readFileSync(path.resolve('./', jsonPath), 'utf8'));
for (const key in json.dependencies) {
if (key in json.dependencies) {
const version = json.dependencies[key];
if (!currentPackages.has(key)) {
currentPackages.set(key, new Set());
const modulePackageJSONPath = resolvePackagePath(key, path.dirname(jsonPath));
if (modulePackageJSONPath) {
const queueItem: QueueItem = {
name: key,
parent: path.dirname(modulePackageJSONPath),
repositoryURL: null,
licenseUrl: null,
licenseUrlIsValid: null,
};
queueItems.push(queueItem);
}
}
currentPackages.get(key)?.add(version);
}
}
});
return queueItems;
};
run();