Skip to content
This repository was archived by the owner on Oct 10, 2022. It is now read-only.

Commit c3b2105

Browse files
committed
Improve defaultFilter
1 parent 40d04b8 commit c3b2105

File tree

2 files changed

+23
-44
lines changed

2 files changed

+23
-44
lines changed

src/deploy/util.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1-
const path = require('path')
1+
const { basename, sep } = require('path')
22

33
const pWaitFor = require('p-wait-for')
44

55
// Default filter when scanning for files
66
const defaultFilter = (filePath) => {
7-
if (filePath == null) return false
8-
const filename = path.basename(filePath)
9-
switch (true) {
10-
case filename === 'node_modules':
11-
case filename.startsWith('.') && filename !== '.well-known':
12-
case filename.match(/(\/__MACOSX|\/\.)/):
13-
return false
14-
default:
15-
return true
7+
if (filePath == null) {
8+
return false
169
}
10+
11+
const filename = basename(filePath)
12+
return (
13+
filename !== 'node_modules' &&
14+
!(filename.startsWith('.') && filename !== '.well-known') &&
15+
!filename.includes('/__MACOSX') &&
16+
!filename.includes('/.')
17+
)
1718
}
1819

1920
// normalize windows paths to unix paths
@@ -23,7 +24,7 @@ const normalizePath = (relname) => {
2324
}
2425
return (
2526
relname
26-
.split(path.sep)
27+
.split(sep)
2728
// .map(segment => encodeURI(segment)) // TODO I'm fairly certain we shouldn't encodeURI here, thats only for the file upload step
2829
.join('/')
2930
)

src/deploy/util.test.js

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,38 +16,16 @@ test('normalizePath should throw the error if name is invalid', (t) => {
1616
t.throws(() => normalizePath('#'))
1717
})
1818

19-
test('pass empty name to defaultFilter', (t) => {
20-
const cases = [
21-
{
22-
input: null,
23-
expect: false,
24-
},
25-
{
26-
input: undefined,
27-
expect: false,
28-
},
29-
{
30-
input: 'foo/bar/baz.js',
31-
expect: true,
32-
},
33-
{
34-
input: 'directory/node_modules',
35-
expect: false,
36-
},
37-
{
38-
input: 'directory/.gitignore',
39-
expect: false,
40-
},
41-
{
42-
input: 'directory/.well-known',
43-
expect: true,
44-
},
45-
{
46-
input: '__MACOSX',
47-
expect: true,
48-
},
49-
]
50-
cases.forEach(({ input, expect }) => {
51-
t.is(defaultFilter(input), expect)
19+
const filteredFiles = ['foo/bar/baz.js', 'directory/.well-known', '__MACOSX']
20+
filteredFiles.forEach((filePath) => {
21+
test(`filters ${filePath}`, (t) => {
22+
t.true(defaultFilter(filePath))
23+
})
24+
})
25+
26+
const unfilteredFiles = [null, undefined, 'directory/node_modules', 'directory/.gitignore']
27+
unfilteredFiles.forEach((filePath) => {
28+
test(`does not filter ${filePath}`, (t) => {
29+
t.false(defaultFilter(filePath))
5230
})
5331
})

0 commit comments

Comments
 (0)