Skip to content

Commit dc61d8a

Browse files
committed
chore: moved writeFunctionConfiguration to a separate utils folder
1 parent 9e9209b commit dc61d8a

File tree

2 files changed

+44
-34
lines changed

2 files changed

+44
-34
lines changed

packages/runtime/src/helpers/functions.ts

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import type { NetlifyConfig, NetlifyPluginConstants } from '@netlify/build'
22
import bridgeFile from '@vercel/node-bridge'
33
import chalk from 'chalk'
44
import destr from 'destr'
5-
import { resolveModuleRoot } from './config'
65
import { copyFile, ensureDir, existsSync, readJSON, writeFile, writeJSON } from 'fs-extra'
76
import type { ImageConfigComplete, RemotePattern } from 'next/dist/shared/lib/image-config'
87
import { outdent } from 'outdent'
@@ -13,8 +12,6 @@ import {
1312
ODB_FUNCTION_NAME,
1413
IMAGE_FUNCTION_NAME,
1514
DEFAULT_FUNCTIONS_SRC,
16-
NEXT_PLUGIN,
17-
NEXT_PLUGIN_NAME,
1815
HANDLER_FUNCTION_TITLE,
1916
ODB_FUNCTION_TITLE,
2017
IMAGE_FUNCTION_TITLE,
@@ -25,6 +22,7 @@ import { getResolverForPages, getResolverForSourceFiles } from '../templates/get
2522

2623
import { ApiConfig, ApiRouteType, extractConfigFromFile } from './analysis'
2724
import { getSourceFileForPage } from './files'
25+
import { writeFunctionConfiguration } from './utilities/functions'
2826
import { getFunctionNameForPage } from './utils'
2927

3028
export interface ApiRouteConfig {
@@ -33,37 +31,6 @@ export interface ApiRouteConfig {
3331
compiled: string
3432
}
3533

36-
const checkForPackage = async (packageDir: string, nodeModule: boolean) => {
37-
const packagePlugin = existsSync(packageDir) ? await readJSON(packageDir) : null
38-
let nextPlugin
39-
if (!nodeModule && packagePlugin){
40-
nextPlugin = packagePlugin.dependencies[NEXT_PLUGIN] ? packagePlugin.dependencies[NEXT_PLUGIN] : null
41-
}else if (nodeModule && packagePlugin){
42-
nextPlugin = packagePlugin.version ? packagePlugin.version : null
43-
}
44-
45-
return nextPlugin
46-
}
47-
48-
const writeFunctionConfiguration = async (functionName: string, functionTitle: string, functionsDir: string) => {
49-
const pluginPackagePath = '.netlify/plugins/package.json'
50-
const ProjDir = resolveModuleRoot(NEXT_PLUGIN)
51-
const nodeModulesPath = `${ProjDir}/package.json`
52-
53-
const nextPluginVersion =
54-
(await checkForPackage(nodeModulesPath, true)) || (await checkForPackage(pluginPackagePath, false))
55-
56-
const metadata = {
57-
config: {
58-
name: functionTitle,
59-
generator: nextPluginVersion ? `${NEXT_PLUGIN_NAME}@${nextPluginVersion}` : 'Next Runtime Version Not Found',
60-
},
61-
version: 1,
62-
}
63-
64-
await writeFile(join(functionsDir, functionName, `${functionName}.json`), JSON.stringify(metadata))
65-
}
66-
6734
export const generateFunctions = async (
6835
{ FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC, INTERNAL_FUNCTIONS_SRC, PUBLISH_DIR }: NetlifyPluginConstants,
6936
appDir: string,
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { existsSync, readJSON, writeFile } from 'fs-extra'
2+
import { join } from 'pathe'
3+
4+
import { NEXT_PLUGIN, NEXT_PLUGIN_NAME } from '../../constants'
5+
import { resolveModuleRoot } from '../config'
6+
7+
const checkForPackage = async (packageDir: string, nodeModule: boolean) => {
8+
const packagePlugin = existsSync(packageDir) ? await readJSON(packageDir) : null
9+
let nextPlugin
10+
if (!nodeModule && packagePlugin) {
11+
nextPlugin = packagePlugin.dependencies[NEXT_PLUGIN] ? packagePlugin.dependencies[NEXT_PLUGIN] : null
12+
} else if (nodeModule && packagePlugin) {
13+
nextPlugin = packagePlugin.version ? packagePlugin.version : null
14+
}
15+
16+
return nextPlugin
17+
}
18+
19+
/**
20+
* Creates a function configuration file for the given function
21+
*
22+
* @param functionName The name of the function, e.g. `___netlify-handler`
23+
* @param functionTitle The name of the function that will be displayed in logs, e.g. `Next.js SSR handler`
24+
* @param functionsDir The directory where the function is located, e.g. `.netlify/functions`
25+
*/
26+
export const writeFunctionConfiguration = async (functionName: string, functionTitle: string, functionsDir: string) => {
27+
const pluginPackagePath = '.netlify/plugins/package.json'
28+
const ProjDir = resolveModuleRoot(NEXT_PLUGIN)
29+
const nodeModulesPath = `${ProjDir}/package.json`
30+
31+
const nextPluginVersion =
32+
(await checkForPackage(nodeModulesPath, true)) || (await checkForPackage(pluginPackagePath, false))
33+
34+
const metadata = {
35+
config: {
36+
name: functionTitle,
37+
generator: nextPluginVersion ? `${NEXT_PLUGIN_NAME}@${nextPluginVersion}` : 'Next Runtime Version Not Found',
38+
},
39+
version: 1,
40+
}
41+
42+
await writeFile(join(functionsDir, functionName, `${functionName}.json`), JSON.stringify(metadata))
43+
}

0 commit comments

Comments
 (0)