UNPKG

3.18 kBJavaScriptView Raw
1'use strict'
2
3const isPlainObj = require('is-plain-obj')
4
5const { isDefined } = require('./utils/remove_falsy')
6
7const bundlers = ['esbuild', 'nft', 'zisi']
8const WILDCARD_ALL = '*'
9
10// Removing the legacy `functions` from the `build` block.
11// Looking for a default directory in the `functions` block, separating it
12// from the rest of the configuration if it exists.
13const 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// Normalizes a functions configuration object, so that the first level of keys
24// represents function expressions mapped to a configuration object.
25//
26// Example input:
27// {
28// "external_node_modules": ["one"],
29// "my-function": { "external_node_modules": ["two"] }
30// }
31//
32// Example output:
33// {
34// "*": { "external_node_modules": ["one"] },
35// "my-function": { "external_node_modules": ["two"] }
36// }
37// If `prop` is one of `configProperties``, we might be dealing with a
38// top-level property (i.e. one that targets all functions). We consider
39// that to be the case if the value is an object where all keys are also
40// config properties. If not, it's simply a function with the same name
41// as one of the config properties.
42const normalizeFunctionsProp = (functions, [propName, propValue]) =>
43 isConfigProperty(propName) && !isConfigLeaf(propValue)
44 ? { ...functions, [WILDCARD_ALL]: { [propName]: propValue, ...functions[WILDCARD_ALL] } }
45 : { ...functions, [propName]: propValue }
46
47const isConfigLeaf = (functionConfig) =>
48 isPlainObj(functionConfig) && Object.keys(functionConfig).every(isConfigProperty)
49
50const isConfigProperty = (propName) => FUNCTION_CONFIG_PROPERTIES.has(propName)
51
52const FUNCTION_CONFIG_PROPERTIES = new Set([
53 'directory',
54 'external_node_modules',
55 'ignored_node_modules',
56 'included_files',
57 'node_bundler',
58])
59
60// Takes a functions configuration object and looks for the functions directory
61// definition, returning it under the `directory` key. The rest of the config
62// object is returned under the `functions` key.
63const extractFunctionsDirectory = ({ [WILDCARD_ALL]: { directory, ...wildcardFunctionsConfig }, ...functions }) => ({
64 directory,
65 functions: { ...functions, [WILDCARD_ALL]: wildcardFunctionsConfig },
66})
67
68const 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
86module.exports = { normalizeFunctionsProps, bundlers, WILDCARD_ALL, FUNCTION_CONFIG_PROPERTIES }