Skip to content

Commit

Permalink
fix: avoid modifying singleton variable (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
mohd-akram authored Jan 3, 2024
1 parent 0de7f91 commit 837af58
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 35 deletions.
37 changes: 4 additions & 33 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,6 @@ const strictDefaults = [
'/.git',
]

const allLevels = [
// these are included by default but can be excluded by package.json files array
'!/readme{,.*[^~$]}',
'!/copying{,.*[^~$]}',
'!/license{,.*[^~$]}',
'!/licence{,.*[^~$]}',
]

const rootOnly = [
/^!.*readme/i,
/^!.*copying/i,
/^!.*licen[sc]e/i,
]

const normalizePath = (path) => path.split('\\').join('/')

const readOutOfTreeIgnoreFiles = (root, rel, result = []) => {
Expand Down Expand Up @@ -141,7 +127,6 @@ class PackWalker extends IgnoreWalker {
// known required files for this directory
this.injectRules(strictRules, [
...strictDefaults,
...allLevels,
...this.requiredFiles.map((file) => `!${file}`),
])
}
Expand Down Expand Up @@ -294,8 +279,11 @@ class PackWalker extends IgnoreWalker {
const ignores = []
const strict = [
...strictDefaults,
...allLevels,
'!/package.json',
'!/readme{,.*[^~$]}',
'!/copying{,.*[^~$]}',
'!/license{,.*[^~$]}',
'!/licence{,.*[^~$]}',
'/.git',
'/node_modules',
'.npmrc',
Expand All @@ -314,9 +302,6 @@ class PackWalker extends IgnoreWalker {
file = file.slice(0, -1)
}
const inverse = `!${file}`

this.excludeNonRoot(file)

try {
// if an entry in the files array is a specific file, then we need to include it as a
// strict requirement for this package. if it's a directory or a pattern, it's a default
Expand Down Expand Up @@ -365,20 +350,6 @@ class PackWalker extends IgnoreWalker {
this.injectRules(strictRules, strict, callback)
}

// excludes non root files by checking if elements from the files array in
// package.json contain an ! and readme/license/licence/copying, and then
// removing readme/license/licence/copying accordingly from strict defaults
excludeNonRoot (file) {
// Find the pattern
const matchingPattern = rootOnly.find(regex => regex.test(file))

if (matchingPattern) {
// Find which index matches the pattern and remove it from allLevels
const indexToRemove = allLevels.findIndex(element => matchingPattern.test(element))
allLevels.splice(indexToRemove, 1)
}
}

// custom method: after we've finished gathering the files for the root package, we call this
// before emitting the 'done' event in order to gather all of the files for bundled deps
async gatherBundles () {
Expand Down
7 changes: 5 additions & 2 deletions test/package-json-negate-non-root.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// exclude readme, license, and licnce files if package.json
// exclude readme, license, and licence files if package.json
// files array includes !readme, !license, or !licence
'use strict'

Expand All @@ -9,7 +9,7 @@ const packlist = require('../')
const pkg = t.testdir({
'package.json': JSON.stringify({
files: [
'**/*.js',
'lib',
'!readme.md',
'!licence',
'!license',
Expand Down Expand Up @@ -65,5 +65,8 @@ t.test('package with negated readme, licence and license files', async (t) => {
'lib/a/b/c/c.js',
'package.json',
'readme.md',
'lib/a/b/c/file.txt',
'lib/a/b/file.txt',
'lib/a/file.txt',
])
})

0 comments on commit 837af58

Please sign in to comment.