Skip to content

Commit

Permalink
feat: create pipe create command
Browse files Browse the repository at this point in the history
  • Loading branch information
mrgnhnt96 committed Jan 28, 2025
1 parent 7ffa0d1 commit 797a137
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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_pipe_command.dart';

class CreateCommand extends Command<int> {
CreateCommand({
Expand All @@ -25,6 +26,12 @@ class CreateCommand extends Command<int> {
logger: logger,
),
);
addSubcommand(
CreatePipeCommand(
fs: fs,
logger: logger,
),
);
}

@override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
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 CreatePipeCommand extends CreateAComponentCommand {
CreatePipeCommand({
required super.fs,
required super.logger,
}) {
argParser
..addOption(
'return-type',
abbr: 'r',
help: 'The return type of the pipe',
valueHelp: 'return-type',
)
..addOption(
'input-type',
abbr: 'i',
help: 'The input type of the pipe',
valueHelp: 'input-type',
);
}

@override
String get description => 'creates a new pipe';

@override
String get name => 'pipe';

@override
String get fileName => '${_returnType.toSnakeCase()}_pipe.dart';

@override
String get directory => config.createPaths.pipe;

String _returnType = '';
void returnTypePrompt() {
var returnType = argResults?['return-type'] as String?;

while (returnType == null || returnType.isEmpty) {
returnType = logger.prompt(
"What is ${green.wrap("pipe's")} "
"the ${yellow.wrap('return type')}?",
);
}

_returnType = returnType.trim();
}

String _inputType = '';
void inputTypePrompt() {
var inputType = argResults?['input-type'] as String?;

const other = 'Other';
const defaultInputs = [
'String',
'Map<String, dynamic>',
other,
];

while (inputType == null || inputType.isEmpty) {
final result = logger.chooseOne(
"What is ${green.wrap("pipe's")} "
"the ${yellow.wrap('input type')}?",
choices: defaultInputs,
);

if (result == other) {
// clears previous line
logger.write('\u001b[1A\u001b[2K');

inputType = logger.prompt(
"What is ${green.wrap("pipe's")} "
"the ${yellow.wrap('input type')}?",
);
} else {
inputType = result;
}
}

_inputType = switch (inputType) {
_ when defaultInputs.contains(inputType) => inputType,
_ => inputType.trim().toPascalCase(),
};
}

@override
void prompt() {
returnTypePrompt();
inputTypePrompt();
}

@override
String content() => '''
import 'dart:async';
import 'package:revali_router/revali_router.dart';
class ${_returnType.toPascalCase()}Pipe implements Pipe<$_inputType, ${_returnType.toPascalCase()}> {
const ${_returnType.toPascalCase()}Pipe();
@override
FutureOr<${_returnType.toPascalCase()}> transform($_inputType value, PipeContext context) {
// TODO: implement transform
throw UnimplementedError();
}
}
''';
}

0 comments on commit 797a137

Please sign in to comment.