fmtTsJsFile function

Future<ProcessOutcome> fmtTsJsFile(
  1. String filePath
)

Formats the TypeScript/JavaScript file at filePath via prettier --write.

Returns a ProcessOutcome capturing the exit code and any stderr output. ok is false if the file does not exist, prettier cannot be launched, or it exits non-zero.

Implementation

Future<ProcessOutcome> fmtTsJsFile(String filePath) async {
  if (!await File(filePath).exists()) {
    Log.printRed('TS/JS file not found: $filePath');
    return ProcessOutcome.preconditionFailed('file not found: $filePath');
  }
  final ProcessResult result;
  try {
    result = await Process.run('prettier', ['--write', filePath]);
  } catch (e) {
    Log.printRed('Could not run "prettier" on $filePath: $e');
    return ProcessOutcome.launchFailed('$e');
  }
  final outcome = ProcessOutcome.fromResult(result);
  if (!outcome.ok) {
    Log.printRed('prettier failed for $filePath (${outcome.summary()})');
  }
  return outcome;
}