function runFormatting()

in src/worker.ts [25:67]


function runFormatting(
  settings: IInitializationMessage,
  files: IFilesMessage,
): Observable<WorkerMessage> {
  const output: IFormattedMessage = {
    files: files.files.length,
    formatted: [],
    failed: [],
    id: files.id,
    type: MessageType.Formatted,
  };

  return of(...files.files).pipe(
    mergeMap(async (file) => {
      const contents = await fs.readFile(file.path, 'utf-8');
      let formatted: string;
      try {
        formatted = prettier.format(contents, {
          ...(await prettier.resolveConfig(file.path)),
          filepath: file.path,
        });
      } catch (e) {
        process.stderr.write('\r\n' + inspect(e) + '\r\n');
        output.failed.push(file);
        return output;
      }

      if (formatted === contents) {
        return output;
      }

      if (settings.mode === WorkerMode.Write) {
        await fs.writeFile(file.path, formatted);
      } else if (settings.mode === WorkerMode.Print) {
        process.stdout.write(formatted);
      }

      output.formatted.push(file);
      return output;
    }),
    last(),
  );
}