From 031e45a747a0bb5381bb438ed790301ca2db3d02 Mon Sep 17 00:00:00 2001 From: Vinayak Sarawagi Date: Wed, 2 Oct 2024 20:30:00 +0530 Subject: [PATCH 1/2] ADD dev and build commands --- lib/codegen/command.ts | 34 ++++++++++++++++--------- lib/config/command.ts | 2 ++ lib/console/commands/listCommands.ts | 3 ++- lib/database/commands/migrations.ts | 18 +++++++++---- lib/database/commands/utils.ts | 29 --------------------- lib/dev-server/build.ts | 38 ++++++++++++++++++++++++++++ lib/dev-server/serve.ts | 26 +++++++++++++++++++ lib/foundation/container-factory.ts | 14 ++++++++++ lib/serviceProvider.ts | 4 +++ lib/utils/helpers.ts | 14 ++++++++++ resources/stubs/command.eta | 3 +++ 11 files changed, 138 insertions(+), 47 deletions(-) delete mode 100644 lib/database/commands/utils.ts create mode 100644 lib/dev-server/build.ts create mode 100644 lib/dev-server/serve.ts diff --git a/lib/codegen/command.ts b/lib/codegen/command.ts index ecf1280..da6e08f 100644 --- a/lib/codegen/command.ts +++ b/lib/codegen/command.ts @@ -28,8 +28,9 @@ export class CodegenCommand { _cli.success(`Successfully created ${filePath}`); } catch (e) { _cli.error(e['message']); - return; } + + process.exit(); } @Command('make:controller {name}', { desc: 'Command to create a controller' }) @@ -50,8 +51,9 @@ export class CodegenCommand { _cli.success(`Successfully created ${filePath}`); } catch (e) { _cli.error(e['message']); - return; } + + process.exit(); } @Command('make:service {name}', { desc: 'Command to create a service' }) @@ -67,8 +69,9 @@ export class CodegenCommand { _cli.success(`Successfully created ${filePath}`); } catch (e) { _cli.error(e['message']); - return; } + + process.exit(); } @Command('make:job {name}', { desc: 'Command to create a job' }) @@ -90,8 +93,8 @@ export class CodegenCommand { } catch (e) { console.log(e); _cli.error(e['message']); - return; } + process.exit(); } @Command('make:model {name}', { desc: 'Command to create a model' }) @@ -111,8 +114,8 @@ export class CodegenCommand { _cli.success(`Successfully created ${filePath}`); } catch (e) { _cli.error(e['message']); - return; } + process.exit(); } @Command('make:repo {repoName} {modelFileName} {--without-interface}', { @@ -147,8 +150,9 @@ export class CodegenCommand { _cli.success(`Successfully created ${filePath}`); } catch (e) { _cli.error(e['message']); - return; } + + process.exit(); } @Command('make:exception {name}', { @@ -166,8 +170,9 @@ export class CodegenCommand { _cli.success(`Successfully created ${filePath}`); } catch (e) { _cli.error(e['message']); - return; } + + process.exit(); } @Command('make:resource {name}', { desc: 'Command to create a service' }) @@ -183,8 +188,9 @@ export class CodegenCommand { await CommandRunner.run(`make:service ${name}`, { silent: true }); } catch (e) { _cli.error(e['message']); - return; } + + process.exit(); } @Command('make:event {name}', { @@ -206,8 +212,9 @@ export class CodegenCommand { _cli.success(`Successfully created ${filePath}`); } catch (e) { _cli.error(e['message']); - return; } + + process.exit(); } @Command( @@ -232,8 +239,9 @@ export class CodegenCommand { _cli.success(`Successfully created ${filePath}`); } catch (e) { _cli.error(e['message']); - return; } + + process.exit(); } @Command( @@ -256,8 +264,9 @@ export class CodegenCommand { _cli.success(`Successfully created ${filePath}`); } catch (e) { _cli.error(e['message']); - return; } + + process.exit(); } @Command('make:mail {name}', { @@ -278,7 +287,8 @@ export class CodegenCommand { _cli.success(`Successfully created ${filePath}`); } catch (e) { _cli.error(e['message']); - return; } + + process.exit(); } } diff --git a/lib/config/command.ts b/lib/config/command.ts index c44de97..3cc7ebc 100644 --- a/lib/config/command.ts +++ b/lib/config/command.ts @@ -30,5 +30,7 @@ export class ViewConfigCommand { // eslint-disable-next-line no-console console.log(printRows.join('\n')); + + process.exit(); } } diff --git a/lib/console/commands/listCommands.ts b/lib/console/commands/listCommands.ts index 85e1391..f1280ba 100644 --- a/lib/console/commands/listCommands.ts +++ b/lib/console/commands/listCommands.ts @@ -61,6 +61,7 @@ export class ListCommands { console.log(); console.log(printRows.join('\n')); console.log(); - return; + + process.exit(); } } diff --git a/lib/database/commands/migrations.ts b/lib/database/commands/migrations.ts index 0acf08c..052db80 100644 --- a/lib/database/commands/migrations.ts +++ b/lib/database/commands/migrations.ts @@ -31,6 +31,7 @@ export class DbOperationsCommand { } _cli.table(['Migration', 'Status'], statusList); + process.exit(); } @Command('migrate {--connection==}', { @@ -48,13 +49,15 @@ export class DbOperationsCommand { if (migrations.length === 0) { _cli.info('No migrations to run'); - return; + process.exit(); } _cli.info(`Batch Number: ${batch}`); for (const migration of migrations) { _cli.success(migration); } + + process.exit(); } @Command('migrate:rollback {--connection==}', { @@ -72,13 +75,15 @@ export class DbOperationsCommand { if (migrations.length === 0) { _cli.info('No migrations to rollback. Already at the base migration'); - return; + process.exit(); } _cli.info(`Reverted Batch: ${batch}`); for (const migration of migrations) { _cli.success(migration); } + + process.exit(); } @Command('migrate:reset {--connection==}', { @@ -96,7 +101,7 @@ export class DbOperationsCommand { if (!confirm) { _cli.info('Thank you! Exiting...'); - return; + process.exit(); } const password = await _cli.password( @@ -107,7 +112,7 @@ export class DbOperationsCommand { const conPassword = connConfig.connection?.['password']; if (conPassword && password !== conPassword) { _cli.error(' Wrong Password. Exiting... '); - return; + process.exit(); } } @@ -117,13 +122,15 @@ export class DbOperationsCommand { if (migrations.length === 0) { _cli.info('No migrations to rollback. Already at the base migration'); - return; + process.exit(); } _cli.info('Rollback of following migrations are done:'); for (const migration of migrations) { _cli.success(migration); } + + process.exit(); } @Command('make:migration {name} {--connection=}', { @@ -143,5 +150,6 @@ export class DbOperationsCommand { const paths = res.split('/'); _cli.success(paths[paths.length - 1]); + process.exit(); } } diff --git a/lib/database/commands/utils.ts b/lib/database/commands/utils.ts deleted file mode 100644 index cad062b..0000000 --- a/lib/database/commands/utils.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { Injectable } from '@nestjs/common'; -import { Command, ConsoleIO } from '../../console'; -import { ObjectionService } from '../service'; - -@Injectable() -export class DatabaseUtilitiesCommand { - constructor() {} - - @Command('db:column-info {tableName} {--connection==}') - async listColumnInfo(_cli: ConsoleIO): Promise { - const conn = - _cli.option('connection') || ObjectionService.config.default; - const knex = ObjectionService.connection(conn); - const tableName = _cli.argument('tableName'); - - const columnInfo = await knex.table(tableName).columnInfo(); - - const arr = []; - for (const column in columnInfo) { - arr.push({ - column, - ...columnInfo[column], - }); - } - - console.log(arr); - _cli.table(['Column', ''], arr); - } -} diff --git a/lib/dev-server/build.ts b/lib/dev-server/build.ts new file mode 100644 index 0000000..c7734f2 --- /dev/null +++ b/lib/dev-server/build.ts @@ -0,0 +1,38 @@ +import { Command, ConsoleIO } from '../console'; +import { getTime, Package } from '../utils'; +import pc from 'picocolors'; + +@Command( + `build + {--c|config : Path to the .intentrc file.} + {--t|tsconfig : Path to tsconfig file.} + {--d|debug : Run in debug mode (with --inspect flag).} + {--dtc|disable-type-check : Disable type checking. Enabled by default.}`, + { desc: 'Command to start the dev server' }, +) +export class BuildProjectCommand { + async handle(_cli: ConsoleIO): Promise { + const debug = _cli.option('debug'); + const disableTypeCheck = _cli.option('disable-type-check'); + const tsConfig = _cli.option('tsconfig'); + const config = _cli.option('config'); + + const now = Date.now(); + const { BuildCommand } = Package.load('@intentjs/cli'); + const command = new BuildCommand(); + await command.handle({ + debug, + disableTypeCheck, + tsConfig, + config, + }); + + console.log( + pc.gray(`[${getTime()}]`), + pc.bgWhite(pc.black(` INTENT `)), + `Build process completed in ${Date.now() - now}ms`, + ); + + process.exit(); + } +} diff --git a/lib/dev-server/serve.ts b/lib/dev-server/serve.ts new file mode 100644 index 0000000..fa74cb2 --- /dev/null +++ b/lib/dev-server/serve.ts @@ -0,0 +1,26 @@ +import { Command, ConsoleIO } from '../console'; +import { Package } from '../utils'; + +@Command(`dev + {--config : Path to intent.config.json file} + {--debug : Start debug mode in the server}`) +export class DevServerCommand { + async handle(_cli: ConsoleIO): Promise { + const debug = _cli.option('debug'); + const disableTypeCheck = _cli.option('debug'); + const tsConfig = _cli.option('tsConfig'); + const config = _cli.option('config'); + const port = +_cli.option('port'); + + const { StartServerCommand } = Package.load('@intentjs/cli'); + const command = new StartServerCommand(); + await command.handle({ + watch: true, + debug, + disableTypeCheck, + tsConfig, + config, + port, + }); + } +} diff --git a/lib/foundation/container-factory.ts b/lib/foundation/container-factory.ts index f69f47e..4e0948e 100644 --- a/lib/foundation/container-factory.ts +++ b/lib/foundation/container-factory.ts @@ -8,10 +8,24 @@ export class ContainerFactory { containerCls: Type, ): Promise { const container = new containerCls(); + container.build(); + + /** + * Build a module for NestJS DI Container + */ const module = ModuleBuilder.build(container); + + /** + * Build NestJS DI Container + */ const app = await NestFactory.createApplicationContext(module); + + /** + * Run the `boot` method of the main application container + */ container.boot(app); + return app; } } diff --git a/lib/serviceProvider.ts b/lib/serviceProvider.ts index 62f3104..7ea00ec 100644 --- a/lib/serviceProvider.ts +++ b/lib/serviceProvider.ts @@ -19,6 +19,8 @@ import { QueueService } from './queue'; import { QueueConsoleCommands } from './queue/console'; import { QueueMetadata } from './queue/metadata'; import { StorageService } from './storage/service'; +import { BuildProjectCommand } from './dev-server/build'; +import { DevServerCommand } from './dev-server/serve'; export const IntentProvidersFactory = ( config: any[], @@ -51,6 +53,8 @@ export const IntentProvidersFactory = ( LocalizationService, EventQueueWorker, LoggerService, + BuildProjectCommand, + DevServerCommand, ); } diff --git a/lib/utils/helpers.ts b/lib/utils/helpers.ts index f274cd7..24b47a0 100644 --- a/lib/utils/helpers.ts +++ b/lib/utils/helpers.ts @@ -121,3 +121,17 @@ const parseError = (error: ValidationError) => { return errors; }; + +export const getTime = () => { + const date = new Date(); + const hours = date.getHours(); + const minutes = date.getMinutes(); + const seconds = date.getSeconds(); + const ampm = hours >= 12 ? 'PM' : 'AM'; + + const formattedHours = hours % 12 || 12; + const formattedMinutes = minutes.toString().padStart(2, '0'); + const formattedSeconds = seconds.toString().padStart(2, '0'); + + return `${formattedHours}:${formattedMinutes}:${formattedSeconds} ${ampm}`; +}; diff --git a/resources/stubs/command.eta b/resources/stubs/command.eta index 5709361..0b983d7 100644 --- a/resources/stubs/command.eta +++ b/resources/stubs/command.eta @@ -10,5 +10,8 @@ export class <%= it.className %> { async handle(_cli: ConsoleIO): Promise { _cli.error('Command not implemented'); + + // you can remove the process.exit() if you would like your command to keep running. + process.exit(); } } From a0bf3055ab1c6518a362108be669e0eae2354387 Mon Sep 17 00:00:00 2001 From: Vinayak Sarawagi Date: Wed, 2 Oct 2024 20:45:08 +0530 Subject: [PATCH 2/2] refactor commands and add descriptions --- lib/dev-server/build.ts | 4 ++-- lib/dev-server/serve.ts | 8 ++++++-- package.json | 2 +- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/dev-server/build.ts b/lib/dev-server/build.ts index c7734f2..489956b 100644 --- a/lib/dev-server/build.ts +++ b/lib/dev-server/build.ts @@ -3,12 +3,12 @@ import { getTime, Package } from '../utils'; import pc from 'picocolors'; @Command( - `build + `server:build {--c|config : Path to the .intentrc file.} {--t|tsconfig : Path to tsconfig file.} {--d|debug : Run in debug mode (with --inspect flag).} {--dtc|disable-type-check : Disable type checking. Enabled by default.}`, - { desc: 'Command to start the dev server' }, + { desc: 'Command to build the application' }, ) export class BuildProjectCommand { async handle(_cli: ConsoleIO): Promise { diff --git a/lib/dev-server/serve.ts b/lib/dev-server/serve.ts index fa74cb2..190d73f 100644 --- a/lib/dev-server/serve.ts +++ b/lib/dev-server/serve.ts @@ -1,9 +1,13 @@ import { Command, ConsoleIO } from '../console'; import { Package } from '../utils'; -@Command(`dev +@Command( + `server:dev {--config : Path to intent.config.json file} - {--debug : Start debug mode in the server}`) + {--debug : Start debug mode in the server} +`, + { desc: 'Command to start a server in watch (live-reload) mode.' }, +) export class DevServerCommand { async handle(_cli: ConsoleIO): Promise { const debug = _cli.option('debug'); diff --git a/package.json b/package.json index 2535a8d..3505428 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@intentjs/core", - "version": "0.1.31", + "version": "0.1.32", "description": "Core module for Intent", "main": "dist/lib/index.js", "types": "dist/lib/index.d.ts",