getImportRelativePath function

String getImportRelativePath(
  1. String from,
  2. String to
)

Gets the relative import path between two file paths and normalizes it for Dart imports.

Parameters:

  • from: The String source file path.
  • to: The String target file path.

Returns:

  • A String representing the normalized relative import path from from to to.

Implementation

String getImportRelativePath(String from, String to) {
  final raw = path.relative(to, from: from);
  final isTrulyUpperLevel = RegExp(r'^\.\./\.\./').hasMatch(raw);
  return raw.replaceFirst(RegExp(r'^\.\./'), isTrulyUpperLevel ? '' : './');
}