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.firstOrNull == 'init') {
    init();
    return;
  }

  final argMorphemeYaml = argResults.getOptionMorphemeYaml();
  projectName = YamlHelper.loadFileYaml(argMorphemeYaml).projectName;

  appsName = argResults?['apps-name'];

  final searchFileJson2Dart = appsName?.isNotEmpty ?? false
      ? '${appsName}_json2dart.yaml'
      : '*json2dart.yaml';

  final workingDirectory = find(
    searchFileJson2Dart,
    workingDirectory: join(current, 'json2dart'),
  ).toList();

  final listCommands = <String>[];

  for (var pathJson2Dart in workingDirectory) {
    if (!exists(pathJson2Dart)) {
      StatusHelper.warning(
          'you don\'t have "json2dart.yaml" in $pathJson2Dart');
    }

    final yml = YamlHelper.loadFileYaml(pathJson2Dart);
    Map json2DartMap = Map.from(yml);

    if (json2DartMap['json2dart'] != null) {
      final config = json2DartMap.remove('json2dart');

      if (config['body_format_date_time'] != null) {
        defaultBodyDateFormat =
            ".toFormatDateTimeBody('${config['body_format_date_time']}')";
      }
      if (config['response_format_date_time'] != null) {
        defaultResponseDateFormat =
            ".toFormatDateTimeResponse('${config['response_format_date_time']}')";
      }
      if (config['endpoint'] != null && config['endpoint'] is bool) {
        isEndpoint = config['endpoint'];
      }
      if (config['api'] != null && config['api'] is bool) {
        isApi = config['api'];
      }
      if (config['unit-test'] != null && config['unit-test'] is bool) {
        isUnitTest = config['unit-test'];
      }
      if (config['replace'] != null && config['replace'] is bool) {
        isReplace = config['replace'];
      }
      if (config['format'] != null && config['format'] is bool) {
        isFormat = config['format'];
      }
      if (config['cubit'] != null && config['cubit'] is bool) {
        isCubit = config['cubit'];
      }
    }

    isEndpoint = (argResults?.arguments.firstWhereOrNull(
                    (element) => element.contains('endpoint')) !=
                null
            ? argResults!['endpoint']
            : null) ??
        isEndpoint ??
        true;

    isApi = (argResults?.arguments
                    .firstWhereOrNull((element) => element.contains('api')) !=
                null
            ? argResults!['api']
            : null) ??
        isApi ??
        true;
    isUnitTest = (argResults?.arguments.firstWhereOrNull(
                    (element) => element.contains('unit-test')) !=
                null
            ? argResults!['unit-test']
            : null) ??
        isUnitTest ??
        false;
    isOnlyUnitTest = (argResults?.arguments.firstWhereOrNull(
                    (element) => element.contains('only-unit-test')) !=
                null
            ? argResults!['only-unit-test']
            : null) ??
        isOnlyUnitTest ??
        false;
    isReplace = (argResults?.arguments.firstWhereOrNull(
                    (element) => element.contains('replace')) !=
                null
            ? argResults!['replace']
            : null) ??
        isReplace ??
        false;
    isFormat = (argResults?.arguments.firstWhereOrNull(
                    (element) => element.contains('format')) !=
                null
            ? argResults!['format']
            : null) ??
        isFormat ??
        true;
    isCubit = (argResults?.arguments.firstWhereOrNull(
                    (element) => element.contains('cubit')) !=
                null
            ? argResults!['cubit']
            : null) ??
        isCubit ??
        true;
    featureName = argResults?['feature-name'];
    pageName = argResults?['page-name'];

    if (!isOnlyUnitTest && isEndpoint) {
      await 'morpheme endpoint --json2dart'.run;
    }

    if (featureName != null) {
      if (json2DartMap.keys
              .firstWhereOrNull((element) => element == featureName) ==
          null) {
        StatusHelper.warning('$featureName not found in json2dart.yaml');
      } else {
        final lastPathJson2Dart = pathJson2Dart.split(separator).last;

        String featurePath = join(current, 'features', featureName);
        String? appsName = this.appsName;
        if (lastPathJson2Dart.contains('_')) {
          appsName = lastPathJson2Dart.split('_').first;
          featurePath =
              join(current, 'apps', appsName, 'features', featureName);
        }
        await handleFeature(
            featurePath, featureName!, json2DartMap[featureName], appsName);

        if (isFormat && format.isNotEmpty) {
          printMessage('Execute morpheme format................');
          await ModularHelper.format(format);
        }
      }
    } else {
      for (var element in json2DartMap.entries) {
        final featureName = element.key;

        final lastPathJson2Dart = pathJson2Dart.split(separator).last;

        String? appsName = this.appsName;
        if (lastPathJson2Dart.contains('_')) {
          appsName = lastPathJson2Dart.split('_').first;
        }
        listCommands.add(
          'morpheme json2dart --feature-name $featureName '
          '--morpheme-yaml $argMorphemeYaml '
          '${appsName?.isNotEmpty ?? false ? '--apps-name $appsName' : ''} '
          '${isApi ? '--api' : '--no-api'} '
          '${isUnitTest ? '--unit-test' : '--no-unit-test'} '
          '${isOnlyUnitTest ? '--only-unit-test' : '--no-only-unit-test'} '
          '${isReplace ? '--replace' : '--no-replace'} '
          '--no-endpoint '
          '--no-format ',
        );
      }
    }
  }

  if (listCommands.isNotEmpty) {
    await ModularHelper.executeCommand(listCommands);
    printMessage('Execute morpheme format................');
    await ModularHelper.format([join(current, 'features')]);
  }

  StatusHelper.success('morpheme json2dart');
}