From c6f2b69b025575dc683f26f3d098e23a22c462da Mon Sep 17 00:00:00 2001 From: Luke Karrys Date: Wed, 7 Dec 2022 10:15:10 -0700 Subject: [PATCH] fix: treat glob the same as globstar (#147) This makes `directory/*` work the same as `directory/**` to maintain backwards compatibility with previous versions of `npm pack`. Ref: https://github.com/npm/cli/issues/5918 --- lib/index.js | 2 ++ test/package-json-directory-glob.js | 42 +++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 test/package-json-directory-glob.js diff --git a/lib/index.js b/lib/index.js index 91606e4..2d6504d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -300,6 +300,8 @@ class PackWalker extends IgnoreWalker { file = file.slice(1) } else if (file.startsWith('./')) { file = file.slice(2) + } else if (file.endsWith('/*')) { + file = file.slice(0, -2) } const inverse = `!${file}` try { diff --git a/test/package-json-directory-glob.js b/test/package-json-directory-glob.js new file mode 100644 index 0000000..48b228d --- /dev/null +++ b/test/package-json-directory-glob.js @@ -0,0 +1,42 @@ +'use strict' + +const Arborist = require('@npmcli/arborist') +const t = require('tap') +const packlist = require('..') + +const createTestdir = (...files) => t.testdir({ + 'package.json': JSON.stringify({ + files, + }), + folder: { + one: { file: 'one' }, + two: { file: 'two' }, + }, + folder1: { + one: { file: 'one' }, + two: { file: 'two' }, + }, +}) + +t.test('package json directory glob', async (t) => { + const pkgFiles = [ + 'folder', + 'folder/', + 'folder/*', + 'folder/**', + ] + + for (const files of pkgFiles) { + await t.test(files, async t => { + const pkg = createTestdir(files) + const arborist = new Arborist({ path: pkg }) + const tree = await arborist.loadActual() + const res = await packlist(tree) + t.same(res, [ + 'folder/one/file', + 'folder/two/file', + 'package.json', + ]) + }) + } +})