makeAtomicFileWriter = (
  fileName,
  { fs, path, os },
  pid = undefined,
  nonce = undefined,
  make = there =>
    makeAtomicFileWriter(there, { fs, path, os }, pid, nonce, make),
) => {
  const writer = makeFileWriter(fileName, { fs, path }, make);

  // Windows does not support atomic rename so we do the next best albeit racey
  // thing.
  if (os.platform() === 'win32') {
    return harden({
      ...writer,
      atomicWriteText: async (txt, opts) => {
        await writer.writeText(txt, opts);
        return writer.readOnly().stat();
      },
    });
  }

  return harden({
    ...writer,
    atomicWriteText: async (txt, opts) => {
      const scratchName = `${fileName}.${nonce || 'no-nonce'}.${
        pid || 'no-pid'
      }.scratch`;
      const scratchWriter = writer.neighbor(scratchName);
      await scratchWriter.writeText(txt, opts);
      const stats = await scratchWriter.readOnly().stat();
      await scratchWriter.rename(fileName);
      return stats;
    },
  });
}
@param {string} fileName
@param {{
  fs: Pick<import('fs'), 'existsSync'> &
    { promises: Pick<
        import('fs/promises'),
        'readFile' | 'stat' | 'writeFile' | 'mkdir' | 'rename' | 'rm'
      >,
    },
  path: Pick<import('path'), 'dirname' | 'resolve' | 'relative' | 'normalize'>,
  os: Pick<import('os'), 'platform'>,
}} io
@param {number} [pid]
@param {number} [nonce]
@param {(there: string) => ReturnType<makeAtomicFileWriter>} make