-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParams.js
More file actions
102 lines (77 loc) · 2.06 KB
/
Params.js
File metadata and controls
102 lines (77 loc) · 2.06 KB
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
import * as core from "@actions/core"
function TypeifyValue(ValueString)
{
// already not-a-string so don't change it
if ( typeof ValueString != typeof '' )
return ValueString;
switch ( ValueString.toLowerCase().trim() )
{
case 'false':
return false;
case 'true':
return true;
}
// todo? spot ints
// no change
return ValueString;
}
function GetParams()
{
const Params = {};
function ParseCommandLineArg(Argument)
{
const Parts = Argument.split('=');
let Key = Parts.shift();
// trim - or --
while ( Key.startsWith('-') )
Key = Key.slice(1);
// ignore empty param
if ( Key.length == 0 )
{
console.warn(`Got empty line param [${Key}]`);
return;
}
// make all params lowercase
Key = Key.toLowerCase();
// keep anything with = in the value
let Value = Parts.join('=');
// default non-value'd arguments to true to indicate presence
if ( Value.length == 0 )
Value = true;
// turn false and true keywords into bools, so that "false" == false
Value = TypeifyValue(Value);
//console.log(`Got command line param [${Key}]=[${Value}]`);
Params[Key] = Value;
}
process.argv.forEach(ParseCommandLineArg);
return Params;
}
const Params = GetParams();
// if you specify a default, this wont throw and instead return the default
export function GetParam(Key,DefaultValueIfMissing=undefined)
{
// gr: lower case only applies to CLI
let CliKey = Key.toLowerCase();
// CLI args get priority
if ( Params.hasOwnProperty(CliKey) )
return Params[CliKey];
// try github inputs
let InputValue = core.getInput(Key);
if ( InputValue )
{
// turn false and true keywords into bools, so that "false" == false
InputValue = TypeifyValue(InputValue);
return InputValue;
}
// now try env var
if ( process.env.hasOwnProperty(Key) )
{
InputValue = process.env[Key];
InputValue = TypeifyValue(InputValue);
return InputValue;
}
if ( DefaultValueIfMissing === undefined )
throw `Missing required parameter, github input or env var "${Key}"`;
return DefaultValueIfMissing;
}
export default Params;