From c3b2105e0648ea9e2de170c8f129e5004c63e01d 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 | 23 ++++++++++----------- src/deploy/util.test.js | 44 +++++++++++------------------------------ 2 files changed, 23 insertions(+), 44 deletions(-) diff --git a/src/deploy/util.js b/src/deploy/util.js index 4525485..70729cd 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 = (filePath) => { - if (filePath == null) return false - const filename = path.basename(filePath) - switch (true) { - case filename === 'node_modules': - case filename.startsWith('.') && filename !== '.well-known': - case filename.match(/(\/__MACOSX|\/\.)/): - return false - default: - return true + 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 971af20..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(({ input, expect }) => { - t.is(defaultFilter(input), 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)) }) })