Skip to content

Commit

Permalink
fix: remove duplicate functions and let .js take precedence (#359)
Browse files Browse the repository at this point in the history
* feat: remove duplicate functions and let .js take precedence

* fix: use entire extensions list to check function precedence

* fix: filter out function files based on extension before parsing them

* fix: filter out function files based on extension before parsing them

* fix: filter out function files based on extension before parsing them
  • Loading branch information
khendrikse committed Apr 6, 2023
1 parent f217fae commit bc52282
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
20 changes: 20 additions & 0 deletions node/finder.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { test, expect } from 'vitest'

import { removeDuplicatesByExtension } from './finder.js'

test('filters out any duplicate files based on the extension', () => {
const functions = [
'file1.js',
'file1.ts',
'file2.tsx',
'file2.jsx',
'file3.tsx',
'file3.js',
'file4.ts',
'file5.ts',
'file5.tsx',
]
const expected = ['file1.js', 'file2.jsx', 'file3.js', 'file4.ts', 'file5.ts']

expect(removeDuplicatesByExtension(functions)).toStrictEqual(expected)
})
34 changes: 27 additions & 7 deletions node/finder.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,36 @@
import { promises as fs } from 'fs'
import { basename, extname, join } from 'path'
import { basename, extname, join, parse } from 'path'

import { EdgeFunction } from './edge_function.js'
import { nonNullable } from './utils/non_nullable.js'

const ALLOWED_EXTENSIONS = new Set(['.js', '.jsx', '.ts', '.tsx'])
// the order of the allowed extensions is also the order we remove duplicates
// with a lower index meaning a higher precedence over the others
const ALLOWED_EXTENSIONS = ['.js', '.jsx', '.ts', '.tsx']

export const removeDuplicatesByExtension = (functions: string[]) => {
const seen = new Map()

return Object.values(
functions.reduce((acc, path) => {
const { ext, name } = parse(path)
const extIndex = ALLOWED_EXTENSIONS.indexOf(ext)

if (!seen.has(name) || seen.get(name) > extIndex) {
seen.set(name, extIndex)
return { ...acc, [name]: path }
}

return acc
}, {}),
) as string[]
}

const findFunctionInDirectory = async (directory: string): Promise<EdgeFunction | undefined> => {
const name = basename(directory)
const candidatePaths = [...ALLOWED_EXTENSIONS]
.flatMap((extension) => [`${name}${extension}`, `index${extension}`])
.map((filename) => join(directory, filename))
const candidatePaths = ALLOWED_EXTENSIONS.flatMap((extension) => [`${name}${extension}`, `index${extension}`]).map(
(filename) => join(directory, filename),
)

let functionPath

Expand Down Expand Up @@ -48,7 +68,7 @@ const findFunctionInPath = async (path: string): Promise<EdgeFunction | undefine

const extension = extname(path)

if (ALLOWED_EXTENSIONS.has(extension)) {
if (ALLOWED_EXTENSIONS.includes(extension)) {
return { name: basename(path, extension), path }
}
}
Expand All @@ -57,7 +77,7 @@ const findFunctionsInDirectory = async (baseDirectory: string) => {
let items: string[] = []

try {
items = await fs.readdir(baseDirectory)
items = await fs.readdir(baseDirectory).then(removeDuplicatesByExtension)
} catch {
// no-op
}
Expand Down

0 comments on commit bc52282

Please sign in to comment.