1 | 'use strict'
|
2 |
|
3 | const isPlainObj = require('is-plain-obj')
|
4 |
|
5 | const { isDefined } = require('./utils/remove_falsy')
|
6 |
|
7 | const bundlers = ['esbuild', 'nft', 'zisi']
|
8 | const WILDCARD_ALL = '*'
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | const normalizeFunctionsProps = function (
|
14 | { functions: v1FunctionsDirectory, ...build },
|
15 | { [WILDCARD_ALL]: wildcardProps, ...functions },
|
16 | ) {
|
17 | const functionsA = Object.entries(functions).reduce(normalizeFunctionsProp, { [WILDCARD_ALL]: wildcardProps })
|
18 | const { directory: functionsDirectory, functions: functionsB } = extractFunctionsDirectory(functionsA)
|
19 | const functionsDirectoryProps = getFunctionsDirectoryProps({ functionsDirectory, v1FunctionsDirectory })
|
20 | return { build, functions: functionsB, functionsDirectoryProps }
|
21 | }
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | const normalizeFunctionsProp = (functions, [propName, propValue]) =>
|
43 | isConfigProperty(propName) && !isConfigLeaf(propValue)
|
44 | ? { ...functions, [WILDCARD_ALL]: { [propName]: propValue, ...functions[WILDCARD_ALL] } }
|
45 | : { ...functions, [propName]: propValue }
|
46 |
|
47 | const isConfigLeaf = (functionConfig) =>
|
48 | isPlainObj(functionConfig) && Object.keys(functionConfig).every(isConfigProperty)
|
49 |
|
50 | const isConfigProperty = (propName) => FUNCTION_CONFIG_PROPERTIES.has(propName)
|
51 |
|
52 | const FUNCTION_CONFIG_PROPERTIES = new Set([
|
53 | 'directory',
|
54 | 'external_node_modules',
|
55 | 'ignored_node_modules',
|
56 | 'included_files',
|
57 | 'node_bundler',
|
58 | ])
|
59 |
|
60 |
|
61 |
|
62 |
|
63 | const extractFunctionsDirectory = ({ [WILDCARD_ALL]: { directory, ...wildcardFunctionsConfig }, ...functions }) => ({
|
64 | directory,
|
65 | functions: { ...functions, [WILDCARD_ALL]: wildcardFunctionsConfig },
|
66 | })
|
67 |
|
68 | const getFunctionsDirectoryProps = ({ functionsDirectory, v1FunctionsDirectory }) => {
|
69 | if (isDefined(functionsDirectory)) {
|
70 | return {
|
71 | functionsDirectory,
|
72 | functionsDirectoryOrigin: 'config',
|
73 | }
|
74 | }
|
75 |
|
76 | if (isDefined(v1FunctionsDirectory)) {
|
77 | return {
|
78 | functionsDirectory: v1FunctionsDirectory,
|
79 | functionsDirectoryOrigin: 'config-v1',
|
80 | }
|
81 | }
|
82 |
|
83 | return {}
|
84 | }
|
85 |
|
86 | module.exports = { normalizeFunctionsProps, bundlers, WILDCARD_ALL, FUNCTION_CONFIG_PROPERTIES }
|