Add --output option to npm-pack

Currently, npm-pack will create a tarball file in the current working directory with the following name:

<pkg.name>-<pkg.version>.tgz

Because the version is always changing, this creates difficulty for automated shell scripts (without JSON parsing).

Also, as others have described in #4074, sometimes it may be desirable to create the tarball in another directory. I have to include an npm-script to do this:

{
  "scripts": {
    "pack": "(cd output/ && exec npm pack ../)",
    "pack:rename": "(cd output/ && mv \"$(npm pack ../ | tail -n 1)\" package.tgz)"
  }
}

My suggestion is, npm-pack can add a new -O, --output option, and use it as the destination, instead of sticking to the default generated name. This shouldn’t be too hard to change and it will make scripting deployment easier.

Example:

$ npm pack -O output/package.tgz && ls output/
package.tgz

Semver lex-sorts correctly so couldn’t you just ls (or find as appropriate) with sort options and pipe to head or tail, to find the most recent one? Of course this could break down if you’re trying to build an older version or whatever, but it works for most cases.

I’m not saying the proposed option is a bad idea, certainly, but there are workarounds.

If you look at my script closely you can actually see my workaround - the last line of output from npm pack will be the file name, so npm pack ../ | tail -n 1 captures the file name and pass it to mv. But it’s a workaround after all.

Semver doesn’t properly lex sort though. 1.9.0 vs 1.10.0 for example.