-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathanalysis.ts
103 lines (94 loc) · 2.95 KB
/
analysis.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
import fs from 'fs'
import { extractExportedConstValue, UnsupportedValueError } from 'next/dist/build/analysis/extract-const-value'
import { parseModule } from 'next/dist/build/analysis/parse-module'
import { relative } from 'pathe'
export interface ApiStandardConfig {
type?: never
runtime?: 'nodejs' | 'experimental-edge'
schedule?: never
}
export interface ApiScheduledConfig {
type: 'experimental-scheduled'
runtime?: 'nodejs'
schedule: string
}
export interface ApiBackgroundConfig {
type: 'experimental-background'
runtime?: 'nodejs'
schedule?: never
}
export type ApiConfig = ApiStandardConfig | ApiScheduledConfig | ApiBackgroundConfig
export const validateConfigValue = (config: ApiConfig, apiFilePath: string): config is ApiConfig => {
if (config.type === 'experimental-scheduled') {
if (!config.schedule) {
console.error(
`Invalid config value in ${relative(
process.cwd(),
apiFilePath,
)}: schedule is required when type is "experimental-scheduled"`,
)
return false
}
if ((config as ApiConfig).runtime === 'experimental-edge') {
console.error(
`Invalid config value in ${relative(
process.cwd(),
apiFilePath,
)}: edge runtime is not supported for scheduled functions`,
)
return false
}
return true
}
if (!config.type || config.type === 'experimental-background') {
if (config.schedule) {
console.error(
`Invalid config value in ${relative(
process.cwd(),
apiFilePath,
)}: schedule is not allowed unless type is "experimental-scheduled"`,
)
return false
}
if (config.type && (config as ApiConfig).runtime === 'experimental-edge') {
console.error(
`Invalid config value in ${relative(
process.cwd(),
apiFilePath,
)}: edge runtime is not supported for background functions`,
)
return false
}
return true
}
console.error(
`Invalid config value in ${relative(process.cwd(), apiFilePath)}: type ${
(config as ApiConfig).type
} is not supported`,
)
return false
}
/**
* Uses Next's swc static analysis to extract the config values from a file.
*/
export const extractConfigFromFile = async (apiFilePath: string): Promise<ApiConfig> => {
const fileContent = await fs.promises.readFile(apiFilePath, 'utf8')
// No need to parse if there's no "config"
if (!fileContent.includes('config')) {
return {}
}
const ast = await parseModule(apiFilePath, fileContent)
let config: ApiConfig
try {
config = extractExportedConstValue(ast, 'config')
} catch (error) {
if (error instanceof UnsupportedValueError) {
console.warn(`Unsupported config value in ${relative(process.cwd(), apiFilePath)}`)
}
return {}
}
if (validateConfigValue(config, apiFilePath)) {
return config
}
throw new Error(`Unsupported config value in ${relative(process.cwd(), apiFilePath)}`)
}