playAnimation function

Future<({String message, bool success})> playAnimation(
  1. String skillDir
)

Play the thinkback animation.

Requires the year_in_review.js and player.js files to exist in the skill directory. Launches the Node.js player in the terminal's alternate screen, then opens the HTML file in the browser for video download.

Implementation

Future<({bool success, String message})> playAnimation(String skillDir) async {
  final dataPath = p.join(skillDir, 'year_in_review.js');
  final playerPath = p.join(skillDir, 'player.js');

  // Verify data file exists.
  if (!await _pathExists(dataPath)) {
    return (
      success: false,
      message: 'No animation found. Run /think-back first to generate one.',
    );
  }

  // Verify player script exists.
  if (!await _pathExists(playerPath)) {
    return (
      success: false,
      message:
          'Player script not found. The player.js file is missing from the '
          'thinkback skill.',
    );
  }

  // Run the player script.
  try {
    final result = await Process.run('node', [
      playerPath,
    ], workingDirectory: skillDir);

    if (result.exitCode != 0) {
      // Animation may have been interrupted (e.g., Ctrl+C).
    }
  } catch (_) {
    // Animation may have been interrupted.
  }

  // Open the HTML file in browser for video download.
  final htmlPath = p.join(skillDir, 'year_in_review.html');
  if (await _pathExists(htmlPath)) {
    final openCmd = Platform.isMacOS
        ? 'open'
        : Platform.isWindows
        ? 'start'
        : 'xdg-open';
    try {
      await Process.start(openCmd, [htmlPath]);
    } catch (_) {
      // Ignore errors opening browser.
    }
  }

  return (success: true, message: 'Year in review animation complete!');
}