Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a4282ed

Browse files
committedJun 15, 2021
feat(pack): add pack-destination config
This will allow users to specify a folder in which to save their tarballs.
1 parent e5abf2a commit a4282ed

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
@@ -878,6 +878,13 @@ when publishing or changing package permissions with `npm access`.
878878
If not set, and a registry response fails with a challenge for a one-time
879879
password, npm will prompt on the command line for one.
880880

881+
#### `pack-destination`
882+
883+
* Default: "."
884+
* Type: String
885+
886+
Directory in which `npm pack` will save tarballs.
887+
881888
#### `package`
882889

883890
* 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
const getWorkspaces = require('./workspaces/get-workspaces.js')
78

89
const { getContents, logTar } = require('./utils/tar.js')
@@ -24,7 +25,13 @@ class Pack extends BaseCommand {
2425

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

3037
/* istanbul ignore next - see test/lib/load-all-commands.js */
@@ -68,9 +75,10 @@ class Pack extends BaseCommand {
6875
for (const { arg, filename, manifest } of manifests) {
6976
const tarballData = await libpack(arg, this.npm.flatOptions)
7077
const pkgContents = await getContents(manifest, tarballData)
78+
const tarballFilename = path.join(this.npm.config.get('pack-destination'), filename)
7179

7280
if (!dryRun)
73-
await writeFile(filename, tarballData)
81+
await writeFile(tarballFilename, tarballData)
7482

7583
tarballs.push(pkgContents)
7684
}

‎lib/utils/config/definitions.js

+8
Original file line numberDiff line numberDiff line change
@@ -1339,6 +1339,14 @@ define('package-lock-only', {
13391339
flatten,
13401340
})
13411341

1342+
define('pack-destination', {
1343+
default: '.',
1344+
type: String,
1345+
description: `
1346+
Directory in which \`npm pack\` will save tarballs.
1347+
`,
1348+
})
1349+
13421350
define('parseable', {
13431351
default: false,
13441352
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
@@ -757,6 +757,13 @@ when publishing or changing package permissions with \`npm access\`.
757757
If not set, and a registry response fails with a challenge for a one-time
758758
password, npm will prompt on the command line for one.
759759
760+
#### \`pack-destination\`
761+
762+
* Default: "."
763+
* Type: String
764+
765+
Directory in which \`npm pack\` will save tarballs.
766+
760767
#### \`package\`
761768
762769
* 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, 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.join('/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)
Please sign in to comment.