run method
Runs this command.
The return value is wrapped in a Future if necessary and returned by
CommandRunner.runCommand.
Implementation
@override
Future<int> run() async {
String name = '';
if (argResults != null && argResults!.rest.isNotEmpty) {
name = argResults!.rest.first;
}
if (name.isEmpty) {
logger.error('No name provided.');
logger.info('Usage: flo_cli add $_type <name>');
return 64;
}
String? targetFeature;
if (_type == 'feature') {
targetFeature = name;
} else {
targetFeature = argResults?['feature'] as String?;
if (targetFeature == null || targetFeature.isEmpty) {
if (argResults != null && argResults!.rest.length > 1) {
targetFeature = argResults!.rest[1];
} else {
final existingFeatures = _getExistingFeatures();
const noneChoice = 'None (shared/core)';
const createNewChoice = '+ Create new feature...';
final choices = [
noneChoice,
...existingFeatures,
createNewChoice,
];
final selected = logger.chooseOne<String>(
'Which feature does this $_type belong to?',
choices: choices,
defaultValue: noneChoice,
);
if (selected == createNewChoice) {
final newFeature = logger.prompt('Enter name');
if (newFeature.trim().isNotEmpty) {
targetFeature = newFeature.trim();
}
} else if (selected != noneChoice) {
targetFeature = selected;
}
}
}
}
logger.info('Adding $_type "$name"...');
try {
final generator = locator<ComponentGenerator>();
await generator.generateComponent(_type, name, feature: targetFeature);
logger.success('Successfully added $_type "$name".');
return 0;
} catch (e) {
logger.error('Failed to add component: $e');
return 1;
}
}