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
/
index.ts
57 lines (43 loc) · 1.86 KB
/
index.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
import { ArgumentPlaceholder, Expression, SpreadElement, JSXNamespacedName } from '@babel/types'
import { nonNullable } from '../../../utils/non_nullable.js'
import { createBindingsMethod } from '../parser/bindings.js'
import { getMainExport } from '../parser/exports.js'
import { getImports } from '../parser/imports.js'
import { safelyParseFile } from '../parser/index.js'
import { parse as parseSchedule } from './properties/schedule.js'
export const IN_SOURCE_CONFIG_MODULE = '@netlify/functions'
export type ISCValues = Partial<ReturnType<typeof parseSchedule>>
// Parses a JS/TS file and looks for in-source config declarations. It returns
// an array of all declarations found, with `property` indicating the name of
// the property and `data` its value.
export const findISCDeclarationsInPath = async (sourcePath: string): Promise<ISCValues> => {
const ast = await safelyParseFile(sourcePath)
if (ast === null) {
return {}
}
const imports = ast.body.flatMap((node) => getImports(node, IN_SOURCE_CONFIG_MODULE))
const getAllBindings = createBindingsMethod(ast.body)
const mainExports = getMainExport(ast.body, getAllBindings)
const iscExports = mainExports
.map(({ args, local: exportName }) => {
const matchingImport = imports.find(({ local: importName }) => importName === exportName)
if (matchingImport === undefined) {
return null
}
switch (matchingImport.imported) {
case 'schedule':
return parseSchedule({ args }, getAllBindings)
default:
// no-op
}
return null
})
.filter(nonNullable)
const mergedExports: ISCValues = iscExports.reduce((acc, obj) => ({ ...acc, ...obj }), {})
return mergedExports
}
export type ISCHandlerArg = ArgumentPlaceholder | Expression | SpreadElement | JSXNamespacedName
export interface ISCExport {
local: string
args: ISCHandlerArg[]
}