-
Notifications
You must be signed in to change notification settings - Fork 89
feat: add generator meta data for framework generated Netlify Functions #1999
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
619f289
fix: moving functionmetadata over
taty2010 6acb832
chore: updated naming for file and when version not found
taty2010 75d46b4
chore: prettier
taty2010 ec99ea8
Revert "chore: prettier"
taty2010 5af790b
Revert updated naming for file and when version not found"
taty2010 307955a
chore: updating not found mssg + image func title
taty2010 05706a9
chore: refactor based on feedback
nickytonline 43f412c
chore: refactored parameters for writeFunctionConfiguration
nickytonline 74b67d1
chore: small refactor and renaming
nickytonline afaa0d2
Merge remote-tracking branch 'origin/main' into tn/generator-test
nickytonline 7b480e5
Update packages/runtime/src/helpers/functionsMetaData.ts
nickytonline afe46a2
test: updated test when runtime version is unknown
nickytonline 0447d44
chore: added a comment about returning an unknown version of the next…
nickytonline 8f0f56e
Merge branch 'main' into tn/generator-test
nickytonline de7ba0f
chore: refactor based on PR feedback
nickytonline 2b11cf0
Merge branch 'main' into tn/generator-test
kodiakhq[bot] bc7b5a6
chore: fixed an error causing tests to fail
nickytonline File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,44 @@ | ||
import { existsSync, readJSON, writeFile } from 'fs-extra' | ||
import { join } from 'pathe' | ||
|
||
import { NEXT_PLUGIN, NEXT_PLUGIN_NAME } from '../constants' | ||
|
||
import { resolveModuleRoot } from './config' | ||
|
||
const checkForPackage = async (packageDir: string, nodeModule: boolean) => { | ||
const packagePlugin = existsSync(packageDir) ? await readJSON(packageDir) : null | ||
let nextPlugin | ||
if (!nodeModule && packagePlugin) { | ||
nextPlugin = packagePlugin.dependencies[NEXT_PLUGIN] ? packagePlugin.dependencies[NEXT_PLUGIN] : null | ||
} else if (nodeModule && packagePlugin) { | ||
nextPlugin = packagePlugin.version ? packagePlugin.version : null | ||
} | ||
|
||
return nextPlugin | ||
} | ||
|
||
/** | ||
* Creates a function configuration file for the given function | ||
* | ||
* @param functionName The name of the function, e.g. `___netlify-handler` | ||
* @param functionTitle The name of the function that will be displayed in logs, e.g. `Next.js SSR handler` | ||
* @param functionsDir The directory where the function is located, e.g. `.netlify/functions` | ||
*/ | ||
export const writeFunctionConfiguration = async (functionName: string, functionTitle: string, functionsDir: string) => { | ||
const pluginPackagePath = '.netlify/plugins/package.json' | ||
const ProjDir = resolveModuleRoot(NEXT_PLUGIN) | ||
const nodeModulesPath = `${ProjDir}/package.json` | ||
|
||
const nextPluginVersion = | ||
(await checkForPackage(nodeModulesPath, true)) || (await checkForPackage(pluginPackagePath, false)) | ||
|
||
const metadata = { | ||
config: { | ||
name: functionTitle, | ||
generator: `${NEXT_PLUGIN_NAME}@${nextPluginVersion || 'version-not-found'}`, | ||
nickytonline marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
version: 1, | ||
} | ||
|
||
await writeFile(join(functionsDir, functionName, `${functionName}.json`), JSON.stringify(metadata)) | ||
} |
This file contains hidden or 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@version-not-found', | ||
}, | ||
version: 1, | ||
} | ||
|
||
const filePathToSaveTo = join(functionsDir, functionName, `${functionName}.json`) | ||
await writeFunctionConfiguration(functionName, functionTitle, functionsDir) | ||
const actual = await readJSON(filePathToSaveTo) | ||
|
||
expect(actual).toEqual(expected) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.