Skip to content

Commit c984fb5

Browse files
wraithgarlukekarrys
authored andcommitted
feat(pack): add pack-destination config
This will allow users to specify a folder in which to save their tarballs. PR-URL: #3420 Credit: @wraithgar Close: #3420 Reviewed-by: @ruyadorno
1 parent c90612c commit c984fb5

File tree

9 files changed

+81
-9
lines changed

9 files changed

+81
-9
lines changed

docs/content/commands/npm-pack.md

+7
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ Whether or not to output JSON data, rather than the normal output.
3636

3737
Not supported by all npm commands.
3838

39+
#### `pack-destination`
40+
41+
* Default: "."
42+
* Type: String
43+
44+
Directory in which `npm pack` will save tarballs.
45+
3946
#### `workspace`
4047

4148
* Default:

docs/content/using-npm/config.md

+7
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,13 @@ when publishing or changing package permissions with `npm access`.
867867
If not set, and a registry response fails with a challenge for a one-time
868868
password, npm will prompt on the command line for one.
869869

870+
#### `pack-destination`
871+
872+
* Default: "."
873+
* Type: String
874+
875+
Directory in which `npm pack` will save tarballs.
876+
870877
#### `package`
871878

872879
* Default:

lib/pack.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const log = require('npmlog')
33
const pacote = require('pacote')
44
const libpack = require('libnpmpack')
55
const npa = require('npm-package-arg')
6+
const path = require('path')
67

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

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

2425
/* istanbul ignore next - see test/lib/load-all-commands.js */
2526
static get params () {
26-
return ['dry-run', 'json', 'workspace', 'workspaces']
27+
return [
28+
'dry-run',
29+
'json',
30+
'pack-destination',
31+
'workspace',
32+
'workspaces',
33+
]
2734
}
2835

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

7179
if (!dryRun)
72-
await writeFile(filename, tarballData)
80+
await writeFile(tarballFilename, tarballData)
7381

7482
tarballs.push(pkgContents)
7583
}

lib/utils/config/definitions.js

+8
Original file line numberDiff line numberDiff line change
@@ -1336,6 +1336,14 @@ define('package-lock-only', {
13361336
flatten,
13371337
})
13381338

1339+
define('pack-destination', {
1340+
default: '.',
1341+
type: String,
1342+
description: `
1343+
Directory in which \`npm pack\` will save tarballs.
1344+
`,
1345+
})
1346+
13391347
define('parseable', {
13401348
default: false,
13411349
type: Boolean,

tap-snapshots/test/lib/load-all-commands.js.test.cjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ Usage:
657657
npm pack [[<@scope>/]<pkg>...]
658658
659659
Options:
660-
[--dry-run] [--json]
660+
[--dry-run] [--json] [--pack-destination <pack-destination>]
661661
[-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
662662
[-ws|--workspaces]
663663

tap-snapshots/test/lib/utils/config/definitions.js.test.cjs

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ Array [
9898
"package",
9999
"package-lock",
100100
"package-lock-only",
101+
"pack-destination",
101102
"parseable",
102103
"prefer-offline",
103104
"prefer-online",

tap-snapshots/test/lib/utils/config/describe-all.js.test.cjs

+7
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,13 @@ when publishing or changing package permissions with \`npm access\`.
746746
If not set, and a registry response fails with a challenge for a one-time
747747
password, npm will prompt on the command line for one.
748748
749+
#### \`pack-destination\`
750+
751+
* Default: "."
752+
* Type: String
753+
754+
Directory in which \`npm pack\` will save tarballs.
755+
749756
#### \`package\`
750757
751758
* Default:

tap-snapshots/test/lib/utils/npm-usage.js.test.cjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ All commands:
744744
npm pack [[<@scope>/]<pkg>...]
745745
746746
Options:
747-
[--dry-run] [--json]
747+
[--dry-run] [--json] [--pack-destination <pack-destination>]
748748
[-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
749749
[-ws|--workspaces]
750750

test/lib/pack.js

+39-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const t = require('tap')
22
const mockNpm = require('../fixtures/mock-npm')
33
const pacote = require('pacote')
4+
const path = require('path')
45

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

2930
t.test('should pack current directory with no arguments', (t) => {
31+
let tarballFileName
3032
const Pack = t.mock('../../lib/pack.js', {
3133
libnpmpack,
3234
npmlog: {
@@ -35,14 +37,46 @@ t.test('should pack current directory with no arguments', (t) => {
3537
clearProgress: () => {},
3638
},
3739
fs: {
38-
writeFile: (file, data, cb) => cb(),
40+
writeFile: (file, data, cb) => {
41+
tarballFileName = file
42+
cb()
43+
},
44+
},
45+
})
46+
const npm = mockNpm({
47+
output,
48+
})
49+
const pack = new Pack(npm)
50+
51+
pack.exec([], err => {
52+
t.error(err, { bail: true })
53+
54+
const filename = `npm-${require('../../package.json').version}.tgz`
55+
t.strictSame(OUTPUT, [[filename]])
56+
t.strictSame(tarballFileName, path.resolve(filename))
57+
t.end()
58+
})
59+
})
60+
61+
t.test('follows pack-destination config', (t) => {
62+
let tarballFileName
63+
const Pack = t.mock('../../lib/pack.js', {
64+
libnpmpack,
65+
npmlog: {
66+
notice: () => {},
67+
showProgress: () => {},
68+
clearProgress: () => {},
69+
},
70+
fs: {
71+
writeFile: (file, data, cb) => {
72+
tarballFileName = file
73+
cb()
74+
},
3975
},
4076
})
4177
const npm = mockNpm({
4278
config: {
43-
unicode: false,
44-
json: false,
45-
'dry-run': false,
79+
'pack-destination': '/tmp/test',
4680
},
4781
output,
4882
})
@@ -53,10 +87,10 @@ t.test('should pack current directory with no arguments', (t) => {
5387

5488
const filename = `npm-${require('../../package.json').version}.tgz`
5589
t.strictSame(OUTPUT, [[filename]])
90+
t.strictSame(tarballFileName, path.resolve('/tmp/test', filename))
5691
t.end()
5792
})
5893
})
59-
6094
t.test('should pack given directory', (t) => {
6195
const testDir = t.testdir({
6296
'package.json': JSON.stringify({

0 commit comments

Comments
 (0)