generate method
void
generate()
Implementation
void generate() async {
final base = 'lib/src/${_feature.snakeCase}';
// Check if feature already exists
final featureDir = Directory(base);
if (featureDir.existsSync()) {
stdout.write(
'β οΈ Feature "${_feature.snakeCase}" already exists. Do you want to override it? (y/n): ');
final response = stdin.readLineSync()?.toLowerCase() ?? 'n';
if (response != 'y') {
print('β Feature generation cancelled.');
return;
}
print('π Overriding existing feature...');
}
// // Start from the script file path
// final currentPath = Directory.current.path;
//
// // Try walking up until we find the correct template path
// String? foundPath;
// Directory dir = Directory(currentPath);
// while (dir.parent.path != dir.path) {
// final maybeTemplate = Directory(path.join(dir.path, 'proj', 'lib'));
// if (maybeTemplate.existsSync()) {
// foundPath = maybeTemplate.path;
// break;
// }
// dir = dir.parent;
// }
final foundPath = await findProjPath();
if (foundPath == null) {
print('β Could not locate Template folder in parent directories.');
return;
}
final templateLib = foundPath;
final templateDir = Directory(path.join(templateLib, "lib", "src", "feature"));
if (!templateDir.existsSync()) {
print('β Template folder not found: ${templateDir.path}');
return;
}
for (final entity in templateDir.listSync(recursive: true)) {
if (entity is File) {
final relativePath = path.relative(entity.path, from: templateDir.path);
final updatedPath = _replaceFeatureInPath(relativePath);
final fullTargetPath = path.join(base, updatedPath);
_copyAndReplaceContent(entity.path, fullTargetPath);
}
}
// Update routes file
_updateRoutesFile(
path.join(Directory.current.path, 'lib/config/routes/routes.dart'));
print('β
Feature "${_feature.snakeCase}" generated successfully!');
}