fetchDependencies function
Fetches the dependencies for the specified project.
The function checks if the pubspec.yaml
and .env
files exist in the specified projectName
directory. If they do, it fetches the dependencies using getDependencies
and writes them
to a dependencies.txt
file within the project directory.
Prints appropriate messages based on the outcome.
Implementation
Future<void> fetchDependencies(String? projectName) async {
final File pubspecFile = File('$projectName/pubspec.yaml');
final File envFile = File('$projectName/.env');
if (!pubspecFile.existsSync() || !envFile.existsSync()) {
print('This command must be run in the directory of an Oyda project.');
return;
}
final List<String> packages = await getDependencies(projectName);
if (packages.isNotEmpty) {
final pubspecMap = YamlMagic.load('$projectName/pubspec.yaml');
final dependencies = pubspecMap['dependencies'];
for (var dep in packages) {
final parts = dep.split(':');
if (parts.length == 2) {
final name = parts[0].trim();
final version = parts[1].trim();
dependencies[name] = '^$version';
}
}
await pubspecFile.writeAsString(pubspecMap.toString());
final File dependenciesFile = File('$projectName/dependencies.txt');
dependenciesFile.writeAsStringSync('dependencies:\n${packages.join('\n')}');
print('Dependencies written to dependencies.txt and pubspec.yaml updated.');
} else {
print('No dependencies found.');
}
}