readRepositoryUrl function

Future<String> readRepositoryUrl(
  1. Directory directory
)

Reads the canonical repository URL of the project at directory.

  • Dart / Flutter: repository: in pubspec.yaml.
  • TypeScript: repository in package.json (either the shorthand string form or the { "type": "git", "url": "..." } object form).

The returned URL is normalized: git+ prefix and trailing .git or / are stripped so the value is usable as a browsable web URL.

Throws an Exception if no repository URL can be found.

Implementation

Future<String> readRepositoryUrl(Directory directory) async {
  final type = detectProjectType(directory);
  return switch (type) {
    ProjectType.dart || ProjectType.flutter => _readFromPubspec(directory),
    ProjectType.typescript => _readFromPackageJson(directory),
    ProjectType.none => throw Exception(
      cError('No repository URL: the project has no manifest.'),
    ),
  };
}