-
-
Notifications
You must be signed in to change notification settings - Fork 933
/
outputRenderer.ts
115 lines (100 loc) · 3.01 KB
/
outputRenderer.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
import { TABLE_OPTIONS } from '@pnpm/cli-utils'
import { LicensePackage } from '@pnpm/license-scanner'
import chalk from 'chalk'
import { table } from '@zkochan/table'
import { groupBy, sortWith } from 'ramda'
function sortLicensesPackages (licensePackages: readonly LicensePackage[]) {
return sortWith(
[
(o1: LicensePackage, o2: LicensePackage) =>
o1.license.localeCompare(o2.license),
],
licensePackages
)
}
function renderPackageName ({ belongsTo, name: packageName }: LicensePackage) {
switch (belongsTo) {
case 'devDependencies':
return `${packageName} ${chalk.dim('(dev)')}`
case 'optionalDependencies':
return `${packageName} ${chalk.dim('(optional)')}`
default:
return packageName as string
}
}
function renderPackageLicense ({ license }: LicensePackage) {
const output = license ?? 'Unknown'
return output as string
}
function renderDetails (licensePackage: LicensePackage) {
const outputs = []
if (licensePackage.author) {
outputs.push(licensePackage.author)
}
if (licensePackage.description) {
outputs.push(licensePackage.description)
}
if (licensePackage.homepage) {
outputs.push(chalk.underline(licensePackage.homepage))
}
return outputs.join('\n')
}
export function renderLicences (
licensesMap: LicensePackage[],
opts: { long?: boolean, json?: boolean }
) {
if (opts.json) {
return { output: renderLicensesJson(licensesMap), exitCode: 0 }
}
return { output: renderLicensesTable(licensesMap, opts), exitCode: 0 }
}
function renderLicensesJson (licensePackages: readonly LicensePackage[]) {
const data = [
...licensePackages.map((licensePkg) => {
return {
name: licensePkg.name,
version: licensePkg.version,
path: licensePkg.path,
license: licensePkg.license,
licenseContents: licensePkg.licenseContents,
author: licensePkg.author,
homepage: licensePkg.homepage,
description: licensePkg.description,
} as LicensePackageJson
}),
].flat()
// Group the package by license
const groupByLicense = groupBy((item: LicensePackageJson) => item.license)
const groupedByLicense = groupByLicense(data)
return JSON.stringify(groupedByLicense, null, 2)
}
export interface LicensePackageJson {
name: string
license: string
author: string
homepage: string
path: string
}
function renderLicensesTable (
licensePackages: readonly LicensePackage[],
opts: { long?: boolean }
) {
const columnNames = ['Package', 'License']
const columnFns = [renderPackageName, renderPackageLicense]
if (opts.long) {
columnNames.push('Details')
columnFns.push(renderDetails)
}
// Avoid the overhead of allocating a new array caused by calling `array.map()`
for (let i = 0; i < columnNames.length; i++)
columnNames[i] = chalk.blueBright(columnNames[i])
return table(
[
columnNames,
...sortLicensesPackages(licensePackages).map((licensePkg) => {
return columnFns.map((fn) => fn(licensePkg))
}),
],
TABLE_OPTIONS
)
}