Skip to content

Commit

Permalink
fix: import function template metadata as module (#6516)
Browse files Browse the repository at this point in the history
* fix: rename .netlify-function-template.js -> .mjs

* test: create typescript serverless function

* test: test typescript outside of nx fixture

---------

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
cubeghost and kodiakhq[bot] committed Apr 17, 2024
1 parent f0af180 commit ffe8696
Show file tree
Hide file tree
Showing 27 changed files with 38 additions and 34 deletions.
8 changes: 4 additions & 4 deletions src/commands/functions/functions-create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ const formatRegistryArrayForInquirer = async function (lang, funcType) {
.filter((folder) => Boolean(folder?.isDirectory()))
.map(async ({ name }) => {
try {
const templatePath = path.join(templatesDir, lang, name, '.netlify-function-template.js')
const templatePath = path.join(templatesDir, lang, name, '.netlify-function-template.mjs')
// @ts-expect-error TS(7036) FIXME: Dynamic import's specifier must be of type 'string... Remove this comment to see the full error message
const template = await import(pathToFileURL(templatePath))
return template.default
Expand Down Expand Up @@ -393,7 +393,7 @@ const downloadFromURL = async function (command, options, argumentName, function
})

// read, execute, and delete function template file if exists
const fnTemplateFile = path.join(fnFolder, '.netlify-function-template.js')
const fnTemplateFile = path.join(fnFolder, '.netlify-function-template.mjs')
if (await fileExistsAsync(fnTemplateFile)) {
const {
default: { addons = [], onComplete },
Expand Down Expand Up @@ -521,7 +521,7 @@ const scaffoldFromTemplate = async function (command, options, argumentName, fun

// These files will not be part of the log message because they'll likely
// be removed before the command finishes.
const omittedFromOutput = new Set(['.netlify-function-template.js', 'package.json', 'package-lock.json'])
const omittedFromOutput = new Set(['.netlify-function-template.mjs', 'package.json', 'package-lock.json'])
const createdFiles = await copyTemplateDir(pathToTemplate, functionPath, vars)
// @ts-expect-error TS(7006) FIXME: Parameter 'filePath' implicitly has an 'any' type.
createdFiles.forEach((filePath) => {
Expand All @@ -538,7 +538,7 @@ const scaffoldFromTemplate = async function (command, options, argumentName, fun
})

// delete function template file that was copied over by copydir
await unlink(path.join(functionPath, '.netlify-function-template.js'))
await unlink(path.join(functionPath, '.netlify-function-template.mjs'))

// npm install
if (functionPackageJson !== undefined) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,42 +181,46 @@ describe.concurrent('functions:create command', () => {
})

test('should only show function templates for the language specified via the --language flag, if one is present', async () => {
await withSiteBuilder('site-with-no-functions-dir', async (builder) => {
await builder.buildAsync()
const createWithLanguageTemplate = async (language, outputPath) =>
await withSiteBuilder('site-with-no-functions-dir', async (builder) => {
await builder.buildAsync()

const createFunctionQuestions = [
{
question: "Select the type of function you'd like to create",
answer: answerWithValue(DOWN),
},
{
question: 'Enter the path, relative to your site',
answer: answerWithValue('test/functions'),
},
{
question: 'Pick a template',
answer: CONFIRM,
},
{
question: 'Name your function',
answer: CONFIRM,
},
]
const createFunctionQuestions = [
{
question: "Select the type of function you'd like to create",
answer: answerWithValue(DOWN),
},
{
question: 'Enter the path, relative to your site',
answer: answerWithValue('test/functions'),
},
{
question: 'Pick a template',
answer: CONFIRM,
},
{
question: 'Name your function',
answer: CONFIRM,
},
]

await withMockApi(routes, async ({ apiUrl }) => {
const childProcess = execa(
cliPath,
['functions:create', '--language', 'javascript'],
getCLIOptions({ apiUrl, builder }),
)
await withMockApi(routes, async ({ apiUrl }) => {
const childProcess = execa(
cliPath,
['functions:create', '--language', language],
getCLIOptions({ apiUrl, builder }),
)

handleQuestions(childProcess, createFunctionQuestions)
handleQuestions(childProcess, createFunctionQuestions)

await childProcess
await childProcess

expect(await fileExistsAsync(`${builder.directory}/test/functions/hello-world/hello-world.js`)).toBe(true)
expect(await fileExistsAsync(`${builder.directory}/test/functions/${outputPath}`)).toBe(true)
})
})
})

await createWithLanguageTemplate('javascript', 'hello-world/hello-world.js')
await createWithLanguageTemplate('typescript', 'hello-world/hello-world.ts')
})

test('throws an error when the --language flag contains an unsupported value', async () => {
Expand Down

3 comments on commit ffe8696

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📊 Benchmark results

  • Dependency count: 1,315
  • Package size: 295 MB
  • Number of ts-expect-error directives: 1,007

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📊 Benchmark results

  • Dependency count: 1,315
  • Package size: 295 MB
  • Number of ts-expect-error directives: 1,007

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📊 Benchmark results

  • Dependency count: 1,315
  • Package size: 295 MB
  • Number of ts-expect-error directives: 1,007

Please sign in to comment.