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 {
    // Setup arg table for script mode
    setupArgTable(
      originalArgs: originalArgs,
      scriptPath: scriptPath,
      scriptArgs: scriptArgs,
    );

    // Execute the script
    final file = File(scriptPath);
    if (!file.existsSync()) {
      safePrint('Error: Script file "$scriptPath" not found');
      exit(1);
    }

    final sourceCode = await file.readAsBytes().then(
      (bytes) => utf8.decode(bytes),
    );

    // Get absolute path for better debugging
    final absolutePath = file.absolute.path;

    // Log the script execution
    print('Executing script: $absolutePath');

    // Execute with script path set for proper line number tracking
    await bridge.execute(sourceCode, scriptPath: absolutePath);
  } catch (e) {
    safePrint('Error executing script "$scriptPath": $e');
    rethrow;
  }
}