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 {
  final org = argResults!['org'] as String;
  final nameArg = argResults!['name'] as String?;
  final dirArg = argResults!['dir'] as String?;
  final platformsArg = argResults!['platforms'] as String;
  final platforms = _parsePlatforms(platformsArg);

  // Validate --dir if provided
  final targetDir = dirArg?.trim();
  if (targetDir != null && !Directory(targetDir).existsSync()) {
    stderr.writeln('❌ Target directory does not exist: $targetDir');
    exit(1);
  }
  if (targetDir != null) {
    stdout.writeln('  \x1B[90m📂 Creating in: $targetDir\x1B[0m');
  } else {
    stdout.writeln('  \x1B[90m📂 Creating in: ${Directory.current.path}\x1B[0m');
  }

  // Non-interactive path: --name was supplied, run directly without TUI.
  if (nameArg != null && nameArg.isNotEmpty) {
    final pluginName = nameArg.trim();
    if (!RegExp(r'^[a-z][a-z0-9_]*$').hasMatch(pluginName)) {
      stderr.writeln('❌ Invalid plugin name "$pluginName". Use only lowercase letters, numbers, and underscores.');
      exit(1);
    }
    final result = InitResult();
    await runApp(
      InitView(
        pluginName: pluginName,
        org: org,
        targetDir: targetDir,
        platforms: platforms,
        result: result,
      ),
    );
    if (result.success) {
      stdout.writeln('  \x1B[1;32m✨ $pluginName created\x1B[0m');
    } else {
      exit(1);
    }
    return;
  }

  // Interactive path: show the TUI name form.
  final result = InitResult();
  await runApp(NitrogenInitApp(result: result, initialOrg: org, initialPlatforms: platforms));
  if (result.success) {
    stdout.writeln('  \x1B[1;32m✨ ${result.pluginName ?? ''} created\x1B[0m');
  } else {
    exit(1);
  }
}