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

fix: avoid modifying singleton variable #210

Merged
merged 1 commit into from
Jan 3, 2024
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
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',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What code path is this change testing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same code path. This glob wasn't sufficient to trigger the test case, as it implicitly excludes the non-root readme/license files already.

'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',
])
})