-
-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathargs.js
More file actions
120 lines (110 loc) · 4.27 KB
/
args.js
File metadata and controls
120 lines (110 loc) · 4.27 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
'use strict'
const parseArgs = require('./lib/parse-args')
const { requireModule } = require('./util')
const { loadEnvQuitely } = require('./env-loader')
const DEFAULT_IGNORE = 'node_modules build dist .git bower_components logs .swp .nyc_output'
const DEFAULT_ARGUMENTS = {
logLevel: 'fatal',
prettyLogs: false,
watch: false,
verboseWatch: false,
debug: false,
debugPort: 9320,
options: false,
pluginTimeout: 10 * 1000, // everything should load in 10 seconds
closeGraceDelay: 500,
lang: 'js',
standardlint: false,
commonPrefix: false
}
const CLI_OPTIONS = {
port: { type: 'string', short: 'p' },
'inspect-port': { type: 'string' },
'body-limit': { type: 'string' },
'plugin-timeout': { type: 'string', short: 'T' },
'close-grace-delay': { type: 'string', short: 'g' },
'trust-proxy-hop': { type: 'string' },
'log-level': { type: 'string', short: 'l' },
address: { type: 'string', short: 'a' },
socket: { type: 'string', short: 's' },
prefix: { type: 'string', short: 'x' },
'ignore-watch': { type: 'string' },
'logging-module': { type: 'string', short: 'L' },
'debug-host': { type: 'string' },
lang: { type: 'string' },
require: { type: 'string', short: 'r' },
import: { type: 'string', short: 'i' },
config: { type: 'string', short: 'c' },
method: { type: 'string' },
'trust-proxy-ips': { type: 'string' },
'follow-watch': { type: 'string' },
'pretty-logs': { type: 'boolean', short: 'P' },
options: { type: 'boolean', short: 'o' },
watch: { type: 'boolean', short: 'w' },
'verbose-watch': { type: 'boolean', short: 'V' },
debug: { type: 'boolean', short: 'd' },
standardlint: { type: 'boolean' },
'common-prefix': { type: 'boolean' },
'include-hooks': { type: 'boolean' },
'trust-proxy-enabled': { type: 'boolean' },
help: { type: 'boolean', short: 'h' },
'debug-port': { type: 'string', short: 'I' }
}
module.exports = function parseCliArgs (args) {
loadEnvQuitely()
const commandLineArguments = parseArgs(args, {
populateRest: true,
envPrefix: 'FASTIFY_',
tokenize: true,
coerceNumbers: ['port', 'inspect-port', 'body-limit', 'plugin-timeout', 'close-grace-delay', 'trust-proxy-hop', 'debug-port'],
options: CLI_OPTIONS
})
const configFileOptions = commandLineArguments.config ? requireModule(commandLineArguments.config) : undefined
const additionalArgs = commandLineArguments['--'] || []
const pluginParsed = parseArgs(additionalArgs, { options: {}, strict: false })
const { _, ...pluginOptions } = pluginParsed
const ignoreWatchArg = commandLineArguments.ignoreWatch || configFileOptions?.ignoreWatch || ''
const followWatchArg = commandLineArguments.followWatch || configFileOptions?.followWatch || ''
let ignoreWatch = `${DEFAULT_IGNORE} ${ignoreWatchArg}`.trim()
if (ignoreWatchArg.includes('.ts$')) {
ignoreWatch = ignoreWatch.replace('dist', '')
}
// Merge objects from lower to higher priority
const parsedArgs = { ...DEFAULT_ARGUMENTS, ...configFileOptions, ...commandLineArguments }
// Set `trustProxy` with enabled taking precedence, followed by IPs and finally hop count
const trustProxyEnabled = parsedArgs.trustProxyEnabled === undefined
? undefined
: parsedArgs.trustProxyEnabled === true || parsedArgs.trustProxyEnabled === 'true'
const trustProxy = trustProxyEnabled || parsedArgs.trustProxyIps || parsedArgs.trustProxyHop
return {
_: parsedArgs._,
'--': additionalArgs,
help: parsedArgs.help,
port: parsedArgs.port,
bodyLimit: parsedArgs.bodyLimit,
pluginTimeout: parsedArgs.pluginTimeout,
closeGraceDelay: parsedArgs.closeGraceDelay,
pluginOptions,
prettyLogs: parsedArgs.prettyLogs,
options: parsedArgs.options,
watch: parsedArgs.watch,
debug: parsedArgs.debug,
debugPort: parsedArgs.debugPort,
debugHost: parsedArgs.debugHost,
ignoreWatch,
verboseWatch: parsedArgs.verboseWatch,
logLevel: parsedArgs.logLevel,
address: parsedArgs.address,
socket: parsedArgs.socket,
require: parsedArgs.require,
import: parsedArgs.import,
prefix: parsedArgs.prefix,
loggingModule: parsedArgs.loggingModule,
lang: parsedArgs.lang,
method: parsedArgs.method,
commonPrefix: parsedArgs.commonPrefix,
includeHooks: parsedArgs.includeHooks,
followWatch: followWatchArg,
trustProxy
}
}