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

Commit

Permalink
Improve defaultFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Dec 11, 2020
1 parent 64429cb commit 9440834
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 45 deletions.
25 changes: 13 additions & 12 deletions src/deploy/util.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
const path = require('path')
const { basename, sep } = require('path')

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

// Default filter when scanning for files
const defaultFilter = (filename) => {
if (filename == null) return false
const n = path.basename(filename)
switch (true) {
case n === 'node_modules':
case n.startsWith('.') && n !== '.well-known':
case n.match(/(\/__MACOSX|\/\.)/):
return false
default:
return true
const defaultFilter = (filePath) => {
if (filePath == null) {
return false
}

const filename = basename(filePath)
return (
filename !== 'node_modules' &&
!(filename.startsWith('.') && filename !== '.well-known') &&
!filename.includes('/__MACOSX') &&
!filename.includes('/.')
)
}

// normalize windows paths to unix paths
Expand All @@ -23,7 +24,7 @@ const normalizePath = (relname) => {
}
return (
relname
.split(path.sep)
.split(sep)
// .map(segment => encodeURI(segment)) // TODO I'm fairly certain we shouldn't encodeURI here, thats only for the file upload step
.join('/')
)
Expand Down
44 changes: 11 additions & 33 deletions src/deploy/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,16 @@ test('normalizePath should throw the error if name is invalid', (t) => {
t.throws(() => normalizePath('#'))
})

test('pass empty name to defaultFilter', (t) => {
const cases = [
{
input: null,
expect: false,
},
{
input: undefined,
expect: false,
},
{
input: 'foo/bar/baz.js',
expect: true,
},
{
input: 'directory/node_modules',
expect: false,
},
{
input: 'directory/.gitignore',
expect: false,
},
{
input: 'directory/.well-known',
expect: true,
},
{
input: '__MACOSX',
expect: true,
},
]
cases.forEach((c) => {
t.is(defaultFilter(c.input), c.expect)
const filteredFiles = ['foo/bar/baz.js', 'directory/.well-known', '__MACOSX']
filteredFiles.forEach((filePath) => {
test(`filters ${filePath}`, (t) => {
t.true(defaultFilter(filePath))
})
})

const unfilteredFiles = [null, undefined, 'directory/node_modules', 'directory/.gitignore']
unfilteredFiles.forEach((filePath) => {
test(`does not filter ${filePath}`, (t) => {
t.false(defaultFilter(filePath))
})
})

0 comments on commit 9440834

Please sign in to comment.