use writeFileAtomic instead of fs.writeFile fixes #6163

I tried to strictly follow the tasks in #6163.

This is my first attempt to contribute to npm so I might completely messed up. 
At least the tests still work for me after my commit.