run method

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

Runs this command.

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

Implementation

@override
Future<int> run() async {
  final targetDir = argResults?['path'] as String? ?? '.';
  final filename = argResults?['filename'] as String? ?? 'LOCALIZABLE.md';
  final force = argResults?['force'] == true;

  final dir = Directory(targetDir);
  if (!dir.existsSync()) {
    dir.createSync(recursive: true);
  }

  final targetFile = File('${dir.path}/$filename');
  if (targetFile.existsSync() && !force) {
    final msg =
        'File "${targetFile.path}" already exists. Use --force to overwrite.';
    if (isJsonOutput) {
      printOutput({'error': msg, 'created': false, 'path': targetFile.path});
    } else {
      print(msg);
    }
    return 1;
  }

  targetFile.writeAsStringSync(agentDocTemplate);

  if (isJsonOutput) {
    printOutput({
      'success': true,
      'created': true,
      'path': targetFile.path,
      'message':
          'Created agent documentation at ${targetFile.path} for LLM agents.',
    });
  } else {
    print('Successfully installed ${targetFile.path}');
    print(
      'LLM agents can now read this file to understand all Localizable CLI commands and workflows.',
    );
  }

  return 0;
}