-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathflags.mts
More file actions
313 lines (294 loc) · 9.12 KB
/
flags.mts
File metadata and controls
313 lines (294 loc) · 9.12 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import os from 'node:os'
import { NODE_OPTIONS } from './env/node-options.mts'
import meow from './meow.mts'
import type { MeowFlag as Flag } from './meow.mts'
// Meow doesn't expose this.
export type AnyFlag = StringFlag | BooleanFlag | NumberFlag
export type BooleanFlag = Flag & { type: 'boolean' }
export type NumberFlag = Flag & { type: 'number' }
export type StringFlag = Flag & { type: 'string' }
export type MeowFlag = AnyFlag & {
description: string
hidden?: boolean | undefined
}
// We use this description in getFlagListOutput, meow doesn't care.
export type MeowFlags = Record<string, MeowFlag>
type RawSpaceSizeFlags = {
maxOldSpaceSize: number
maxSemiSpaceSize: number
}
let _rawSpaceSizeFlags: RawSpaceSizeFlags | undefined
/**
* Reset cached flag values for testing purposes.
* @internal
*/
export function resetFlagCache(): void {
_rawSpaceSizeFlags = undefined
_maxOldSpaceSizeFlag = undefined
_maxSemiSpaceSizeFlag = undefined
}
function getRawSpaceSizeFlags(): RawSpaceSizeFlags {
if (_rawSpaceSizeFlags === undefined) {
const cli = meow({
argv: process.argv.slice(2),
// Prevent meow from potentially exiting early.
autoHelp: false,
autoVersion: false,
flags: {
maxOldSpaceSize: {
type: 'number',
default: 0,
},
maxSemiSpaceSize: {
type: 'number',
default: 0,
},
},
importMeta: { url: import.meta.url } as ImportMeta,
})
const maxOldSpaceSize = Number(cli.flags['maxOldSpaceSize'])
const maxSemiSpaceSize = Number(cli.flags['maxSemiSpaceSize'])
// Validate numeric flags (should be guaranteed by meow type='number', but defensive).
if (Number.isNaN(maxOldSpaceSize) || maxOldSpaceSize < 0) {
throw new Error(
`Invalid value for --max-old-space-size: ${cli.flags['maxOldSpaceSize']}`,
)
}
if (Number.isNaN(maxSemiSpaceSize) || maxSemiSpaceSize < 0) {
throw new Error(
`Invalid value for --max-semi-space-size: ${cli.flags['maxSemiSpaceSize']}`,
)
}
_rawSpaceSizeFlags = {
maxOldSpaceSize,
maxSemiSpaceSize,
}
}
return _rawSpaceSizeFlags!
}
let _maxOldSpaceSizeFlag: number | undefined
export function getMaxOldSpaceSizeFlag(): number {
if (_maxOldSpaceSizeFlag === undefined) {
const rawFlag = getRawSpaceSizeFlags().maxOldSpaceSize
// Check if flag was explicitly set (> 0).
if (rawFlag > 0) {
_maxOldSpaceSizeFlag = rawFlag
} else {
const match = /(?<=--max-old-space-size=)\d+/.exec(
NODE_OPTIONS ?? '',
)?.[0]
if (match) {
const parsed = Number(match)
// Regex guarantees numeric string, but validate defensively.
if (Number.isNaN(parsed) || parsed < 0) {
_maxOldSpaceSizeFlag = 0
} else {
_maxOldSpaceSizeFlag = parsed
}
}
}
// Only apply default if no value was set (null/undefined, not 0).
if (_maxOldSpaceSizeFlag == null) {
// Default value determined by available system memory.
_maxOldSpaceSizeFlag = Math.floor(
// Total system memory in MiB.
(os.totalmem() / 1_024 / 1_024) *
// Set 75% of total memory (safe buffer to avoid system pressure).
0.75,
)
}
}
return _maxOldSpaceSizeFlag
}
// Ensure export because dist/flags.js is required in src/constants.mts.
// eslint-disable-next-line n/exports-style
if (typeof exports === 'object' && exports !== null) {
// eslint-disable-next-line n/exports-style
exports.getMaxOldSpaceSizeFlag = getMaxOldSpaceSizeFlag
}
let _maxSemiSpaceSizeFlag: number | undefined
export function getMaxSemiSpaceSizeFlag(): number {
if (_maxSemiSpaceSizeFlag === undefined) {
_maxSemiSpaceSizeFlag = getRawSpaceSizeFlags().maxSemiSpaceSize
if (!_maxSemiSpaceSizeFlag) {
const match = /(?<=--max-semi-space-size=)\d+/.exec(
NODE_OPTIONS ?? '',
)?.[0]
if (match) {
const parsed = Number(match)
// Regex guarantees numeric string, but validate defensively.
if (Number.isNaN(parsed) || parsed < 0) {
_maxSemiSpaceSizeFlag = 0
} else {
_maxSemiSpaceSizeFlag = parsed
}
} else {
_maxSemiSpaceSizeFlag = 0
}
}
if (!_maxSemiSpaceSizeFlag) {
const maxOldSpaceSize = getMaxOldSpaceSizeFlag()
// Dynamically scale semi-space size based on max-old-space-size.
// https://nodejs.org/api/cli.html#--max-semi-space-sizesize-in-mib
if (maxOldSpaceSize <= 8_192) {
// Use tiered values for smaller heaps to avoid excessive young
// generation size. This helps stay within safe memory limits on
// constrained systems or CI.
if (maxOldSpaceSize <= 512) {
_maxSemiSpaceSizeFlag = 4
} else if (maxOldSpaceSize <= 1_024) {
_maxSemiSpaceSizeFlag = 8
} else if (maxOldSpaceSize <= 2_048) {
_maxSemiSpaceSizeFlag = 16
} else if (maxOldSpaceSize <= 4_096) {
_maxSemiSpaceSizeFlag = 32
} else {
_maxSemiSpaceSizeFlag = 64
}
} else {
// For large heaps (> 8 GiB), compute semi-space size using a log-scaled
// function.
//
// The idea:
// - log2(16_384 MiB) = 14 → semi = 14 * 8 = 112
// - log2(32_768 MiB) = 15 → semi = 15 * 8 = 120
// - Scales gradually as heap increases, avoiding overly large jumps
//
// Each 1 MiB of semi-space adds ~3 MiB to the total young generation
// (V8 uses 3 spaces). So this keeps semi-space proportional, without
// over committing.
//
// Also note: V8 won’t benefit much from >256 MiB semi-space unless
// you’re allocating large short-lived objects very frequently
// (e.g. large arrays, buffers).
const log2OldSpace = Math.log2(maxOldSpaceSize)
const scaledSemiSpace = Math.floor(log2OldSpace) * 8
_maxSemiSpaceSizeFlag = scaledSemiSpace
}
}
}
return _maxSemiSpaceSizeFlag
}
// Ensure export because dist/flags.js is required in src/constants.mts.
// eslint-disable-next-line n/exports-style
if (typeof exports === 'object' && exports !== null) {
// eslint-disable-next-line n/exports-style
exports.getMaxSemiSpaceSizeFlag = getMaxSemiSpaceSizeFlag
}
export const commonFlags: MeowFlags = {
animateHeader: {
type: 'boolean',
default: true,
description: 'Disable animated header shimmer effect',
// Hidden to allow custom documenting of the negated `--no-animate-header` variant.
hidden: true,
},
banner: {
type: 'boolean',
default: true,
description: 'Hide the Socket banner',
// Hidden to allow custom documenting of the negated `--no-banner` variant.
hidden: true,
},
compactHeader: {
type: 'boolean',
default: false,
description: 'Use compact single-line header format (auto-enabled in CI)',
// Only show in root command.
hidden: true,
},
headerTheme: {
type: 'string',
default: 'default',
description:
'Header color theme (default, cyberpunk, forest, ocean, sunset)',
hidden: true,
},
config: {
type: 'string',
default: '',
description: 'Override the local config with this JSON',
shortFlag: 'c',
// Only show in root command.
hidden: true,
},
dryRun: {
type: 'boolean',
default: false,
description: 'Run without uploading',
// Only show in root command.
hidden: true,
},
help: {
type: 'boolean',
default: false,
description: 'Show help',
shortFlag: 'h',
// Only show in root command.
hidden: true,
},
helpFull: {
type: 'boolean',
default: false,
description: 'Show full help including environment variables',
// Only show in root command.
hidden: true,
},
maxOldSpaceSize: {
type: 'number',
get default() {
return getMaxOldSpaceSizeFlag()
},
description: 'Set Node.js memory limit',
// Only show in root command in debug mode.
hidden: true,
},
maxSemiSpaceSize: {
type: 'number',
get default() {
return getMaxSemiSpaceSizeFlag()
},
description: 'Set Node.js heap size',
// Only show in root command in debug mode.
hidden: true,
},
spinner: {
type: 'boolean',
default: true,
description: 'Hide the console spinner',
// Hidden to allow custom documenting of the negated `--no-spinner` variant.
hidden: true,
},
noLog: {
type: 'boolean',
default: false,
description:
'Suppress non-essential log output so stdout is clean for automation (e.g. piping --json through jq). Errors still print to stderr.',
},
}
export const outputFlags: MeowFlags = {
json: {
type: 'boolean',
default: false,
description: 'Output as JSON',
shortFlag: 'j',
},
markdown: {
type: 'boolean',
default: false,
description: 'Output as Markdown',
shortFlag: 'm',
},
}
export const validationFlags: MeowFlags = {
all: {
type: 'boolean',
default: false,
description: 'Include all issues',
},
strict: {
type: 'boolean',
default: false,
description: 'Exits with an error code if any matching issues are found',
},
}