Add logic to write package.json for npm deployment types.

And improved the handling for ~ expansion in the generated script.