Skip to content

Commit eb626d3

Browse files
committed
chore: update wording
1 parent 6c49c25 commit eb626d3

1 file changed

Lines changed: 35 additions & 29 deletions

File tree

infra/compiler/src/license.ts

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@ import chalk from 'chalk'
55
import { fdir } from 'fdir'
66
import type { Metafile } from 'esbuild'
77

8-
type LicenseData = {
8+
type PackageJson = {
99
name: string
1010
version: string
1111
author?: string | { name: string }
12+
license?: string
1213
repository?: { url: string }
14+
}
15+
16+
type LicenseData = PackageJson & {
1317
license: string
1418
licenseText: string
1519
noticeText: string | null
@@ -43,7 +47,7 @@ export function checkLicense(pkgPath: string, meta: any) {
4347
const absEntryPoint = path.resolve(path.dirname(pkgPath), input)
4448
const absPackageRoot = path.resolve(path.dirname(pkgPath), relativePath)
4549
const maxDepth = absEntryPoint.split(path.posix.sep).length - absPackageRoot.split(path.posix.sep).length
46-
checker.findPackageJson(absEntryPoint, maxDepth)
50+
checker.findLicense(absEntryPoint, maxDepth)
4751
}
4852
}
4953
return checker
@@ -56,7 +60,7 @@ class LicenseChecker {
5660

5761
constructor(private _rootDir: string) {}
5862

59-
findPackageJson(entryPoint: string, maxDepth: number) {
63+
findLicense(entryPoint: string, maxDepth: number) {
6064
let dir = path.dirname(entryPoint)
6165
let pkg = null
6266
let cntUpDir = 0
@@ -67,13 +71,12 @@ class LicenseChecker {
6771
break
6872
}
6973
const pkgPath = path.join(dir, 'package.json')
70-
const exists = fss.existsSync(pkgPath)
71-
if (exists) {
72-
const pkgJson = JSON.parse(fss.readFileSync(pkgPath, { encoding: 'utf-8' }))
73-
const license = pkgJson.license || pkgJson.licenses
74+
if (fss.existsSync(pkgPath)) {
75+
const pkgJson = this._getPkgJson(dir)
76+
const license = pkgJson.license
7477
const { name, version } = pkgJson
7578
const hasLicense = license && license.length > 0
76-
if ((name && version) || hasLicense) {
79+
if (name && version && hasLicense) {
7780
// found
7881
const licenseText = readFile(dir, ['license', 'licence'])
7982
if (!licenseText) {
@@ -104,18 +107,21 @@ class LicenseChecker {
104107
return `${name}@${version}`
105108
}
106109

107-
private _getPkgName() {
108-
const pkgJson = path.join(this._rootDir, 'package.json')
109-
const pkg = JSON.parse(fss.readFileSync(pkgJson, { encoding: 'utf-8' })) as { name: string }
110-
return pkg.name
110+
private _getPkgJson(dir: string) {
111+
if (this._cache.has(dir)) {
112+
return this._cache.get(dir)!
113+
}
114+
const pkgJson = path.join(dir, 'package.json')
115+
return JSON.parse(fss.readFileSync(pkgJson, { encoding: 'utf-8' })) as PackageJson
111116
}
112117

113118
render(baseLicense: string) {
114119
const baseLicenseText = fss.readFileSync(baseLicense, { encoding: 'utf-8' })
115120

116121
const contents: string[] = []
117-
contents.push(`# License of ${this._getPkgName()}`)
118-
contents.push(`${this._getPkgName()} is released under the MIT license: `)
122+
const rootPkg = this._getPkgJson(this._rootDir)
123+
contents.push(`# License of ${rootPkg.name}`)
124+
contents.push(`${rootPkg.name} is released under the MIT license: `)
119125
contents.push('')
120126
contents.push(
121127
baseLicenseText
@@ -129,44 +135,44 @@ class LicenseChecker {
129135
contents.push(Array.from(this.licenseTypeDependencies).sort().join(', '))
130136
contents.push('')
131137
contents.push('# Bundled dependencies:')
132-
const dependencies: string[] = []
138+
const dependentLicenseTexts: string[] = []
133139
const sortedDependencies = new Map([...this.dependencies].sort())
134-
sortedDependencies.forEach((value) => {
140+
sortedDependencies.forEach((pkg) => {
135141
const lines: string[] = []
136-
lines.push(`## ${value.name} `)
137-
lines.push(`License: ${value.license} `)
138-
if (value.author) {
139-
if (typeof value.author === 'object') {
140-
lines.push(`Author: ${value.author.name} `)
142+
lines.push(`## ${pkg.name} `)
143+
lines.push(`License: ${pkg.license} `)
144+
if (pkg.author) {
145+
if (typeof pkg.author === 'object') {
146+
lines.push(`Author: ${pkg.author.name} `)
141147
} else {
142-
lines.push(`Author: ${value.author.split('<')[0].split('(')[0].trim()} `)
148+
lines.push(`Author: ${pkg.author.split('<')[0].split('(')[0].trim()} `)
143149
}
144150
}
145-
if (value.repository && value.repository.url) {
146-
lines.push(`Repository: ${value.repository.url} `)
151+
if (pkg.repository && pkg.repository.url) {
152+
lines.push(`Repository: ${pkg.repository.url} `)
147153
}
148154
lines.push('### License Text')
149155
lines.push(
150-
value.licenseText
156+
pkg.licenseText
151157
.replace(/\n\r|\r/g, '\n')
152158
.split('\n')
153159
.map((line) => `> ${line}`)
154160
.join('\n')
155161
)
156162

157-
if (value.noticeText) {
163+
if (pkg.noticeText) {
158164
lines.push('### Notice Text')
159165
lines.push(
160-
value.noticeText
166+
pkg.noticeText
161167
.replace(/\n\r|\r/g, '\n')
162168
.split('\n')
163169
.map((line) => `> ${line}`)
164170
.join('\n')
165171
)
166172
}
167-
dependencies.push(lines.join('\n'))
173+
dependentLicenseTexts.push(lines.join('\n'))
168174
})
169-
contents.push(dependencies.join('\n\n---------------------------------------\n\n'))
175+
contents.push(dependentLicenseTexts.join('\n\n---------------------------------------\n\n'))
170176

171177
const licenseFile = path.join(this._rootDir, 'LICENSE.md')
172178
return {

0 commit comments

Comments
 (0)