run method

  1. @override
Future<void> run(
  1. List<String> args, {
  2. required Logger logger,
})
override

The execution body of the command.

Implementation

@override
Future<void> run(List<String> args, {required Logger logger}) async {
  final finder = ProjectFinder();
  final projectRoot = finder.findProjectRoot();

  final configManager = ConfigManager(projectRoot);
  if (!configManager.configExists()) {
    logger.error('No configuration found. Please run "deeplink init" first.');
    return;
  }

  final config = configManager.load();

  String? url;
  if (args.isNotEmpty) {
    final index = args.indexOf('--url');
    if (index != -1 && index + 1 < args.length) {
      url = args[index + 1];
    } else {
      url = args.first;
    }
  }

  if (url == null || url.isEmpty) {
    final defaultUrl = config.schemes.isNotEmpty
        ? '${config.schemes.first}://product/10'
        : 'myapp://product/10';
    url = Console.ask('Enter deep link URL to test', defaultValue: defaultUrl);
  }

  logger.println('Testing URL: $url\n');

  final target = Console.ask('Select Platform to test (android/ios/both)', defaultValue: 'both')
      .toLowerCase();

  var testedAny = false;

  if (target == 'android' || target == 'both') {
    logger.info('Testing on Android...');
    try {
      final adbResult = await Process.run(
        'adb',
        [
          'shell',
          'am',
          'start',
          '-a',
          'android.intent.action.VIEW',
          '-d',
          url,
        ],
        runInShell: true,
      );

      if (adbResult.exitCode == 0) {
        logger.success('Successfully sent intent to Android device/emulator.');
        testedAny = true;
      } else {
        logger.error('Android test failed (is adb installed and a device connected?):',
            adbResult.stderr);
      }
    } catch (e) {
      logger.error(
          'Android test failed: Make sure adb is installed and in your PATH. details: ${e.toString()}');
    }
  }

  if (target == 'ios' || target == 'both') {
    logger.info('Testing on iOS...');
    try {
      final simctlResult = await Process.run(
        'xcrun',
        [
          'simctl',
          'openurl',
          'booted',
          url,
        ],
        runInShell: true,
      );

      if (simctlResult.exitCode == 0) {
        logger.success('Successfully sent URL command to booted iOS simulator.');
        testedAny = true;
      } else {
        logger.error('iOS test failed (is a simulator booted?):', simctlResult.stderr);
      }
    } catch (e) {
      logger.error(
          'iOS test failed: Make sure Xcode developer tools are installed. details: ${e.toString()}');
    }
  }

  if (testedAny) {
    logger.success('Testing complete. Check your device/simulator to verify the app opened.');
  }
}