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
/
main.ts
138 lines (115 loc) · 4.38 KB
/
main.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { extname } from 'path'
import { Config } from './config.js'
import { FeatureFlags, getFlags } from './feature_flags.js'
import { FunctionSource } from './function.js'
import { getFunctionFromPath, getFunctionsFromPaths } from './runtimes/index.js'
import { findISCDeclarationsInPath, ISCValues } from './runtimes/node/in_source_config/index.js'
import { GetSrcFilesFunction, RuntimeName } from './runtimes/runtime.js'
import { listFunctionsDirectories, resolveFunctionsDirectories } from './utils/fs.js'
export { zipFunction, zipFunctions } from './zip.js'
interface ListedFunction {
name: string
mainFile: string
runtime: RuntimeName
extension: string
schedule?: string
}
type ListedFunctionFile = ListedFunction & {
srcFile: string
}
interface ListFunctionsOptions {
basePath?: string
config?: Config
featureFlags?: FeatureFlags
parseISC?: boolean
}
interface AugmentedFunctionSource extends FunctionSource {
inSourceConfig?: ISCValues
}
const augmentWithISC = async (func: FunctionSource): Promise<AugmentedFunctionSource> => {
// ISC is currently only supported in JavaScript and TypeScript functions.
if (func.runtime.name !== 'js') {
return func
}
const inSourceConfig = await findISCDeclarationsInPath(func.mainFile, func.name)
return { ...func, inSourceConfig }
}
// List all Netlify Functions main entry files for a specific directory
export const listFunctions = async function (
relativeSrcFolders: string | string[],
{
featureFlags: inputFeatureFlags,
config,
parseISC = false,
}: { featureFlags?: FeatureFlags; config?: Config; parseISC?: boolean } = {},
) {
const featureFlags = getFlags(inputFeatureFlags)
const srcFolders = resolveFunctionsDirectories(relativeSrcFolders)
const paths = await listFunctionsDirectories(srcFolders)
const functionsMap = await getFunctionsFromPaths(paths, { featureFlags, config })
const functions = [...functionsMap.values()]
const augmentedFunctions = parseISC ? await Promise.all(functions.map(augmentWithISC)) : functions
return augmentedFunctions.map(getListedFunction)
}
// Finds a function at a specific path.
export const listFunction = async function (
path: string,
{
featureFlags: inputFeatureFlags,
config,
parseISC = false,
}: { featureFlags?: FeatureFlags; config?: Config; parseISC?: boolean } = {},
) {
const featureFlags = getFlags(inputFeatureFlags)
const func = await getFunctionFromPath(path, { featureFlags, config })
if (!func) {
return
}
const augmentedFunction = parseISC ? await augmentWithISC(func) : func
return getListedFunction(augmentedFunction)
}
// List all Netlify Functions files for a specific directory
export const listFunctionsFiles = async function (
relativeSrcFolders: string | string[],
{ basePath, config, featureFlags: inputFeatureFlags, parseISC = false }: ListFunctionsOptions = {},
) {
const featureFlags = getFlags(inputFeatureFlags)
const srcFolders = resolveFunctionsDirectories(relativeSrcFolders)
const paths = await listFunctionsDirectories(srcFolders)
const functionsMap = await getFunctionsFromPaths(paths, { config, featureFlags })
const functions = [...functionsMap.values()]
const augmentedFunctions = parseISC ? await Promise.all(functions.map(augmentWithISC)) : functions
const listedFunctionsFiles = await Promise.all(
augmentedFunctions.map((func) => getListedFunctionFiles(func, { basePath, featureFlags })),
)
return listedFunctionsFiles.flat()
}
const getListedFunction = function ({
runtime,
name,
mainFile,
extension,
config,
inSourceConfig,
}: AugmentedFunctionSource): ListedFunction {
return { name, mainFile, runtime: runtime.name, extension, schedule: inSourceConfig?.schedule ?? config.schedule }
}
const getListedFunctionFiles = async function (
func: AugmentedFunctionSource,
options: { basePath?: string; featureFlags: FeatureFlags },
): Promise<ListedFunctionFile[]> {
const srcFiles = await getSrcFiles({ ...func, ...options })
return srcFiles.map((srcFile) => ({ ...getListedFunction(func), srcFile, extension: extname(srcFile) }))
}
const getSrcFiles: GetSrcFilesFunction = async function ({ extension, runtime, srcPath, ...args }) {
const { getSrcFiles: getRuntimeSrcFiles } = runtime
if (extension === '.zip' || typeof getRuntimeSrcFiles !== 'function') {
return [srcPath]
}
return await getRuntimeSrcFiles({
extension,
runtime,
srcPath,
...args,
})
}