Skip to content
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

feat: Add edge function templates to functions:create command #4731

Merged
merged 93 commits into from
Jul 15, 2022

Conversation

jenae-janzen
Copy link
Contributor

πŸŽ‰ Thanks for submitting a pull request! πŸŽ‰

Summary

Summary
Fixes #https://github.com/netlify/pod-compute/issues/99

Adds edge function templates for JS and TS for the functions:create command.

Opened a new PR because my https://github.com/netlify/cli/pull/4689was getting messy, especially after trying to merge changes from main 😫


For us to review and ship your PR efficiently, please perform the following steps:

  • Open a bug/issue before writing your code πŸ§‘β€πŸ’». This ensures we can discuss the changes and get feedback from everyone that should be involved. If you`re fixing a typo or something that`s on fire πŸ”₯ (e.g. incident related), you can skip this step.
  • Read the contribution guidelines πŸ“–. This ensures your code follows our style guide and
    passes our tests.
  • Update or add tests (if any source code was changed or added) πŸ§ͺ
  • Update or add documentation (if features were changed or added) πŸ“
  • Make sure the status checks below are successful βœ…

A picture of a cute animal (not mandatory, but encouraged)

jenae-janzen and others added 30 commits June 9, 2022 13:22
eduardoboucas
eduardoboucas previously approved these changes Jul 14, 2022
src/commands/functions/functions-create.js Outdated Show resolved Hide resolved
@jenae-janzen
Copy link
Contributor Author

@eduardoboucas thanks for reviewing! I was also wondering about tests -- I updated the existing tests for the serverless function templates so they match the new workflow (the additional prompt at the beginning) and have tested out the edge function templates a lot manually, but I should probably add a few test cases for the edge function templates too, right?

@eduardoboucas
Copy link
Member

@eduardoboucas thanks for reviewing! I was also wondering about tests -- I updated the existing tests for the serverless function templates so they match the new workflow (the additional prompt at the beginning) and have tested out the edge function templates a lot manually, but I should probably add a few test cases for the edge function templates too, right?

Yes, that would be great!

@jenae-janzen
Copy link
Contributor Author

Added one test, but I wanted to include a case where the user specified a custom edge functions directory as well, something like:

test('should use specified edge function directory when found', async (t) => {
  const siteInfo = {
    admin_url: 'https://app.netlify.com/sites/site-name/overview',
    ssl_url: 'https://site-name.netlify.app/',
    id: 'site_id',
    name: 'site-name',
    build_settings: { repo_url: 'https://github.com/owner/repo' },
  }

  const routes = [
    {
      path: 'accounts',
      response: [{ slug: 'test-account' }],
    },
    { path: 'sites/site_id/service-instances', response: [] },
    { path: 'sites/site_id', response: siteInfo },
    {
      path: 'sites',
      response: [siteInfo],
    },
    { path: 'sites/site_id', method: 'patch', response: {} },
  ]

  await withSiteBuilder('site-with-custom-edge-functions-dir', async (builder) => {
    builder.withNetlifyToml({ config: { build: { edge_functions: 'somethingEdgy' } } })

    await builder.buildAsync()

    const createFunctionQuestions = [
      {
        question: "Select the type of function you'd like to create",
        answer: answerWithValue(CONFIRM),
      },
      {
        question: 'Select the language of your function',
        answer: answerWithValue(CONFIRM),
      },
      {
        question: 'Pick a template',
        answer: answerWithValue(CONFIRM),
      },
      {
        question: 'Name your function',
        answer: answerWithValue(CONFIRM),
      },
      {
        question: 'What route do you want your edge function to be invoked on?',
        answer: answerWithValue('/test'),
      },
    ]

    await withMockApi(routes, async ({ apiUrl }) => {
      const childProcess = execa(cliPath, ['functions:create'], {
        env: {
          NETLIFY_API_URL: apiUrl,
          NETLIFY_SITE_ID: 'site_id',
          NETLIFY_AUTH_TOKEN: 'fake-token',
        },
        cwd: builder.directory,
      })

      handleQuestions(childProcess, createFunctionQuestions)

      await childProcess

      t.is(await fs.fileExistsAsync(`${builder.directory}/somethingEdgy/hello/hello.js`, true))
    })
  })
})

However, it keeps failing -- I've been trying to debug it for over an hour now and not sure what else to try or how to get more visibility into why it's failing. Manually testing shows if the edge functions directory is configured in the TOML but doesn't exist, it gets created and the templates are added to it. Not sure if I'm missing something here or if this test is necessary πŸ€”

@eduardoboucas eduardoboucas changed the title Feat: Add edge function templates to functions:create command feat: Add edge function templates to functions:create command Jul 15, 2022
@jenae-janzen jenae-janzen merged commit f1f5cfa into main Jul 15, 2022
@jenae-janzen jenae-janzen deleted the jj/addEdgeFuncTemplate branch July 15, 2022 16:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature code contributing to the implementation of a feature and/or user facing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants