run method

  1. @override
void run()
override

Runs this command.

The return value is wrapped in a Future if necessary and returned by CommandRunner.runCommand.

Implementation

@override
void run() async {
  if (argResults?.rest.isEmpty ?? true) {
    StatusHelper.failed('Feature name is empty');
  }

  final appsName = (argResults?['apps-name'] as String? ?? '').snakeCase;
  final featureName = (argResults?.rest.first ?? '').snakeCase;
  final pathApps = join(current, 'apps', appsName);

  if (appsName.isNotEmpty && !exists(pathApps)) {
    StatusHelper.failed('Apps with "$appsName" does not exists"');
  }

  String pathFeature = join(current, 'features', featureName);
  if (appsName.isNotEmpty) {
    pathFeature = join(pathApps, 'features', featureName);
  }

  if (!exists(pathFeature)) {
    StatusHelper.failed('Feature with "$featureName" does not exists"');
  }

  if (exists(pathFeature)) {
    deleteDir(pathFeature);
  }

  final workingDir = appsName.isEmpty ? current : pathApps;

  final pathLibLocator = join(workingDir, 'lib', 'locator.dart');
  String data = File(pathLibLocator).readAsStringSync();

  data = data.replaceAll(
      "import 'package:${featureName.snakeCase}/locator.dart';", '');
  data =
      data.replaceAll("setupLocatorFeature${featureName.pascalCase}();", '');

  pathLibLocator.write(data);

  final pathPubspec = join(workingDir, 'pubspec.yaml');
  String pubspec = File(pathPubspec).readAsStringSync();

  pubspec = pubspec.replaceAll(
    RegExp(
        "\\s+${featureName.snakeCase}:\\s+path: ./features/${featureName.snakeCase}"),
    '',
  );

  pathPubspec.write(pubspec);

  await ModularHelper.format([
    if (appsName.isEmpty) '.',
    if (appsName.isNotEmpty) pathApps,
  ]);

  StatusHelper.success('removed feature $featureName');
}