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.
  • Loading branch information
wraithgar committed Jun 15, 2021
1 parent e5abf2a commit a4282ed
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 @@ -878,6 +878,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 getWorkspaces = require('./workspaces/get-workspaces.js')

const { getContents, logTar } = require('./utils/tar.js')
Expand All @@ -24,7 +25,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 @@ -68,9 +75,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.join(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 @@ -1339,6 +1339,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 @@ -757,6 +757,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, 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.join('/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 a4282ed

Please sign in to comment.