Skip to content

Commit 83b750d

Browse files
fix: exempt explicit pack targets from allow-directory (#9916)
Backport of #9756 to `release/v11`. Co-authored-by: Yashas Gunderia <[email protected]> Co-authored-by: ychampion <[email protected]>
1 parent 4791b27 commit 83b750d

5 files changed

Lines changed: 58 additions & 0 deletions

File tree

‎lib/commands/pack.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class Pack extends BaseCommand {
4545
) ? { ...this.npm.flatOptions, before: null } : this.npm.flatOptions
4646
const manifest = await pacote.manifest(spec, {
4747
...options,
48+
...(spec.type === 'directory' && { allowDirectory: 'all' }),
4849
Arborist,
4950
preferOnline: true,
5051
_isRoot: true,

‎test/lib/commands/pack.js‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,25 @@ t.test('dry run', async t => {
113113
t.throws(() => fs.statSync(path.resolve(npm.prefix, filename)))
114114
})
115115

116+
for (const allowDirectory of ['none', 'root']) {
117+
t.test(`dry run with allow-directory=${allowDirectory}`, async t => {
118+
const { npm, outputs } = await loadMockNpm(t, {
119+
prefixDir: {
120+
'package.json': JSON.stringify({
121+
name: 'test-package',
122+
version: '1.0.0',
123+
}),
124+
},
125+
config: {
126+
'allow-directory': allowDirectory,
127+
'dry-run': true,
128+
},
129+
})
130+
await npm.exec('pack', [])
131+
t.strictSame(outputs, ['test-package-1.0.0.tgz'])
132+
})
133+
}
134+
116135
t.test('foreground-scripts defaults to true', async t => {
117136
const { npm, outputs, logs } = await loadMockNpm(t, {
118137
prefixDir: {

‎test/lib/commands/publish.js‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,25 @@ t.test('dry-run', async t => {
151151
t.matchSnapshot(logs.notice)
152152
})
153153

154+
for (const allowDirectory of ['none', 'root']) {
155+
t.test(`dry-run with allow-directory=${allowDirectory}`, async t => {
156+
const { joinedOutput, npm, registry } = await loadNpmWithRegistry(t, {
157+
config: {
158+
'allow-directory': allowDirectory,
159+
'dry-run': true,
160+
...auth,
161+
},
162+
prefixDir: {
163+
'package.json': JSON.stringify(pkgJson, null, 2),
164+
},
165+
authorization: token,
166+
})
167+
registry.publish(pkg, { noPut: true })
168+
await npm.exec('publish', [])
169+
t.equal(joinedOutput(), `+ ${pkg}@1.0.0`)
170+
})
171+
}
172+
154173
t.test('foreground-scripts defaults to true', async t => {
155174
const { outputs, npm, logs, registry } = await loadNpmWithRegistry(t, {
156175
config: {

‎workspaces/libnpmpack/lib/index.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ async function pack (spec = 'file:.', opts = {}) {
1212
// gets spec
1313
spec = npa(spec)
1414

15+
// An explicit directory is the package being packed, not a dependency fetch.
16+
if (spec.type === 'directory') {
17+
opts = { ...opts, allowDirectory: 'all' }
18+
}
19+
1520
const manifest = await pacote.manifest(spec, { ...opts, Arborist, _isRoot: true })
1621

1722
const stdio = opts.foregroundScripts ? 'inherit' : 'pipe'

‎workspaces/libnpmpack/test/index.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,20 @@ t.test('packs from local directory', async t => {
3636
})
3737
})
3838

39+
for (const allowDirectory of ['none', 'root']) {
40+
t.test(`packs an explicit local directory with allow-directory=${allowDirectory}`, async t => {
41+
const testDir = t.testdir({
42+
'package.json': JSON.stringify({
43+
name: 'my-cool-pkg',
44+
version: '1.0.0',
45+
}, null, 2),
46+
})
47+
48+
const tarball = await pack(testDir, { allowDirectory })
49+
t.ok(tarball)
50+
})
51+
}
52+
3953
t.test('flattens path separators in name so tarball stays in packDestination', async t => {
4054
const testDir = t.testdir({
4155
src: {

0 commit comments

Comments
 (0)