Skip to content

Commit

Permalink
feat(pack): add pack-destination config
Browse files Browse the repository at this point in the history
This will allow users to specify a folder in which to save their
tarballs.

PR-URL: #3420
Credit: @wraithgar
Close: #3420
Reviewed-by: @ruyadorno
  • Loading branch information
wraithgar authored and lukekarrys committed Jun 17, 2021
1 parent c90612c commit c984fb5
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 9 deletions.
7 changes: 7 additions & 0 deletions docs/content/commands/npm-pack.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ Whether or not to output JSON data, rather than the normal output.

Not supported by all npm commands.

#### `pack-destination`

* Default: "."
* Type: String

Directory in which `npm pack` will save tarballs.

#### `workspace`

* Default:
Expand Down
7 changes: 7 additions & 0 deletions docs/content/using-npm/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,13 @@ when publishing or changing package permissions with `npm access`.
If not set, and a registry response fails with a challenge for a one-time
password, npm will prompt on the command line for one.

#### `pack-destination`

* Default: "."
* Type: String

Directory in which `npm pack` will save tarballs.

#### `package`

* Default:
Expand Down
12 changes: 10 additions & 2 deletions lib/pack.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const log = require('npmlog')
const pacote = require('pacote')
const libpack = require('libnpmpack')
const npa = require('npm-package-arg')
const path = require('path')

const { getContents, logTar } = require('./utils/tar.js')

Expand All @@ -23,7 +24,13 @@ class Pack extends BaseCommand {

/* istanbul ignore next - see test/lib/load-all-commands.js */
static get params () {
return ['dry-run', 'json', 'workspace', 'workspaces']
return [
'dry-run',
'json',
'pack-destination',
'workspace',
'workspaces',
]
}

/* istanbul ignore next - see test/lib/load-all-commands.js */
Expand Down Expand Up @@ -67,9 +74,10 @@ class Pack extends BaseCommand {
for (const { arg, filename, manifest } of manifests) {
const tarballData = await libpack(arg, this.npm.flatOptions)
const pkgContents = await getContents(manifest, tarballData)
const tarballFilename = path.resolve(this.npm.config.get('pack-destination'), filename)

if (!dryRun)
await writeFile(filename, tarballData)
await writeFile(tarballFilename, tarballData)

tarballs.push(pkgContents)
}
Expand Down
8 changes: 8 additions & 0 deletions lib/utils/config/definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1336,6 +1336,14 @@ define('package-lock-only', {
flatten,
})

define('pack-destination', {
default: '.',
type: String,
description: `
Directory in which \`npm pack\` will save tarballs.
`,
})

define('parseable', {
default: false,
type: Boolean,
Expand Down
2 changes: 1 addition & 1 deletion tap-snapshots/test/lib/load-all-commands.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ Usage:
npm pack [[<@scope>/]<pkg>...]
Options:
[--dry-run] [--json]
[--dry-run] [--json] [--pack-destination <pack-destination>]
[-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
[-ws|--workspaces]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ Array [
"package",
"package-lock",
"package-lock-only",
"pack-destination",
"parseable",
"prefer-offline",
"prefer-online",
Expand Down
7 changes: 7 additions & 0 deletions tap-snapshots/test/lib/utils/config/describe-all.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,13 @@ when publishing or changing package permissions with \`npm access\`.
If not set, and a registry response fails with a challenge for a one-time
password, npm will prompt on the command line for one.
#### \`pack-destination\`
* Default: "."
* Type: String
Directory in which \`npm pack\` will save tarballs.
#### \`package\`
* Default:
Expand Down
2 changes: 1 addition & 1 deletion tap-snapshots/test/lib/utils/npm-usage.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ All commands:
npm pack [[<@scope>/]<pkg>...]
Options:
[--dry-run] [--json]
[--dry-run] [--json] [--pack-destination <pack-destination>]
[-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
[-ws|--workspaces]
Expand Down
44 changes: 39 additions & 5 deletions test/lib/pack.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const t = require('tap')
const mockNpm = require('../fixtures/mock-npm')
const pacote = require('pacote')
const path = require('path')

const OUTPUT = []
const output = (...msg) => OUTPUT.push(msg)
Expand All @@ -27,6 +28,7 @@ const mockPacote = {
t.afterEach(() => OUTPUT.length = 0)

t.test('should pack current directory with no arguments', (t) => {
let tarballFileName
const Pack = t.mock('../../lib/pack.js', {
libnpmpack,
npmlog: {
Expand All @@ -35,14 +37,46 @@ t.test('should pack current directory with no arguments', (t) => {
clearProgress: () => {},
},
fs: {
writeFile: (file, data, cb) => cb(),
writeFile: (file, data, cb) => {
tarballFileName = file
cb()
},
},
})
const npm = mockNpm({
output,
})
const pack = new Pack(npm)

pack.exec([], err => {
t.error(err, { bail: true })

const filename = `npm-${require('../../package.json').version}.tgz`
t.strictSame(OUTPUT, [[filename]])
t.strictSame(tarballFileName, path.resolve(filename))
t.end()
})
})

t.test('follows pack-destination config', (t) => {
let tarballFileName
const Pack = t.mock('../../lib/pack.js', {
libnpmpack,
npmlog: {
notice: () => {},
showProgress: () => {},
clearProgress: () => {},
},
fs: {
writeFile: (file, data, cb) => {
tarballFileName = file
cb()
},
},
})
const npm = mockNpm({
config: {
unicode: false,
json: false,
'dry-run': false,
'pack-destination': '/tmp/test',
},
output,
})
Expand All @@ -53,10 +87,10 @@ t.test('should pack current directory with no arguments', (t) => {

const filename = `npm-${require('../../package.json').version}.tgz`
t.strictSame(OUTPUT, [[filename]])
t.strictSame(tarballFileName, path.resolve('/tmp/test', filename))
t.end()
})
})

t.test('should pack given directory', (t) => {
const testDir = t.testdir({
'package.json': JSON.stringify({
Expand Down

0 comments on commit c984fb5

Please sign in to comment.