forked from GoogleCloudPlatform/functions-framework-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.ts
168 lines (159 loc) · 4.93 KB
/
options.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import * as minimist from 'minimist';
import * as semver from 'semver';
import {resolve} from 'path';
import {SignatureType, isValidSignatureType} from './types';
/**
* Error thrown when an invalid option is provided.
*/
export class OptionsError extends Error {}
/**
* The set of all options that can be used to configure the behaviour of
* the framework.
*/
export interface FrameworkOptions {
/**
* The port on which this server listens to all HTTP requests.
*/
port: string;
/**
* The name of the function within user's node module to execute. If such a
* function is not defined, then falls back to 'function' name.
*/
target: string;
/**
* The path to the source code file containing the client function.
*/
sourceLocation: string;
/**
* The signature type of the client function.
*/
signatureType: SignatureType;
/**
* Whether or not the --help CLI flag was provided.
*/
printHelp: boolean;
/**
* Whether or not to enable execution id support.
*/
enableExecutionId: boolean;
}
/**
* Helper class for parsing an configurable option from provided CLI flags
* or environment variables.
*/
class ConfigurableOption<T> {
constructor(
/**
* The CLI flag that can be use to configure this option.
*/
public readonly cliOption: string,
/**
* The name of the environment variable used to configure this option.
*/
private envVar: string,
/**
* The default value used when this option is not configured via a CLI flag
* or environment variable.
*/
private defaultValue: T,
/**
* A function used to valid the user provided value.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private validator: (x: any) => T = x => x as T
) {}
parse(cliArgs: minimist.ParsedArgs, envVars: NodeJS.ProcessEnv): T {
return this.validator(
cliArgs[this.cliOption] || envVars[this.envVar] || this.defaultValue
);
}
}
const PortOption = new ConfigurableOption('port', 'PORT', '8080');
const FunctionTargetOption = new ConfigurableOption(
'target',
'FUNCTION_TARGET',
'function'
);
const SourceLocationOption = new ConfigurableOption(
'source',
'FUNCTION_SOURCE',
'',
resolve
);
const SignatureOption = new ConfigurableOption(
'signature-type',
'FUNCTION_SIGNATURE_TYPE',
'http' as SignatureType,
x => {
if (isValidSignatureType(x)) {
return x;
}
throw new OptionsError(
`Function signature type must be one of: ${SignatureType.join(', ')}.`
);
}
);
export const requriedNodeJsVersion = '13.0.0';
const EnableExecutionIdOption = new ConfigurableOption(
'enable-execution-id',
'ENABLE_EXECUTION_ID',
false,
x => {
const shouldEnable =
(typeof x === 'boolean' && x) ||
(typeof x === 'string' && x.toLowerCase() === 'true');
const nodeVersion = process.versions.node;
if (shouldEnable && semver.lt(nodeVersion, requriedNodeJsVersion)) {
throw new OptionsError(
`Execution id is only supported with Node.js versions ${requriedNodeJsVersion} and above. Your current version is ${nodeVersion}. Please upgrade.`
);
}
return shouldEnable;
}
);
export const helpText = `Example usage:
functions-framework --target=helloWorld --port=8080
Documentation:
https://github.com/GoogleCloudPlatform/functions-framework-nodejs`;
/**
* Parse the configurable framework options from the provided CLI arguments and
* environment variables.
* @param cliArgs the raw command line arguments
* @param envVars the environment variables to parse options from
* @returns the parsed options that should be used to configure the framework.
*/
export const parseOptions = (
cliArgs: string[] = process.argv,
envVars: NodeJS.ProcessEnv = process.env
): FrameworkOptions => {
const argv = minimist(cliArgs, {
string: [
PortOption.cliOption,
FunctionTargetOption.cliOption,
SignatureOption.cliOption,
SourceLocationOption.cliOption,
],
boolean: [EnableExecutionIdOption.cliOption],
});
return {
port: PortOption.parse(argv, envVars),
target: FunctionTargetOption.parse(argv, envVars),
sourceLocation: SourceLocationOption.parse(argv, envVars),
signatureType: SignatureOption.parse(argv, envVars),
printHelp: cliArgs[2] === '-h' || cliArgs[2] === '--help',
enableExecutionId: EnableExecutionIdOption.parse(argv, envVars),
};
};