diff --git a/constructs/revali_server/lib/cli/commands/create/create_command.dart b/constructs/revali_server/lib/cli/commands/create/create_command.dart index f2fe158..68125b4 100644 --- a/constructs/revali_server/lib/cli/commands/create/create_command.dart +++ b/constructs/revali_server/lib/cli/commands/create/create_command.dart @@ -7,6 +7,7 @@ import 'package:file/file.dart'; import 'package:mason_logger/mason_logger.dart'; import 'package:revali_server/cli/commands/create/create_components/create_app_command.dart'; import 'package:revali_server/cli/commands/create/create_components/create_controller_command.dart'; +import 'package:revali_server/cli/commands/create/create_components/create_lifecycle_component_command.dart'; import 'package:revali_server/cli/commands/create/create_components/create_pipe_command.dart'; class CreateCommand extends Command { @@ -14,24 +15,21 @@ class CreateCommand extends Command { required FileSystem fs, required this.logger, }) { - addSubcommand( - CreateControllerCommand( - fs: fs, - logger: logger, - ), - ); - addSubcommand( - CreateAppCommand( - fs: fs, - logger: logger, - ), - ); - addSubcommand( - CreatePipeCommand( - fs: fs, - logger: logger, - ), - ); + final subCommands = [ + CreateControllerCommand.new, + CreateAppCommand.new, + CreatePipeCommand.new, + CreateLifecycleComponentCommand.new, + ]; + + for (final sub in subCommands) { + addSubcommand( + sub( + fs: fs, + logger: logger, + ), + ); + } } @override diff --git a/constructs/revali_server/lib/cli/commands/create/create_components/create_lifecycle_component_command.dart b/constructs/revali_server/lib/cli/commands/create/create_components/create_lifecycle_component_command.dart new file mode 100644 index 0000000..e82e760 --- /dev/null +++ b/constructs/revali_server/lib/cli/commands/create/create_components/create_lifecycle_component_command.dart @@ -0,0 +1,81 @@ +import 'package:change_case/change_case.dart'; +import 'package:mason_logger/mason_logger.dart'; +import 'package:revali_server/cli/commands/create/create_components/create_a_component_command.dart'; + +class CreateLifecycleComponentCommand extends CreateAComponentCommand { + CreateLifecycleComponentCommand({ + required super.fs, + required super.logger, + }) { + argParser.addOption( + 'name', + abbr: 'n', + help: 'The name of the lifecycle component', + valueHelp: 'name', + ); + } + + @override + String get description => 'creates a new lifecycle component'; + + @override + String get name => 'lifecycle Component'; + + @override + String get fileName => '${_name.toSnakeCase()}.dart'; + + @override + String get directory => config.createPaths.lifecycleComponent; + + String _name = ''; + void namePrompt() { + var name = argResults?['name'] as String?; + + while (name == null || name.isEmpty) { + name = logger.prompt( + "What is the name of the ${green.wrap("Lifecycle Component")}?", + ); + } + + _name = name.trim(); + } + + @override + void prompt() { + namePrompt(); + } + + @override + String content() => ''' +import 'package:revali_router/revali_router.dart'; + +class ${_name.toPascalCase()} implements LifecycleComponent { + const ${_name.toPascalCase()}(); + + GuardResult guard() { + // TODO: implement guard + throw UnimplementedError(); + } + + MiddlewareResult middleware() { + // TODO: implement middleware + throw UnimplementedError(); + } + + InterceptorPreResult preInterceptor() { + // TODO: implement preInterceptor + throw UnimplementedError(); + } + + InterceptorPostResult postInterceptor() { + // TODO: implement postInterceptor + throw UnimplementedError(); + } + + ExceptionCatcherResult exceptionCatcher() { + // TODO: implement exceptionCatcher + throw UnimplementedError(); + } +} +'''; +} diff --git a/constructs/revali_server/lib/cli/models/create_paths.dart b/constructs/revali_server/lib/cli/models/create_paths.dart index 66746ae..2e4815c 100644 --- a/constructs/revali_server/lib/cli/models/create_paths.dart +++ b/constructs/revali_server/lib/cli/models/create_paths.dart @@ -9,9 +9,14 @@ class CreatePaths { String? controller, String? app, String? pipe, + String? lifecycleComponent, }) : controller = _assert(controller, p.join('routes', 'controllers')), app = _assert(app, p.join('routes', 'apps')), - pipe = _assert(pipe, p.join('lib', 'components', 'pipes')); + pipe = _assert(pipe, p.join('lib', 'components', 'pipes')), + lifecycleComponent = _assert( + lifecycleComponent, + p.join('lib', 'components', 'lifecycle_components'), + ); factory CreatePaths.fromJson(Map json) => _$CreatePathsFromJson(json); @@ -23,10 +28,14 @@ class CreatePaths { }; } + @JsonKey(fromJson: _multiString) final String controller; + @JsonKey(fromJson: _multiString) final String app; @JsonKey(fromJson: _multiString) final String pipe; + @JsonKey(fromJson: _multiString) + final String lifecycleComponent; Map toJson() => _$CreatePathsToJson(this); diff --git a/constructs/revali_server/lib/cli/models/create_paths.g.dart b/constructs/revali_server/lib/cli/models/create_paths.g.dart index b7327c0..c2330be 100644 --- a/constructs/revali_server/lib/cli/models/create_paths.g.dart +++ b/constructs/revali_server/lib/cli/models/create_paths.g.dart @@ -7,9 +7,10 @@ part of 'create_paths.dart'; // ************************************************************************** CreatePaths _$CreatePathsFromJson(Map json) => CreatePaths( - controller: json['controller'] as String?, - app: json['app'] as String?, + controller: CreatePaths._multiString(json['controller']), + app: CreatePaths._multiString(json['app']), pipe: CreatePaths._multiString(json['pipe']), + lifecycleComponent: CreatePaths._multiString(json['lifecycle_component']), ); Map _$CreatePathsToJson(CreatePaths instance) => @@ -17,4 +18,5 @@ Map _$CreatePathsToJson(CreatePaths instance) => 'controller': instance.controller, 'app': instance.app, 'pipe': instance.pipe, + 'lifecycle_component': instance.lifecycleComponent, };