Generates a hash for an installed npm package, useful for salting caches.
AVA for example caches precompiled test
files. It generates a salt for its cache based on the various packages that are
used when compiling the test files.
package-hash can generate an appropriate hash based on the package location
(on disk) and the package.json file. This hash is salted with a hash
for the package-hash itself.
package-hash can detect when the package-to-be-hashed is a Git repository. In
the AVA example this is useful when you're debugging one of the packages used to
compile the test files. You can clone it locally and use npm link so AVA can
find the clone. The hash will include the HEAD (.git/HEAD) and its
corresponding ref (e.g. .git/refs/heads/master), any packed refs
(.git/packed-refs), as well as the diff (git diff) for any non-committed
changes. This makes it really easy to test your changes without having to
explicitly clear the cache in the parent project.
packageHash() / packageHash.sync() must be called with a file path for an
existing package.json file. To get the path to an npm package it's easiest to
use require.resolve('the-name/package.json').