Skip to content

Add a source flag/env option to add flexibility to execution path #53

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 3 commits into from
Aug 1, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ Command-line flag | Environment variable | Description
`--port` | `PORT` | The port on which the Functions Framework listens for requests. Default: `8080`
`--target` | `FUNCTION_TARGET` | The name of the exported function to be invoked in response to requests. Default: `function`
`--signature-type` | `FUNCTION_SIGNATURE_TYPE` | The signature used when writing your function. Controls unmarshalling rules and determines which arguments are used to invoke your function. Default: `http`; accepted values: `http` or `event`
`--source` | `SOURCE` | The path of your project directory where you want to start. Functions framework always look only at root path, setting this option will look for your function in any other folder. Default: `/`

Choose a reason for hiding this comment

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

Env variable needs to be prefixed: FUNCTION_SOURCE


You can set command-line flags in your `package.json` via the `start` script.
For example:
Expand Down
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
// unmarshalled from an incoming request.

import * as minimist from 'minimist';
import { resolve } from 'path';

import {
ErrorHandler,
Expand All @@ -41,13 +42,15 @@ const FLAG = {
PORT: 'port',
TARGET: 'target',
SIGNATURE_TYPE: 'signature-type', // dash
SOURCE: 'source',
};

// Supported environment variables
const ENV = {
PORT: 'PORT',
TARGET: 'FUNCTION_TARGET',
SIGNATURE_TYPE: 'FUNCTION_SIGNATURE_TYPE', // underscore
SOURCE: 'SOURCE',

Choose a reason for hiding this comment

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

FUNCTION_SOURCE

};

enum NodeEnv {
Expand All @@ -58,7 +61,10 @@ const argv = minimist(process.argv, {
string: [FLAG.PORT, FLAG.TARGET, FLAG.SIGNATURE_TYPE],
});

const CODE_LOCATION = process.cwd();
const CODE_LOCATION = resolve(
process.cwd(),
argv[FLAG.SOURCE] || process.env[ENV.SOURCE] || '/'
);
const PORT = argv[FLAG.PORT] || process.env[ENV.PORT] || '8080';
const TARGET = argv[FLAG.TARGET] || process.env[ENV.TARGET] || 'function';

Expand Down