execute method
execute command
Implementation
@override
Future<void> execute() async {
var componentName = this.name;
final componentsDir =
flags.contains('--dir') ? _getArgValue('--dir') : 'lib/components';
final verbose = flags.contains('--verbose') ? true : false;
final customFileName =
flags.contains('--file') ? _getArgValue('--file') : null;
final routePath =
flags.contains('--route')
? _getArgValue('--route')
: '/${componentName.toLowerCase()}';
if (verbose) {
LogService.info('Creating component $componentName in $componentsDir');
}
final projectDir = Directory.current.path;
final fullComponentsDir = path.join(projectDir, componentsDir);
try {
if (!Directory(fullComponentsDir).existsSync()) {
Directory(fullComponentsDir).createSync(recursive: true);
if (verbose) {
LogService.info("Created directory: $componentsDir");
}
}
// Determine the file path
final fileName =
customFileName != null
? customFileName.endsWith('.dart')
? customFileName
: '$customFileName.dart'
: '${_getFileNameFromComponentName(componentName)}.dart';
final filePath = path.join(fullComponentsDir, fileName);
// Check if the file already exists
if (File(filePath).existsSync()) {
LogService.error("File already exists: $filePath");
return;
}
// Generate the page content
final pageContent = _generateComponentContent(componentName, routePath);
// Write the file
File(filePath).writeAsStringSync(pageContent);
LogService.success("Generated page at: $filePath");
LogService.info("Running router scanner to update routes...");
} catch (e, stackTrace) {
LogService.error(
"Error running component scanner",
error: e,
stackTrace: stackTrace,
);
}
}