-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathenv.ts
More file actions
196 lines (188 loc) · 5.65 KB
/
env.ts
File metadata and controls
196 lines (188 loc) · 5.65 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// deno-lint-ignore-file no-explicit-any
import { existsSync, path } from "./deps.ts";
import { abort, debug } from "./utils.ts";
export type EnvValue = boolean | string | Array<string>;
type EnvValues = { [name: string]: EnvValue };
type EnvFunction = (name?: string, value?: EnvValue) => any;
/** Factory class for creating environment functions. */
export class Env {
private values: EnvValues;
private constructor() {
this.values = {
...{
"--tasks": [],
"--debug": !!Deno.env.get("DRAKE_DEBUG"),
"--default-task": "",
"--cache": "",
"--directory": Deno.cwd(),
"--abort-exits": false,
"--always-make": false,
"--dry-run": false,
"--github-actions": false,
"--help": false,
"--list-all": false,
"--list-tasks": false,
"--quiet": false,
"--version": false,
},
} as EnvValues;
}
/** Set command-line options and variables. */
private setValue(name: string, value: EnvValue) {
switch (name) {
case "--abort-exits":
case "--always-make":
case "--debug":
case "--dry-run":
case "--github-actions":
case "--help":
case "--list-all":
case "--list-tasks":
case "--quiet":
case "--version":
if (typeof value !== "boolean") {
abort(`${name} must be a boolean`);
}
break;
case "--cache":
if (typeof value !== "string") {
abort(`${name} must be a string`);
}
if (value !== "") {
const dir = path.dirname(value);
if (!existsSync(dir) || !Deno.statSync(dir).isDirectory) {
abort(`--cache file directory missing or not a directory: ${dir}`);
}
value = path.join(Deno.realPathSync(dir), path.basename(value));
}
break;
case "--directory":
if (typeof value !== "string") {
abort(`${name} must be a string`);
}
if (!existsSync(value) || !Deno.statSync(value).isDirectory) {
abort(`--directory missing or not a directory: ${value}`);
}
value = Deno.realPathSync(value);
Deno.chdir(value);
break;
case "--default-task":
if (typeof value !== "string") {
abort(`${name} must be a string`);
}
break;
case "--tasks":
if (
!(value instanceof Array) ||
!value.every((v) => typeof v === "string")
) {
abort("--tasks must be a string array");
}
break;
default:
if (name.startsWith("-")) {
abort(`illegal option: ${name}`);
}
if (!/^[a-zA-Z]\w*$/.test(name)) {
abort(`illegal variable name: ${name}`);
}
if (typeof value !== "string") {
abort(`variable value must be a string: ${name}`);
}
}
this.values[name] = value;
}
/** Return a new environment getter/setter function with `this` set to a new Env object. */
static create(): EnvFunction {
const env = new Env();
return function (
this: Env,
name?: string,
value?: EnvValue,
): any {
if (name === undefined) {
return this; // Return function's Env object if called without parameters.
}
if (arguments.length !== 1) {
debug("set", `${name}: ${value}`);
this.setValue(name, value!);
}
return this.values[name];
}.bind(env);
}
/** Parse command-line arguments. */
parseArgs(args: string[]): void {
let arg: string | undefined;
// deno-lint-ignore no-extra-boolean-cast
while (!!(arg = args.shift())) {
const match = arg.match(/^([a-zA-Z]\w*)=(.*)$/);
if (match) {
this.values[match[1]] = match[2]; // Set named variable.
continue;
}
const shortOpts: { [arg: string]: string } = {
"-a": "--always-make",
"-D": "--debug",
"-d": "--directory",
"-n": "--dry-run",
"-g": "--github-actions",
"-h": "--help",
"-l": "--list-tasks",
"-L": "--list-all",
"-q": "--quiet",
} as const;
if (shortOpts[arg] !== undefined) {
arg = shortOpts[arg];
}
switch (arg) {
case "--always-make":
case "--debug":
case "--github-actions":
case "--dry-run":
case "--help":
case "--list-tasks":
case "--list-all":
case "--quiet":
case "--version":
this.setValue(arg, true);
break;
case "--cache":
// deno-lint-ignore no-case-declarations
case "--directory":
const value = args.shift();
if (value === undefined) {
abort(`missing ${arg} option value`);
}
this.setValue(arg, value);
break;
default:
if (arg.startsWith("-")) {
abort(`illegal option: ${arg}`);
}
(this.values["--tasks"] as Array<string>).push(arg);
break;
}
}
}
}
/**
* The Drake `env` API function gets and optionally sets the command-line
* options, task names and variables.
*
* Options are keyed by their long option name e.g. `env("--dry-run")`.
* Command-line flag options return a boolean; the `--cache` and `--directory` options
* return a string.
*
* Command-line variables are keyed by name. For example `vers=1.0.1` on the
* command-line sets the `vers` value to `"1.0.1"`.
*
* Command-line tasks are stored in the `--tasks` string array.
*
* Examples:
*
* env("--abort-exits", true);
* env("--default-task", "test");
* console.log(`version: ${env("vers")}`);
* if (!env("--quiet")) console.log(message);
*/
export const env = Env.create();