import { promises as fs } from 'fs' ; import { cleanDir } from './clean' ; import { runESBuild } from './esbuild' ; import { exec } from './exec' ; import { BuildParams , cleanupParams } from './params' ; import { scanFolder } from './scan' ; import { updatePackageJSON } from './update-package' ; /**
 * Buld package
 */ export async function buildFiles ( params ) { const fullParams = cleanupParams ( params ) ; let { root , source , target , buildScript } = fullParams ; let packageJSON ; try { const content = await fs . readFile ( root + 'package.json' , 'utf8' ) ; packageJSON = JSON . parse ( content ) ; } catch ( err ) { throw new Error ( 'Cannot find package.json in root directory.' ) ; } const scripts = packageJSON . scripts as Record < string , string > ; if ( typeof scripts !== 'object' ) { throw new Error ( 'package.json is missing scripts' ) ; } if ( typeof buildScript === 'string' && typeof scripts [ buildScript ] !== 'string' ) { throw new Error ( `Missing scripts["${ buildScript }"] in package.json` ) ; } if ( fullParams . cleanup ) { await cleanDir ( root + target ) ; } const files = await scanFolder ( root + source ) ; if ( ! files . length ) { throw new Error ( `Cannot find any files to parse.` ) ; } console . log ( `Found ${ files . length } files to parse.` ) ; if ( typeof buildScript === 'string' ) { console . log ( `Executing npm script ${ buildScript }...` ) ; await exec ( root , 'npm' , [ 'run' , buildScript ] ) ; } else { console . log ( 'Executing tsc...' ) ; await exec ( root , process . platform === 'win32' ? 'tsc.cmd' : 'tsc' , [ '-b' , ] ) ; } await runESBuild ( fullParams , files ) ; if ( fullParams . updateExports ) { await updatePackageJSON ( root , target , files ) ; } }