This repository has been archived by the owner on May 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
config.ts
58 lines (49 loc) · 1.8 KB
/
config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import mergeOptions from 'merge-options'
import { FunctionSource } from './function.js'
import type { NodeBundlerName } from './runtimes/node/bundlers/types.js'
import type { NodeVersionString } from './runtimes/node/index.js'
import { minimatch } from './utils/matching.js'
export interface FunctionConfig {
externalNodeModules?: string[]
includedFiles?: string[]
includedFilesBasePath?: string
ignoredNodeModules?: string[]
nodeBundler?: NodeBundlerName
nodeSourcemap?: boolean
nodeVersion?: NodeVersionString
processDynamicNodeImports?: boolean
rustTargetDirectory?: string
schedule?: string
zipGo?: boolean
}
type GlobPattern = string
export type Config = Record<GlobPattern, FunctionConfig>
export const getConfigForFunction = ({
config,
func,
}: {
config?: Config
func: Omit<FunctionSource, 'config'>
}): FunctionConfig => {
if (!config) {
return {}
}
// It's safe to mutate the array because it's local to this function.
const matches = Object.keys(config)
.filter((expression) => minimatch(func.name, expression))
.map((expression) => {
const wildcardCount = [...expression].filter((char) => char === '*').length
// The weight increases with the number of hardcoded (i.e. non-wildcard)
// characters — e.g. "netlify" has a higher weight than "net*". We do a
// subtraction of 1 if there is at least one wildcard character, so that
// "netlify" has a higher weight than "netlify*".
const weight = expression.length - wildcardCount - (wildcardCount === 0 ? 0 : 1)
return {
expression,
weight,
}
})
.sort(({ weight: weightA }, { weight: weightB }) => weightA - weightB)
.map(({ expression }) => config[expression])
return mergeOptions.apply({ concatArrays: true, ignoreUndefined: true }, matches)
}