convert method

Future<SvgFile> convert({
  1. required bool force,
  2. Progress progress = noOp,
})

creates an Svg image from the smcat file.

Requires that the 'smcat' cli tools are installed.

If force is true, the conversion will be performed even if the svg file already exists.

Throws SMCatException if the conversion fails

Implementation

Future<SvgFile> convert(
    {required bool force, Progress progress = noOp}) async {
  if (!force && !isConversionRequired()) {
    return svgFile;
  }

  progress('Generating: $svgPath ');
  if (File(svgPath).existsSync()) {
    File(svgPath).deleteSync();
  }

  final process = await Process.start('smcat', [p.basename(pathTo)],
      workingDirectory: p.dirname(pathTo));

  process.stdout.transform(utf8.decoder).listen((data) {
    progress(data);
  });

  process.stderr.transform(utf8.decoder).listen((data) {
    if (!data.contains('viz.js:33')) {
      progress(data);
    }
  });

  final exitCode = await process.exitCode;

  if (exitCode == 0) {
    /// See if the filename contains a page no.
    progress('Generation of $svgPath complete.');
    _svgFile = SvgFile(svgPath);
    await _svgFile.addPageNo();
    return _svgFile;
  } else {
    progress('Generation of $svgPath failed.');
    throw SMCatException(
        'Generation of $svgPath failed. exitCode: $exitCode');
  }
}