getPubspecContent static method

PubspecContentModel? getPubspecContent()

Reads and returns the content of the pubspec.yaml file. @returns {PubspecContentModel?} The file content, or null if not found.

Implementation

static PubspecContentModel? getPubspecContent() {
  final pubspecFilePath = p.join(Directory.current.path, 'pubspec.yaml');
  final pubspecFile = File(pubspecFilePath);

  if (!pubspecFile.existsSync()) {
    stderr.writeln('Error: pubspec.yaml not found in the current directory.');
    return null;
  }

  try {
    return PubspecContentModel(
      path: pubspecFilePath,
      content: pubspecFile.readAsStringSync(),
    );
  } on FileSystemException catch (e) {
    stderr.writeln('Error reading pubspec.yaml: ${e.message}');
    return null;
  }
}