import chalk from 'chalk'; import path from 'node:path'; import * as esbuild from 'esbuild'; import { TopLevelDirectoryName, CodeDirectoryName } from '../domain/file-names'; import { existsAsync, rmAsync } from '../helpers/fs-helpers'; import { symbols } from '../helpers/symbols'; import { runWithSpinner } from '../helpers/spinner'; const ENTRY_FILE = 'index.ts'; const OUTPUT_FILE = 'index.js'; export const tsBuild = async () => { const directory = path.join('./'); const srcPath = path.join(directory, TopLevelDirectoryName.Code, CodeDirectoryName.Src); const buildDirPath = path.join(directory, TopLevelDirectoryName.Code, CodeDirectoryName.Build); const entryPoint = path.join(srcPath, ENTRY_FILE); if (!(await existsAsync(entryPoint))) { throw new Error(`Entry file '${TopLevelDirectoryName.Code}/${CodeDirectoryName.Src}/${ENTRY_FILE}' not found.`); } await rmAsync(buildDirPath, { recursive: true, force: true }); await runWithSpinner(`Bundling from ${TopLevelDirectoryName.Code}/${CodeDirectoryName.Src}/${ENTRY_FILE}...`, () => esbuild.build({ entryPoints: [entryPoint], bundle: true, outfile: path.join(buildDirPath, OUTPUT_FILE), platform: 'node', target: 'es2022', format: 'cjs', sourcemap: 'inline', }), ); console.log(chalk.green(` ${symbols.success}${OUTPUT_FILE}`)); console.log( chalk.green( `\nBuild complete. Output written to ${TopLevelDirectoryName.Code}/${CodeDirectoryName.Build}/${OUTPUT_FILE}`, ), ); };