|
|
import { parseArgs } from 'node:util' |
|
|
import { InvalidArgumentError } from 'next/dist/compiled/commander' |
|
|
|
|
|
export function printAndExit(message: string, code = 1) { |
|
|
if (code === 0) { |
|
|
console.log(message) |
|
|
} else { |
|
|
console.error(message) |
|
|
} |
|
|
|
|
|
return process.exit(code) |
|
|
} |
|
|
|
|
|
const parseNodeArgs = (args: string[]) => { |
|
|
const { values, tokens } = parseArgs({ args, strict: false, tokens: true }) |
|
|
|
|
|
|
|
|
|
|
|
let orphan = null |
|
|
for (let i = 0; i < tokens.length; i++) { |
|
|
const token = tokens[i] |
|
|
|
|
|
if (token.kind === 'option-terminator') { |
|
|
break |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (token.kind === 'option') { |
|
|
orphan = typeof token.value === 'undefined' ? token : null |
|
|
continue |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (token.kind !== 'positional') { |
|
|
orphan = null |
|
|
continue |
|
|
} |
|
|
|
|
|
|
|
|
if (!orphan) { |
|
|
continue |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (orphan.name in values && typeof values[orphan.name] === 'string') { |
|
|
values[orphan.name] += ` ${token.value}` |
|
|
} else { |
|
|
values[orphan.name] = token.value |
|
|
} |
|
|
} |
|
|
|
|
|
return values |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const tokenizeArgs = (input: string): string[] => { |
|
|
let args: string[] = [] |
|
|
let isInString = false |
|
|
let willStartNewArg = true |
|
|
|
|
|
for (let i = 0; i < input.length; i++) { |
|
|
let char = input[i] |
|
|
|
|
|
|
|
|
if (char === '\\' && isInString) { |
|
|
|
|
|
if (input.length === i + 1) { |
|
|
throw new Error('Invalid escape character at the end.') |
|
|
} |
|
|
|
|
|
|
|
|
char = input[++i] |
|
|
} |
|
|
|
|
|
else if (char === ' ' && !isInString) { |
|
|
willStartNewArg = true |
|
|
continue |
|
|
} |
|
|
|
|
|
|
|
|
else if (char === '"') { |
|
|
isInString = !isInString |
|
|
continue |
|
|
} |
|
|
|
|
|
|
|
|
if (willStartNewArg) { |
|
|
args.push(char) |
|
|
willStartNewArg = false |
|
|
} |
|
|
|
|
|
else { |
|
|
args[args.length - 1] += char |
|
|
} |
|
|
} |
|
|
|
|
|
if (isInString) { |
|
|
throw new Error('Unterminated string') |
|
|
} |
|
|
|
|
|
return args |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const getNodeOptionsArgs = () => { |
|
|
if (!process.env.NODE_OPTIONS) return [] |
|
|
|
|
|
return tokenizeArgs(process.env.NODE_OPTIONS) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type DebugAddress = { |
|
|
host: string | undefined |
|
|
port: number |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const formatDebugAddress = ({ host, port }: DebugAddress): string => { |
|
|
if (host) return `${host}:${port}` |
|
|
return `${port}` |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const getParsedDebugAddress = (): DebugAddress => { |
|
|
const args = getNodeOptionsArgs() |
|
|
if (args.length === 0) return { host: undefined, port: 9229 } |
|
|
|
|
|
const parsed = parseNodeArgs(args) |
|
|
|
|
|
|
|
|
|
|
|
const address = |
|
|
parsed.inspect ?? parsed['inspect-brk'] ?? parsed['inspect_brk'] |
|
|
|
|
|
if (!address || typeof address !== 'string') { |
|
|
return { host: undefined, port: 9229 } |
|
|
} |
|
|
|
|
|
|
|
|
if (address.includes(':')) { |
|
|
const [host, port] = address.split(':') |
|
|
return { host, port: parseInt(port, 10) } |
|
|
} |
|
|
|
|
|
return { host: undefined, port: parseInt(address, 10) } |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const getFormattedDebugAddress = () => |
|
|
formatDebugAddress(getParsedDebugAddress()) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function formatNodeOptions( |
|
|
args: Record<string, string | boolean | undefined> |
|
|
): string { |
|
|
return Object.entries(args) |
|
|
.map(([key, value]) => { |
|
|
if (value === true) { |
|
|
return `--${key}` |
|
|
} |
|
|
|
|
|
if (value) { |
|
|
return `--${key}=${ |
|
|
// Values with spaces need to be quoted. We use JSON.stringify to |
|
|
// also escape any nested quotes. |
|
|
value.includes(' ') && !value.startsWith('"') |
|
|
? JSON.stringify(value) |
|
|
: value |
|
|
}` |
|
|
} |
|
|
|
|
|
return null |
|
|
}) |
|
|
.filter((arg) => arg !== null) |
|
|
.join(' ') |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getParsedNodeOptionsWithoutInspect() { |
|
|
const args = getNodeOptionsArgs() |
|
|
if (args.length === 0) return {} |
|
|
|
|
|
const parsed = parseNodeArgs(args) |
|
|
|
|
|
|
|
|
delete parsed.inspect |
|
|
delete parsed['inspect-brk'] |
|
|
delete parsed['inspect_brk'] |
|
|
|
|
|
return parsed |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getFormattedNodeOptionsWithoutInspect() { |
|
|
const args = getParsedNodeOptionsWithoutInspect() |
|
|
if (Object.keys(args).length === 0) return '' |
|
|
|
|
|
return formatNodeOptions(args) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function parseValidPositiveInteger(value: string): number { |
|
|
const parsedValue = parseInt(value, 10) |
|
|
|
|
|
if (isNaN(parsedValue) || !isFinite(parsedValue) || parsedValue < 0) { |
|
|
throw new InvalidArgumentError(`'${value}' is not a non-negative number.`) |
|
|
} |
|
|
return parsedValue |
|
|
} |
|
|
|
|
|
export const RESTART_EXIT_CODE = 77 |
|
|
|
|
|
export type NodeInspectType = 'inspect' | 'inspect-brk' | undefined |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getNodeDebugType(): NodeInspectType { |
|
|
const args = [...process.execArgv, ...getNodeOptionsArgs()] |
|
|
if (args.length === 0) return |
|
|
|
|
|
const parsed = parseNodeArgs(args) |
|
|
|
|
|
if (parsed.inspect) return 'inspect' |
|
|
if (parsed['inspect-brk'] || parsed['inspect_brk']) return 'inspect-brk' |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getMaxOldSpaceSize() { |
|
|
const args = getNodeOptionsArgs() |
|
|
if (args.length === 0) return |
|
|
|
|
|
const parsed = parseNodeArgs(args) |
|
|
|
|
|
const size = parsed['max-old-space-size'] || parsed['max_old_space_size'] |
|
|
if (!size || typeof size !== 'string') return |
|
|
|
|
|
return parseInt(size, 10) |
|
|
} |
|
|
|