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 targetPath = argResults?['path'] as String;
  final absolutePath = p.absolute(targetPath);
  print('Scanning project at: $absolutePath');

  final libPath = p.join(absolutePath, 'lib');
  if (!Directory(libPath).existsSync()) {
    print('Error: Could not find lib/ directory at $libPath');
    print('Make sure you are running this in a Flutter or Dart project root.');
    return;
  }

  print('Parsing Dart files...');
  final parser = DartParser(rootPath: absolutePath);
  final nodes = await parser.parseProject();

  print('Parsed ${nodes.length} files.');

  print('Generating graph...');
  final graph = DependencyGraph(nodes: nodes);

  print('Exporting reports...');
  final outDir = p.join(absolutePath, '.dep_explorer');
  await JsonExporter.export(graph, outDir);
  await HtmlExporter.export(graph, outDir);

  print('Scan complete! Reports saved to $outDir');

  final htmlPath = p.normalize(p.join(outDir, 'index.html'));
  final fileUri = 'file:///${htmlPath.replaceAll('\\', '/')}';

  print('\nšŸš€ Visualization ready! Opening in your browser...');

  try {
    if (Platform.isWindows) {
      await Process.run('start', [fileUri], runInShell: true);
    } else if (Platform.isMacOS) {
      await Process.run('open', [fileUri]);
    } else if (Platform.isLinux) {
      await Process.run('xdg-open', [fileUri]);
    }
  } catch (e) {
    print('Could not open browser automatically. You can open this file manually:');
    print('   $fileUri');
  }
}