Skip to content

Commit

Permalink
feat: create command to create lifecycle component
Browse files Browse the repository at this point in the history
  • Loading branch information
mrgnhnt96 committed Jan 28, 2025
1 parent 797a137 commit 9d15f88
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,29 @@ 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<int> {
CreateCommand({
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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Exception> exceptionCatcher() {
// TODO: implement exceptionCatcher
throw UnimplementedError();
}
}
''';
}
11 changes: 10 additions & 1 deletion constructs/revali_server/lib/cli/models/create_paths.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, dynamic> json) =>
_$CreatePathsFromJson(json);
Expand All @@ -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<String, dynamic> toJson() => _$CreatePathsToJson(this);

Expand Down
6 changes: 4 additions & 2 deletions constructs/revali_server/lib/cli/models/create_paths.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9d15f88

Please sign in to comment.