convertImports function

ImportConvertData convertImports(
  1. List<String> lines,
  2. String packageName,
  3. bool toRelative,
  4. bool exitIfChanged,
  5. String filePath,
)

Convert the import paths Returns the converted file as a string at index 0 and the number of converted imports at index 1

Implementation

ImportConvertData convertImports(
  List<String> lines,
  String packageName,
  bool toRelative,
  bool exitIfChanged,
  String filePath,
) {
  bool isMultiLineString = false;
  final convertedLines = <String>[];

  for (var i = 0; i < lines.length; i++) {
    // Check if line is in multiline string
    if (_timesContained(lines[i], "'''") == 1 ||
        _timesContained(lines[i], '"""') == 1) {
      isMultiLineString = !isMultiLineString;
    }

    if (lines[i].startsWith('import') &&
        lines[i].endsWith(';') &&
        !isMultiLineString) {
      // Convert import path
      convertedLines
          .add(_convertPath(lines[i], toRelative, packageName, filePath));
    } else {
      convertedLines.add(lines[i]);
    }
  }

  final convertedFile = convertedLines.join('\n') + '\n';
  final original = lines.join('\n') + '\n';
  if (exitIfChanged && original != convertedFile) {
    if (filePath != null) {
      stdout.writeln(
          '\nā”—ā”ā”šŸšØ File ${filePath} does not have its imports converted.');
    }
    exit(1);
  }

  if (original == convertedFile) {
    return ImportConvertData(original, false);
  }

  return ImportConvertData(convertedFile, true);
}