Make npm bundling safe to be run in parallel

Instead of modifying the contents of the .npm directory
inside a package directory, construct a new temporary
directory and atomically replace at the end.

Also, some minor improvements to testing and pruning
unused packages