Skip to content

Commit

Permalink
functions:invoke: Fix config problems by adding port flag (#878)
Browse files Browse the repository at this point in the history
* functions:invoke: Fix config problems by adding port flag

* Add test for functions:invoke

* Invoke test: Increase timeout

* Remove timeout
  • Loading branch information
RaeesBhatti authored May 6, 2020
1 parent de9b1c9 commit fa3a0f2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
26 changes: 8 additions & 18 deletions src/commands/functions/invoke.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ const chalk = require('chalk')
const Command = require('../../utils/command')
const { flags } = require('@oclif/command')
const inquirer = require('inquirer')
const { serverSettings } = require('../../utils/detect-server')
const fetch = require('node-fetch')
const fs = require('fs')
const path = require('path')

const { NETLIFYDEVWARN } = require('../../utils/logo')
const { getFunctions } = require('../../utils/get-functions')

// https://www.netlify.com/docs/functions/#event-triggered-functions
Expand All @@ -29,27 +29,14 @@ class FunctionsInvokeCommand extends Command {
let { flags, args } = this.parse(FunctionsInvokeCommand)
const { config } = this.netlify

const functionsDir =
flags.functions ||
(config.dev && config.dev.functions) ||
(config.build && config.build.functions) ||
flags.Functions ||
(config.dev && config.dev.Functions) ||
(config.build && config.build.Functions)
const functionsDir = flags.functions || (config.dev && config.dev.functions) || (config.build && config.build.functions)
if (typeof functionsDir === 'undefined') {
this.error('functions directory is undefined, did you forget to set it in netlify.toml?')
process.exit(1)
}

let settings = await serverSettings(Object.assign({}, config.dev, flags))

if (!(settings && settings.command)) {
settings = {
noCmd: true,
port: 8888,
proxyPort: 3999
}
}
if (!flags.port) console.warn(`${NETLIFYDEVWARN} "port" flag was not specified. Attempting to connect to localhost:8888 by default`)
const port = flags.port || 8888

const functions = getFunctions(functionsDir)
const functionToTrigger = await getNameFromArgs(functions, args, flags)
Expand Down Expand Up @@ -120,7 +107,7 @@ class FunctionsInvokeCommand extends Command {

// fetch
fetch(
`http://localhost:${settings.port}/.netlify/functions/${functionToTrigger}` + formatQstring(flags.querystring),
`http://localhost:${port}/.netlify/functions/${functionToTrigger}` + formatQstring(flags.querystring),
{
method: 'post',
headers,
Expand Down Expand Up @@ -252,6 +239,9 @@ FunctionsInvokeCommand.flags = {
identity: flags.boolean({
description: 'simulate Netlify Identity authentication JWT. pass --no-identity to affirm unauthenticated request',
allowNo: true
}),
port: flags.integer({
description: 'Port where netlify dev is accessible. e.g. 8888',
})
}

Expand Down
14 changes: 13 additions & 1 deletion tests/dev.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
const path = require('path')
const { spawn } = require('child_process')
const util = require('util')
const { spawn, exec } = require('child_process')
const test = require('ava')
const fetch = require('node-fetch')
const cliPath = require('./utils/cliPath')
const { randomPort } = require('./utils/')
const sitePath = path.join(__dirname, 'dummy-site')

const execProcess = util.promisify(exec)

let ps, host, port

test.before(async t => {
Expand Down Expand Up @@ -39,6 +42,15 @@ test('netlify dev functions timeout', async t => {
t.is(response, '"ping"')
})

test('netlify functions:invoke', async t => {
const { stdout } = await execProcess([cliPath, 'functions:invoke', 'timeout', '--identity', '--port='+port].join(' '), {
cwd: sitePath,
env: process.env,
})

t.is(stdout, '"ping"\n')
})

test('netlify dev env file', async t => {
const response = await fetch(`http://${host}:${port}/.netlify/functions/env`).then(r => r.text())

Expand Down

0 comments on commit fa3a0f2

Please sign in to comment.