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

functions:invoke: Fix config problems by adding port flag #878

Merged
merged 5 commits into from
May 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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