createService method

Future<void> createService(
  1. String name, {
  2. String onCommand = '',
})

Implementation

Future<void> createService(String name, {String onCommand = ''}) async {
  var sample = ServiceSample('', name);
  if (withArgument.isNotEmpty) {
    if (isURL(withArgument)) {
      var res = await get(Uri.parse(withArgument));
      if (res.statusCode == 200) {
        var content = res.body;
        sample.customContent = replaceVars(content, name);
      } else {
        throw CliException(
            LocaleKeys.error_failed_to_connect.trArgs([withArgument]));
      }
    } else {
      var file = File(withArgument);
      if (file.existsSync()) {
        var content = file.readAsStringSync();
        sample.customContent = replaceVars(content, name);
      } else {
        throw CliException(
            LocaleKeys.error_no_valid_file_or_url.trArgs([withArgument]));
      }
    }
  }

  // Default path is app/data, but if onCommand is specified, use app/data/onCommand
  var folderName = '';

  if (onCommand.isNotEmpty) {
    // Create the directory if it doesn't exist
    var dirPath = 'lib/app/data/$onCommand';
    var directory = Directory(Structure.replaceAsExpected(path: dirPath));
    if (!directory.existsSync()) {
      directory.createSync(recursive: true);
    }
    folderName = onCommand;
  }

  handleFileCreate(
    name,
    'service',
    onCommand,
    false,  // Don't create an extra folder with the name
    sample,
    folderName,
  );
}