handle method
Called automatically before executing the command. You can override to do pre-validation or setup.
Implementation
@override
Future<void> handle(List<String> args) async {
final projectName = argResults?['name'] as String?;
if (projectName == null) {
logger.error('Usage: khadem new --name=<project_name>');
exitCode = 1;
return;
}
final targetPath =
Directory.current.path + Platform.pathSeparator + projectName;
// Check if target directory already exists
if (Directory(targetPath).existsSync()) {
logger.error('❌ Directory "$projectName" already exists');
exitCode = 1;
return;
}
logger.info('📁 Creating new project: $projectName');
logger.info('📥 Cloning template from GitHub...');
try {
// Clone the template repository
await _cloneTemplate(targetPath);
// Clean up git directory
await _cleanupGitDirectory(targetPath);
// Remove template license file (projects should provide their own)
await _cleanupLicenseFiles(targetPath);
// Replace placeholders in files
await _replaceProjectPlaceholders(Directory(targetPath), projectName);
// Update .env file
await _updateEnvFile(targetPath, projectName);
logger.info('✅ Project "$projectName" created successfully!');
logger.info('👉 Next: cd $projectName && dart pub get');
exitCode = 0;
return;
} catch (e) {
logger.error('❌ Failed to create project: $e');
// Clean up partial directory if it exists
if (Directory(targetPath).existsSync()) {
await Directory(targetPath).delete(recursive: true);
}
exitCode = 1;
return;
}
}