@@ -3,10 +3,9 @@ import path from 'node:path'
33import url from 'node:url'
44import { parseArgs } from 'node:util'
55
6- import chalk from 'chalk'
76import { context } from 'esbuild'
87
9- import { checkLicense } from './license.js'
8+ import { generateLicense } from './license.js'
109import { esbuildProblemMatcherPlugin } from './plugins.js'
1110
1211import 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
3135const { values : options } = parseArgs ( { args, options : optionsDef } )
@@ -43,6 +47,18 @@ if (!fss.existsSync(pkgPath)) {
4347const pkg = ( await import ( url . pathToFileURL ( pkgPath ) . href , { with : { type : 'json' } } ) ) . default
4448
4549const 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
4763const 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-
6781const 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}
0 commit comments