-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Expand file tree
/
Copy pathnode_options.ts
More file actions
80 lines (74 loc) · 2.34 KB
/
node_options.ts
File metadata and controls
80 lines (74 loc) · 2.34 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
// Copyright 2018-2026 the Deno authors. MIT license.
import { primordials } from "ext:core/mod.js";
const {
SafeMap,
ArrayPrototypeForEach,
ArrayPrototypePush,
StringPrototypeSlice,
StringPrototypeStartsWith,
} = primordials;
// This module ports:
// - https://github.com/nodejs/node/blob/master/src/node_options-inl.h
// - https://github.com/nodejs/node/blob/master/src/node_options.cc
// - https://github.com/nodejs/node/blob/master/src/node_options.h
// Quote-aware tokenizer for NODE_OPTIONS. Node.js uses a shell-like parser
// that respects single and double quotes, so `--title="hello world"` is a
// single token whose value is `hello world`, not two tokens.
function splitNodeOptions(input: string): string[] {
const args: string[] = [];
let current = "";
let inDouble = false;
let inSingle = false;
for (let i = 0; i < input.length; i++) {
const ch = input[i];
if (ch === '"' && !inSingle) {
inDouble = !inDouble;
} else if (ch === "'" && !inDouble) {
inSingle = !inSingle;
} else if (
(ch === " " || ch === "\t" || ch === "\n" || ch === "\r") && !inDouble &&
!inSingle
) {
if (current.length > 0) {
ArrayPrototypePush(args, current);
current = "";
}
} else {
current += ch;
}
}
if (current.length > 0) {
ArrayPrototypePush(args, current);
}
return args;
}
/** Gets the all options for Node.js
* This function is expensive to execute. `getOptionValue` in `internal/options.ts`
* should be used instead to get a specific option. */
export function getOptions() {
const options = new SafeMap([
["--warnings", { value: true }],
["--pending-deprecation", { value: false }],
["--title", { value: "" }],
]);
const nodeOptions = Deno.env.get("NODE_OPTIONS");
const args = nodeOptions ? splitNodeOptions(nodeOptions) : [];
ArrayPrototypeForEach(args, (arg) => {
if (StringPrototypeStartsWith(arg, "--title=")) {
options.set("--title", { value: StringPrototypeSlice(arg, 8) });
return;
}
switch (arg) {
case "--no-warnings":
options.set("--warnings", { value: false });
break;
case "--pending-deprecation":
options.set("--pending-deprecation", { value: true });
break;
// TODO(kt3k): Handle other options.
default:
break;
}
});
return { options };
}