diff --git a/scripts/buildExternal.js b/scripts/buildExternal.js index 0dd2d4c..b92cfb0 100644 --- a/scripts/buildExternal.js +++ b/scripts/buildExternal.js @@ -1,7 +1,10 @@ import { execSync } from 'child_process' import { join } from 'path' import { fileURLToPath } from 'url' -import { unlink } from 'fs/promises' +import { rename, unlink } from 'fs/promises' +import { glob } from 'glob' + +console.log('[buildExternal] building with node', process.version) // Get the project root directory const __dirname = fileURLToPath(new URL('.', import.meta.url)) @@ -10,7 +13,7 @@ const projectRoot = join(__dirname, '..') async function buildExternal() { try { // Compile TypeScript using local tsc - console.log('Compiling TypeScript...') + console.log('[buildExternal] Compiling TypeScript...') execSync( 'npx tsc main.ts --target es2020 --module esnext --moduleResolution bundler --listEmittedFiles', { @@ -20,7 +23,7 @@ async function buildExternal() { ) // Bundle with esbuild - console.log('Bundling with esbuild...') + console.log('[buildExternal] Bundling with esbuild...') execSync( 'npx esbuild main.js --bundle --format=esm --target=esnext --outfile=out.js --minify --legal-comments=none', { @@ -29,35 +32,30 @@ async function buildExternal() { } ) - // Move output file to public directory - console.log('Moving output file to public directory...') - execSync('mv ./out.js ./public/out.js', { - stdio: 'inherit', - cwd: projectRoot, - }) + // Move output file to public directory - cross platform + console.log('[buildExternal] Moving output file to public directory...') + await rename(join(projectRoot, 'out.js'), join(projectRoot, 'public', 'out.js')) - // Clean up temporary files - console.log('Cleaning up...') + // Clean up temporary files - cross platform + console.log('[buildExternal] Cleaning up...') await Promise.all([ - // Remove lib/*.js files - execSync('rm -rf ./lib/**/**.js && rm -rf ./lib/**.js', { - stdio: 'inherit', - cwd: projectRoot, - }), + // Remove lib/*.js files - preserve .ts files + (async () => { + const jsFiles = await glob('**/*.js', { + cwd: join(projectRoot, 'lib'), + absolute: true, + }) + await Promise.all(jsFiles.map((file) => unlink(file).catch(() => {}))) + })(), // Remove main.js unlink(join(projectRoot, 'main.js')).catch(() => {}), // Ignore if file doesn't exist ]) - console.log('Build completed successfully!') + console.log('[buildExternal] Build completed successfully!') } catch (error) { - console.error('Build failed:', error) + console.error('[buildExternal] Build failed:', error) process.exit(1) } } -// If script is run directly (not imported) -if (import.meta.url === `file://${process.argv[1]}`) { - buildExternal() -} - -export default buildExternal +buildExternal()