@athenna/core
Version:
One foundation for multiple applications.
111 lines (110 loc) • 4.88 kB
JavaScript
/**
* @athenna/core
*
* (c) João Lenon <lenon@athenna.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { rimraf } from 'rimraf';
import { tsc } from '@athenna/tsconfig/tsc';
import { Path, Color } from '@athenna/common';
import { isAbsolute, join, parse } from 'node:path';
import { BaseCommand, Option } from '@athenna/artisan';
import { copyfiles } from '@athenna/tsconfig/copyfiles';
import { UndefinedOutDirException } from '#src/exceptions/UndefinedOutDirException';
export class BuildCommand extends BaseCommand {
static signature() {
return 'build';
}
static description() {
return 'Compile your application code from TypeScript to JavaScript.';
}
async handle() {
this.logger.simple('({bold,green} [ BUILD APPLICATION ])\n');
let tsConfigPath = Config.get('rc.commands.build.tsconfig');
if (!isAbsolute(tsConfigPath)) {
tsConfigPath = join(Path.pwd(), tsConfigPath);
}
const include = Config.get('rc.commands.build.include', []);
if (include.includes('.env')) {
include.splice(include.indexOf('.env'), 1);
}
const compiler = Color.yellow.bold('tsc');
const includedPaths = Color.gray(include.join(', '));
const outDir = this.getOutDir();
const outDirName = Color.yellow.bold(parse(outDir).name);
const tasks = this.logger.task();
tasks.addPromise(`Deleting old ${outDirName} folder`, () => rimraf(outDir));
tasks.addPromise(`Compiling the application with ${compiler} compiler`, () => tsc(tsConfigPath));
if (this.vite) {
const vite = await this.getVite();
tasks.addPromise(`Compiling static files using ${Color.yellow.bold('vite')}`, async () => {
const config = await this.getViteConfig(vite);
return vite.build(config);
});
if (Config.exists('http.vite.ssrEntrypoint')) {
tasks.addPromise(`Compiling SSR entrypoint using ${Color.yellow.bold('vite')}`, async () => {
const config = await this.getViteConfig(vite);
if (!config.build) {
config.build = {};
}
if (!config.build.rollupOptions) {
config.build.rollupOptions = {};
}
config.build.ssr = true;
config.build.outDir = Config.get('http.vite.ssrBuildDirectory');
config.build.rollupOptions.input = Config.get('http.vite.ssrEntrypoint');
return vite.build(config);
});
}
}
if (include.length) {
tasks.addPromise(`Copying included paths to ${outDirName} folder: ${includedPaths}`, () => copyfiles(include, outDir));
}
await tasks.run();
console.log();
this.logger.success('Application successfully compiled');
this.logger
.instruction()
.head('Running compiled code')
.add(`cd ${outDirName}`)
.add('npm ci --omit=dev')
.add('Define your production environment variables')
.add(`node ${Color.yellow.bold(`${parse(Path.bin()).name}/main.js`)}`)
.render();
}
getOutDir() {
if (!Config.exists('rc.commands.build.outDir')) {
throw new UndefinedOutDirException();
}
return Path.pwd(Config.get('rc.commands.build.outDir'));
}
async getVite() {
return import('vite');
}
async getViteConfig(vite) {
const { config } = await vite.loadConfigFromFile({
command: 'build',
mode: 'production'
}, undefined, Path.pwd());
return config;
}
}
__decorate([
Option({
signature: '-v, --vite',
description: 'Use vite to build your application static files.',
default: false
}),
__metadata("design:type", Boolean)
], BuildCommand.prototype, "vite", void 0);