-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathfunctionsMetaData.ts
67 lines (51 loc) · 2.16 KB
/
functionsMetaData.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
import { existsSync, readJSON, writeFile } from 'fs-extra'
import { join } from 'pathe'
import { satisfies } from 'semver'
import { NEXT_PLUGIN, NEXT_PLUGIN_NAME } from '../constants'
import { resolveModuleRoot } from './config'
const getNextRuntimeVersion = async (packageJsonPath: string, useNodeModulesPath: boolean) => {
if (!existsSync(packageJsonPath)) {
return
}
const packagePlugin = await readJSON(packageJsonPath)
return useNodeModulesPath ? packagePlugin.version : packagePlugin.dependencies[NEXT_PLUGIN]
}
const PLUGIN_PACKAGE_PATH = '.netlify/plugins/package.json'
const nextPluginVersion = async (module?: string) => {
const moduleRoot = resolveModuleRoot(module || NEXT_PLUGIN)
const nodeModulesPath = moduleRoot ? join(moduleRoot, 'package.json') : null
return (
(await getNextRuntimeVersion(nodeModulesPath, true)) ||
(await getNextRuntimeVersion(PLUGIN_PACKAGE_PATH, false)) ||
// The runtime version should always be available, but if it's not, return 'unknown'
'unknown'
)
}
export const getPluginVersion = async () => `${NEXT_PLUGIN_NAME}@${await nextPluginVersion()}`
export const useRequireHooks = async () => satisfies(await nextPluginVersion('next'), '13.3.3 - 13.4.9')
// The information needed to create a function configuration file
export interface FunctionInfo {
// The name of the function, e.g. `___netlify-handler`
functionName: string
// The name of the function that will be displayed in logs, e.g. `Next.js SSR handler`
functionTitle: string
// The directory where the function is located, e.g. `.netlify/functions`
functionsDir: string
}
/**
* Creates a function configuration file for the given function.
*
* @param functionInfo The information needed to create a function configuration file
*/
export const writeFunctionConfiguration = async (functionInfo: FunctionInfo) => {
const { functionName, functionTitle, functionsDir } = functionInfo
const generator = await getPluginVersion()
const metadata = {
config: {
name: functionTitle,
generator,
},
version: 1,
}
await writeFile(join(functionsDir, functionName, `${functionName}.json`), JSON.stringify(metadata))
}