Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(pack): add pack-destination config #3420

Merged
merged 1 commit into from Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/content/commands/npm-pack.md
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
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
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
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
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
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
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
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
@@ -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