1 |
|
2 |
|
3 | exports.getOptionValue = getOptionValue;
|
4 | function getOptionValue(opt) {
|
5 | parseOptions();
|
6 | return options[opt];
|
7 | }
|
8 |
|
9 | let options;
|
10 | function parseOptions() {
|
11 | if (!options) {
|
12 | options = {
|
13 | '--preserve-symlinks': false,
|
14 | '--preserve-symlinks-main': false,
|
15 | '--input-type': undefined,
|
16 | '--experimental-specifier-resolution': 'explicit',
|
17 | '--experimental-policy': undefined,
|
18 | '--conditions': [],
|
19 | '--pending-deprecation': false,
|
20 | ...parseArgv(getNodeOptionsEnvArgv()),
|
21 | ...parseArgv(process.execArgv),
|
22 | ...getOptionValuesFromOtherEnvVars()
|
23 | }
|
24 | }
|
25 | }
|
26 |
|
27 | function parseArgv(argv) {
|
28 | return require('arg')({
|
29 | '--preserve-symlinks': Boolean,
|
30 | '--preserve-symlinks-main': Boolean,
|
31 | '--input-type': String,
|
32 | '--experimental-specifier-resolution': String,
|
33 |
|
34 | '--es-module-specifier-resolution': '--experimental-specifier-resolution',
|
35 | '--experimental-policy': String,
|
36 | '--conditions': [String],
|
37 | '--pending-deprecation': Boolean,
|
38 | '--experimental-json-modules': Boolean,
|
39 | '--experimental-wasm-modules': Boolean,
|
40 | }, {
|
41 | argv,
|
42 | permissive: true
|
43 | });
|
44 | }
|
45 |
|
46 | function getNodeOptionsEnvArgv() {
|
47 | const errors = [];
|
48 | const envArgv = ParseNodeOptionsEnvVar(process.env.NODE_OPTIONS || '', errors);
|
49 | if (errors.length !== 0) {
|
50 |
|
51 | }
|
52 | return envArgv;
|
53 | }
|
54 |
|
55 |
|
56 | function ParseNodeOptionsEnvVar(node_options, errors) {
|
57 | const env_argv = [];
|
58 |
|
59 | let is_in_string = false;
|
60 | let will_start_new_arg = true;
|
61 | for (let index = 0; index < node_options.length; ++index) {
|
62 | let c = node_options[index];
|
63 |
|
64 |
|
65 | if (c === '\\' && is_in_string) {
|
66 | if (index + 1 === node_options.length) {
|
67 | errors.push("invalid value for NODE_OPTIONS " +
|
68 | "(invalid escape)\n");
|
69 | return env_argv;
|
70 | } else {
|
71 | c = node_options[++index];
|
72 | }
|
73 | } else if (c === ' ' && !is_in_string) {
|
74 | will_start_new_arg = true;
|
75 | continue;
|
76 | } else if (c === '"') {
|
77 | is_in_string = !is_in_string;
|
78 | continue;
|
79 | }
|
80 |
|
81 | if (will_start_new_arg) {
|
82 | env_argv.push(c);
|
83 | will_start_new_arg = false;
|
84 | } else {
|
85 | env_argv[env_argv.length - 1] += c;
|
86 | }
|
87 | }
|
88 |
|
89 | if (is_in_string) {
|
90 | errors.push("invalid value for NODE_OPTIONS " +
|
91 | "(unterminated string)\n");
|
92 | }
|
93 | return env_argv;
|
94 | }
|
95 |
|
96 |
|
97 | function getOptionValuesFromOtherEnvVars() {
|
98 | const options = {};
|
99 | if(process.env.NODE_PENDING_DEPRECATION === '1') {
|
100 | options['--pending-deprecation'] = true;
|
101 | }
|
102 | return options;
|
103 | }
|