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, add a new feature with "morpheme feature <feature-name>"');
  }

  final appsName = (argResults?['apps-name'] as String? ?? '').snakeCase;
  String featureName = (argResults?.rest.first ?? '').snakeCase;
  if (appsName.isNotEmpty && !RegExp('^${appsName}_').hasMatch(featureName)) {
    featureName = '${appsName}_$featureName';
  }
  final pathApps = join(current, 'apps', appsName);

  if (appsName.isNotEmpty && !exists(pathApps)) {
    StatusHelper.failed(
        'Apps with "$appsName" does not exists, create a new apps with "morpheme apps <apps-name>"');
  }

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

  if (exists(pathFeature)) {
    StatusHelper.failed('Feature already exists in $pathFeature.');
  }

  await addNewFeature(pathFeature, featureName, appsName);
  addNewFeatureInLocator(pathFeature, featureName, appsName);
  addNewFeatureInPubspec(pathFeature, featureName, appsName);
  addNewGitIgnore(pathFeature, featureName, appsName);
  addNewAnalysisOption(pathFeature, featureName, appsName);

  removeUnusedDir(pathFeature, featureName, appsName);

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

  await FlutterHelper.start('pub get', workingDirectory: pathFeature);
  await FlutterHelper.start('pub get',
      workingDirectory: appsName.isEmpty ? '.' : pathApps);

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