Skip to content

Commit d050ce7

Browse files
committed
feat: add license only mode
1 parent 7666c45 commit d050ce7

2 files changed

Lines changed: 37 additions & 19 deletions

File tree

infra/compiler/src/index.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ import path from 'node:path'
33
import url from 'node:url'
44
import { parseArgs } from 'node:util'
55

6-
import chalk from 'chalk'
76
import { context } from 'esbuild'
87

9-
import { checkLicense } from './license.js'
8+
import { generateLicense } from './license.js'
109
import { esbuildProblemMatcherPlugin } from './plugins.js'
1110

1211
import type { PackageJson } from 'type-fest'
@@ -26,6 +25,11 @@ const optionsDef = {
2625
production: {
2726
type: 'boolean',
2827
},
28+
onlyLicense: {
29+
type: 'boolean',
30+
short: 'l',
31+
default: false,
32+
},
2933
} as const
3034

3135
const { values: options } = parseArgs({ args, options: optionsDef })
@@ -43,6 +47,18 @@ if (!fss.existsSync(pkgPath)) {
4347
const pkg = (await import(url.pathToFileURL(pkgPath).href, { with: { type: 'json' } })).default
4448

4549
const absWorkingDir = path.dirname(pkgPath)
50+
const outdir = path.resolve(absWorkingDir, 'dist')
51+
52+
if (options.onlyLicense) {
53+
const metafile = path.join(outdir, 'meta.json')
54+
if (!fss.existsSync(metafile)) {
55+
throw new Error(`Meta file was not found: ${metafile}\nPlease execute \`pnpm run build\` at root directory.`)
56+
}
57+
const meta = JSON.parse(fss.readFileSync(metafile, { encoding: 'utf-8' }))
58+
generateLicense(rootDir, pkgPath, meta)
59+
console.log('The license file was generated successfully.')
60+
process.exit(0)
61+
}
4662

4763
const exports = (pkg.exports || {}) as PackageJson.ExportConditions
4864

@@ -62,8 +78,6 @@ if (entryPoints.length < 1) {
6278
throw new Error(`No export module found to build at: ${absWorkingDir}`)
6379
}
6480

65-
const outdir = path.resolve(absWorkingDir, 'dist')
66-
6781
const ctx = await context({
6882
sourceRoot: absWorkingDir,
6983
entryPoints,
@@ -92,20 +106,6 @@ if (options.watch) {
92106
if (!options.production) {
93107
fss.writeFileSync(path.join(outdir, 'meta.json'), JSON.stringify(result.metafile))
94108

95-
const baseLicense = path.join(rootDir, 'LICENSE')
96-
const checker = checkLicense(pkgPath, result.metafile)
97-
const license = checker.render(baseLicense)
98-
99-
const existingLicenseText = fss.existsSync(license.file)
100-
? fss.readFileSync(license.file, { encoding: 'utf-8' })
101-
: ''
102-
if (existingLicenseText !== license.contents) {
103-
fss.writeFileSync(license.file, license.contents, { encoding: 'utf-8' })
104-
console.info(
105-
chalk.yellow(
106-
`\n${path.relative(rootDir, license.file)} was updated. You should commit the updated file.\n`
107-
)
108-
)
109-
}
109+
generateLicense(rootDir, pkgPath, result.metafile)
110110
}
111111
}

infra/compiler/src/license.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import fss from 'node:fs'
22
import path from 'node:path'
33

4+
import chalk from 'chalk'
45
import { fdir } from 'fdir'
6+
import type { Metafile } from 'esbuild'
57

68
type LicenseData = {
79
name: string
@@ -13,6 +15,22 @@ type LicenseData = {
1315
noticeText: string | null
1416
}
1517

18+
export function generateLicense(rootDir: string, pkgPath: string, metadata: Metafile) {
19+
const baseLicense = path.join(rootDir, 'LICENSE')
20+
const checker = checkLicense(pkgPath, metadata)
21+
const license = checker.render(baseLicense)
22+
23+
const existingLicenseText = fss.existsSync(license.file)
24+
? fss.readFileSync(license.file, { encoding: 'utf-8' })
25+
: ''
26+
if (existingLicenseText !== license.contents) {
27+
fss.writeFileSync(license.file, license.contents, { encoding: 'utf-8' })
28+
console.info(
29+
chalk.yellow(`\n${path.relative(rootDir, license.file)} was updated. You should commit the updated file.\n`)
30+
)
31+
}
32+
}
33+
1634
export function checkLicense(pkgPath: string, meta: any) {
1735
const inputs = Object.keys(meta.inputs)
1836
const checker = new LicenseChecker(path.dirname(pkgPath))

0 commit comments

Comments
 (0)