addPackages static method

Future<void> addPackages({
  1. required List<String> packages,
  2. bool isDevDependency = false,
  3. required String projectPath,
})

Adds packages to a specific project.

packages is a list of package names to add. projectPath is the path to the working directory.

If the command is successful, a success message is printed to the console. If the command fails, an error message is printed to the console.

Implementation

static Future<void> addPackages({
  required List<String> packages,
  bool isDevDependency = false,
  required String projectPath
}) async {
  final processResult = await Process.run(
      'flutter',
      isDevDependency ? <String>['pub', 'add', '-d', ...packages] : <String>['pub', 'add', ...packages],
      workingDirectory: projectPath,
      runInShell: true
  );

  if (processResult.exitCode != 0) {
    Console.writeLine(red('❌  Failed to add packages to pubspec: ${processResult.stderr}'));
    exit(1);
  } else {
    Console.writeLine(green('✅  Packages added to pubspec'));
  }
}