executeJavaScript method

  1. @override
Future<String> executeJavaScript(
  1. String jsCode
)
override

Executes JavaScript code and returns the JSON string result.

Implementation

@override
Future<String> executeJavaScript(String jsCode) async {
  final filePath = path.join((_deno.tmpDir.path),
      'ejs_output_${DateTime.now().microsecondsSinceEpoch}.txt');

  // Wrap the call into a write to file
  final wrappedCode = 'await Deno.writeTextFile("$filePath", $jsCode);';

  final result = await _deno.eval(wrappedCode);

  if (result != "undefined") {
    throw Exception('Expected undefined result from Deno eval, got: $result');
  }

  final file = File(filePath);
  return await file.readAsString();
}