-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add generator meta data for framework generated Netlify Functio…
…ns (#1999) * fix: moving functionmetadata over * chore: updated naming for file and when version not found * chore: prettier * Revert "chore: prettier" This reverts commit 75d46b4. * Revert updated naming for file and when version not found" reverts commit 6acb832. * chore: updating not found mssg + image func title * chore: refactor based on feedback * chore: refactored parameters for writeFunctionConfiguration * chore: small refactor and renaming * Update packages/runtime/src/helpers/functionsMetaData.ts * test: updated test when runtime version is unknown * chore: added a comment about returning an unknown version of the next runtime * chore: refactor based on PR feedback * chore: fixed an error causing tests to fail --------- Co-authored-by: Nick Taylor <nick@iamdeveloper.com> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
- Loading branch information
1 parent
fb93b54
commit e5ddcd2
Showing
7 changed files
with
203 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { existsSync, readJSON, writeFile } from 'fs-extra' | ||
import { join } from 'pathe' | ||
|
||
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] | ||
} | ||
|
||
// 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 pluginPackagePath = '.netlify/plugins/package.json' | ||
const moduleRoot = resolveModuleRoot(NEXT_PLUGIN) | ||
const nodeModulesPath = moduleRoot ? join(moduleRoot, 'package.json') : null | ||
|
||
const nextPluginVersion = | ||
(await getNextRuntimeVersion(nodeModulesPath, true)) || | ||
(await getNextRuntimeVersion(pluginPackagePath, false)) || | ||
// The runtime version should always be available, but if it's not, return 'unknown' | ||
'unknown' | ||
|
||
const metadata = { | ||
config: { | ||
name: functionTitle, | ||
generator: `${NEXT_PLUGIN_NAME}@${nextPluginVersion}`, | ||
}, | ||
version: 1, | ||
} | ||
|
||
await writeFile(join(functionsDir, functionName, `${functionName}.json`), JSON.stringify(metadata)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { readJSON } from 'fs-extra' | ||
import mock from 'mock-fs' | ||
import { join } from 'pathe' | ||
import { NEXT_PLUGIN_NAME } from '../packages/runtime/src/constants' | ||
import { writeFunctionConfiguration } from '../packages/runtime/src/helpers/functionsMetaData' | ||
|
||
describe('writeFunctionConfiguration', () => { | ||
afterEach(() => { | ||
mock.restore() | ||
}) | ||
|
||
it('should write the configuration for a function using node modules version of @netlify/plugin-nextjs', async () => { | ||
const nextRuntimeVersion = '23.4.5' | ||
|
||
mock({ | ||
'.netlify/plugins/package.json': JSON.stringify({ | ||
name: 'test', | ||
version: '1.0.0', | ||
dependencies: { | ||
'@netlify/plugin-nextjs': '29.3.4', | ||
}, | ||
}), | ||
'node_modules/@netlify/plugin-nextjs/package.json': JSON.stringify({ | ||
name: '@netlify/plugin-nextjs', | ||
version: nextRuntimeVersion, | ||
}), | ||
'.netlify/functions/some-folder/someFunctionName': {}, | ||
}) | ||
|
||
const functionName = 'someFunctionName' | ||
const functionTitle = 'some function title' | ||
const functionsDir = '.netlify/functions/some-folder' | ||
|
||
const expected = { | ||
config: { | ||
name: functionTitle, | ||
generator: `${NEXT_PLUGIN_NAME}@${nextRuntimeVersion}`, | ||
}, | ||
version: 1, | ||
} | ||
|
||
const filePathToSaveTo = join(functionsDir, functionName, `${functionName}.json`) | ||
await writeFunctionConfiguration({ functionName, functionTitle, functionsDir }) | ||
const actual = await readJSON(filePathToSaveTo) | ||
|
||
expect(actual).toEqual(expected) | ||
}) | ||
|
||
it('should write the configuration for a function using version of @netlify/plugin-nextjs in package.json', async () => { | ||
const nextRuntimeVersion = '23.4.5' | ||
|
||
mock({ | ||
'.netlify/plugins/package.json': JSON.stringify({ | ||
name: 'test', | ||
version: '1.0.0', | ||
dependencies: { | ||
'@netlify/plugin-nextjs': nextRuntimeVersion, | ||
}, | ||
}), | ||
'.netlify/functions/some-folder/someFunctionName': {}, | ||
}) | ||
|
||
const functionName = 'someFunctionName' | ||
const functionTitle = 'some function title' | ||
const functionsDir = '.netlify/functions/some-folder' | ||
|
||
const expected = { | ||
config: { | ||
name: functionTitle, | ||
generator: `${NEXT_PLUGIN_NAME}@${nextRuntimeVersion}`, | ||
}, | ||
version: 1, | ||
} | ||
|
||
const filePathToSaveTo = join(functionsDir, functionName, `${functionName}.json`) | ||
await writeFunctionConfiguration({ functionName, functionTitle, functionsDir }) | ||
const actual = await readJSON(filePathToSaveTo) | ||
|
||
expect(actual).toEqual(expected) | ||
}) | ||
|
||
it('should write the configuration for a function with runtime version not found', async () => { | ||
mock({ | ||
'.netlify/functions/some-folder/someFunctionName': {}, | ||
}) | ||
|
||
const functionName = 'someFunctionName' | ||
const functionTitle = 'some function title' | ||
const functionsDir = '.netlify/functions/some-folder' | ||
|
||
const expected = { | ||
config: { | ||
name: functionTitle, | ||
generator: '@netlify/next-runtime@unknown', | ||
}, | ||
version: 1, | ||
} | ||
|
||
const filePathToSaveTo = join(functionsDir, functionName, `${functionName}.json`) | ||
await writeFunctionConfiguration({ functionName, functionTitle, functionsDir }) | ||
const actual = await readJSON(filePathToSaveTo) | ||
|
||
expect(actual).toEqual(expected) | ||
}) | ||
}) |