-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
constructs/revali_server/lib/cli/commands/create/create_components/create_pipe_command.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
'''; | ||
} |