format function

Future<void> format(
  1. IShell shell
)

Formats all Markdown files using Remark

since 0.0.1

Implementation

Future<void> format(IShell shell) async {
  if (node.isMissingNpm(shell) || node.isMissingNpx(shell)) {
    _log.warning(
      'Skipping Formatting Markdown files. `npm` or `npx` not found.',
    );
    return;
  }
  _log.info('Formatting Markdown Files...');
  var workingDirectory = shell.workingDirectory();
  var remarkPath = p.absolute(workingDirectory, '.chassis', 'markdown');
  Directory(remarkPath).createSync(recursive: true);
  var remarkShell = shell.copyWith(workingDirectory: remarkPath);
  var remarkShellWorkingDir = remarkShell.workingDirectory();
  await _installRemark(remarkShell);
  var remarkRc = File(p.join(remarkShellWorkingDir, '.remarkrc.js'));
  var remarkConfigIsMissing = isFalse(remarkRc.existsSync());
  if (remarkConfigIsMissing) {
    _log.fine('Creating ${remarkRc.path} configuration');
    remarkRc.createSync();
    await remarkRc.writeAsString(_remarkConfig);
  } else {
    _log.fine('Using existing ${remarkRc.path} configuration');
  }
  try {
    _log.info('Running Remark');
    var projectPath = shell.workingDirectory() + p.separator;
    await remarkShell.run(
      "npx --prefix '$remarkShellWorkingDir' remark --rc-path '${remarkRc.absolute.path}' '$projectPath' --output",
    );
  } finally {
    if (remarkConfigIsMissing) {
      _log.fine('Cleaning Up ${remarkRc.path}');
      remarkRc.deleteSync();
    }
  }
}