|
| 1 | +import { Logger, TaskManager } from '@h3ravel/shared' |
| 2 | + |
| 3 | +import { ConsoleCommand } from '@h3ravel/core' |
| 4 | +import { execa } from 'execa' |
| 5 | +import preferredPM from 'preferred-pm' |
| 6 | + |
| 7 | +export class BuildCommand extends ConsoleCommand { |
| 8 | + |
| 9 | + /** |
| 10 | + * The name and signature of the console command. |
| 11 | + * |
| 12 | + * @var string |
| 13 | + */ |
| 14 | + protected signature: string = `build: |
| 15 | + {--m|minify=false : Minify your bundle output} |
| 16 | + ` |
| 17 | + |
| 18 | + /** |
| 19 | + * The console command description. |
| 20 | + * |
| 21 | + * @var string |
| 22 | + */ |
| 23 | + protected description: string = 'Build the app for production' |
| 24 | + |
| 25 | + public async handle () { |
| 26 | + try { |
| 27 | + await this.fire() |
| 28 | + } catch (e) { |
| 29 | + Logger.error(e as any) |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + protected async fire () { |
| 34 | + const outDir = env('DIST_DIR', 'dist') |
| 35 | + |
| 36 | + const pm = (await preferredPM(base_path()))?.name ?? 'pnpm' |
| 37 | + const minify = this.option('minify') |
| 38 | + const debug = Number(this.option('verbose', 0)) > 0 |
| 39 | + const LOG_LEVELS = [ |
| 40 | + 'silent', |
| 41 | + 'info', |
| 42 | + 'warn', |
| 43 | + 'error', |
| 44 | + ] |
| 45 | + |
| 46 | + const ENV_VARS = { |
| 47 | + EXTENDED_DEBUG: debug ? 'true' : 'false', |
| 48 | + CLI_BUILD: 'true', |
| 49 | + NODE_ENV: 'production', |
| 50 | + DIST_DIR: outDir, |
| 51 | + DIST_MINIFY: minify, |
| 52 | + LOG_LEVEL: LOG_LEVELS[Number(this.option('verbose', 0))] |
| 53 | + } |
| 54 | + |
| 55 | + const silent = ENV_VARS.LOG_LEVEL === 'silent' ? '--silent' : null |
| 56 | + |
| 57 | + Logger.log([['\n INFO ', 'bgBlue'], [' Creating Production Bundle', 'white']], '') |
| 58 | + console.log('') |
| 59 | + |
| 60 | + await TaskManager.taskRunner(Logger.log([[' SUCCESS ', 'bgGreen'], [' Production Bundle Created', 'white']], '', false), async () => { |
| 61 | + await execa( |
| 62 | + pm, |
| 63 | + ['tsdown', silent, '--config-loader', 'unconfig', '-c', 'tsdown.default.config.ts'].filter(e => e !== null), |
| 64 | + { stdout: 'inherit', stderr: 'inherit', cwd: base_path(), env: Object.assign({}, process.env, ENV_VARS) } |
| 65 | + ) |
| 66 | + console.log('') |
| 67 | + }) |
| 68 | + } |
| 69 | +} |
0 commit comments