run method

  1. @override
Future<void> run()
override

Runs this command.

The return value is wrapped in a Future if necessary and returned by CommandRunner.runCommand.

Implementation

@override
Future<void> run() async {
  try {
    // Define the target directory (.github/prompts) in the current project.
    final targetDir =
        Directory(path.join(Directory.current.path, '.github', 'prompts'));
    if (!await targetDir.exists()) {
      await targetDir.create(recursive: true);
      print('Created target directory: ${targetDir.path}');
    }

    // Loop through each prompt file in the list.
    for (final fileName in promptFiles) {
      // Build the package URI for the prompt file.
      final packageUri = Uri.parse('package:fools/prompts/$fileName');
      // Resolve the package URI to a file URI.
      final resolvedUri = await Isolate.resolvePackageUri(packageUri);
      if (resolvedUri == null) {
        print('Could not resolve URI for $fileName');
        continue;
      }

      final sourceFile = File.fromUri(resolvedUri);
      if (!await sourceFile.exists()) {
        print('Prompt file does not exist: ${sourceFile.path}');
        continue;
      }

      // Determine the destination path.
      final targetPath = path.join(targetDir.path, fileName);
      await sourceFile.copy(targetPath);
      print('Copied $fileName to $targetPath');
    }

    print('All prompt files have been copied successfully.');
  } catch (e) {
    print('Error while copying prompt files: $e');
    exit(1);
  }
}