Skip to content

Commit dfa30df

Browse files
gardun0grant
authored andcommitted
Add a source flag/env option to add flexibility to execution path (#53)
* new: Adds source flag for flexibility to execute different paths * Add the option flag/env to README.md * fix: env vars must start with FUNCTION_ prefix
1 parent afbfda5 commit dfa30df

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ Command-line flag | Environment variable | Description
147147
`--port` | `PORT` | The port on which the Functions Framework listens for requests. Default: `8080`
148148
`--target` | `FUNCTION_TARGET` | The name of the exported function to be invoked in response to requests. Default: `function`
149149
`--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`
150+
`--source` | `FUNCTION_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: `/`
150151

151152
You can set command-line flags in your `package.json` via the `start` script.
152153
For example:

src/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
// unmarshalled from an incoming request.
2929

3030
import * as minimist from 'minimist';
31+
import { resolve } from 'path';
3132

3233
import {
3334
ErrorHandler,
@@ -41,13 +42,15 @@ const FLAG = {
4142
PORT: 'port',
4243
TARGET: 'target',
4344
SIGNATURE_TYPE: 'signature-type', // dash
45+
SOURCE: 'source',
4446
};
4547

4648
// Supported environment variables
4749
const ENV = {
4850
PORT: 'PORT',
4951
TARGET: 'FUNCTION_TARGET',
5052
SIGNATURE_TYPE: 'FUNCTION_SIGNATURE_TYPE', // underscore
53+
SOURCE: 'FUNCTION_SOURCE',
5154
};
5255

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

61-
const CODE_LOCATION = process.cwd();
64+
const CODE_LOCATION = resolve(
65+
process.cwd(),
66+
argv[FLAG.SOURCE] || process.env[ENV.SOURCE] || '/'
67+
);
6268
const PORT = argv[FLAG.PORT] || process.env[ENV.PORT] || '8080';
6369
const TARGET = argv[FLAG.TARGET] || process.env[ENV.TARGET] || 'function';
6470

0 commit comments

Comments
 (0)