From 944083429555d506897de7521c0a2df56e21470e Mon Sep 17 00:00:00 2001 From: ehmicky Date: Fri, 11 Dec 2020 16:24:16 +0100 Subject: [PATCH] Improve `defaultFilter` --- src/deploy/util.js | 25 ++++++++++++----------- src/deploy/util.test.js | 44 +++++++++++------------------------------ 2 files changed, 24 insertions(+), 45 deletions(-) diff --git a/src/deploy/util.js b/src/deploy/util.js index 45cd33e..9538321 100644 --- a/src/deploy/util.js +++ b/src/deploy/util.js @@ -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 @@ -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('/') ) diff --git a/src/deploy/util.test.js b/src/deploy/util.test.js index ca4d21a..96d2b18 100644 --- a/src/deploy/util.test.js +++ b/src/deploy/util.test.js @@ -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)) }) })