Skip to content

Commit 837af58

Browse files
authored
fix: avoid modifying singleton variable (#210)
1 parent 0de7f91 commit 837af58

File tree

2 files changed

+9
-35
lines changed

2 files changed

+9
-35
lines changed

lib/index.js

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,6 @@ const strictDefaults = [
4242
'/.git',
4343
]
4444

45-
const allLevels = [
46-
// these are included by default but can be excluded by package.json files array
47-
'!/readme{,.*[^~$]}',
48-
'!/copying{,.*[^~$]}',
49-
'!/license{,.*[^~$]}',
50-
'!/licence{,.*[^~$]}',
51-
]
52-
53-
const rootOnly = [
54-
/^!.*readme/i,
55-
/^!.*copying/i,
56-
/^!.*licen[sc]e/i,
57-
]
58-
5945
const normalizePath = (path) => path.split('\\').join('/')
6046

6147
const readOutOfTreeIgnoreFiles = (root, rel, result = []) => {
@@ -141,7 +127,6 @@ class PackWalker extends IgnoreWalker {
141127
// known required files for this directory
142128
this.injectRules(strictRules, [
143129
...strictDefaults,
144-
...allLevels,
145130
...this.requiredFiles.map((file) => `!${file}`),
146131
])
147132
}
@@ -294,8 +279,11 @@ class PackWalker extends IgnoreWalker {
294279
const ignores = []
295280
const strict = [
296281
...strictDefaults,
297-
...allLevels,
298282
'!/package.json',
283+
'!/readme{,.*[^~$]}',
284+
'!/copying{,.*[^~$]}',
285+
'!/license{,.*[^~$]}',
286+
'!/licence{,.*[^~$]}',
299287
'/.git',
300288
'/node_modules',
301289
'.npmrc',
@@ -314,9 +302,6 @@ class PackWalker extends IgnoreWalker {
314302
file = file.slice(0, -1)
315303
}
316304
const inverse = `!${file}`
317-
318-
this.excludeNonRoot(file)
319-
320305
try {
321306
// if an entry in the files array is a specific file, then we need to include it as a
322307
// strict requirement for this package. if it's a directory or a pattern, it's a default
@@ -365,20 +350,6 @@ class PackWalker extends IgnoreWalker {
365350
this.injectRules(strictRules, strict, callback)
366351
}
367352

368-
// excludes non root files by checking if elements from the files array in
369-
// package.json contain an ! and readme/license/licence/copying, and then
370-
// removing readme/license/licence/copying accordingly from strict defaults
371-
excludeNonRoot (file) {
372-
// Find the pattern
373-
const matchingPattern = rootOnly.find(regex => regex.test(file))
374-
375-
if (matchingPattern) {
376-
// Find which index matches the pattern and remove it from allLevels
377-
const indexToRemove = allLevels.findIndex(element => matchingPattern.test(element))
378-
allLevels.splice(indexToRemove, 1)
379-
}
380-
}
381-
382353
// custom method: after we've finished gathering the files for the root package, we call this
383354
// before emitting the 'done' event in order to gather all of the files for bundled deps
384355
async gatherBundles () {

test/package-json-negate-non-root.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// exclude readme, license, and licnce files if package.json
1+
// exclude readme, license, and licence files if package.json
22
// files array includes !readme, !license, or !licence
33
'use strict'
44

@@ -9,7 +9,7 @@ const packlist = require('../')
99
const pkg = t.testdir({
1010
'package.json': JSON.stringify({
1111
files: [
12-
'**/*.js',
12+
'lib',
1313
'!readme.md',
1414
'!licence',
1515
'!license',
@@ -65,5 +65,8 @@ t.test('package with negated readme, licence and license files', async (t) => {
6565
'lib/a/b/c/c.js',
6666
'package.json',
6767
'readme.md',
68+
'lib/a/b/c/file.txt',
69+
'lib/a/b/file.txt',
70+
'lib/a/file.txt',
6871
])
6972
})

0 commit comments

Comments
 (0)