Skip to content
This repository was archived by the owner on May 25, 2025. It is now read-only.

Commit 8fcfa2b

Browse files
feat: runScript to log environment (preparing for tsn deprecation)
1 parent c508bc4 commit 8fcfa2b

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

src/script/runScript.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os from 'node:os'
2+
import { styleText } from 'node:util'
13
import type { CommonLogger } from '@naturalcycles/js-lib'
24
import { pDelay, setGlobalStringifyFunction } from '@naturalcycles/js-lib'
35
import { inspectStringifyFn } from '../string/inspect'
@@ -43,6 +45,7 @@ const { DEBUG_RUN_SCRIPT } = process.env
4345
* Set env DEBUG_RUN_SCRIPT for extra debugging.
4446
*/
4547
export function runScript(fn: (...args: any[]) => any, opt: RunScriptOptions = {}): void {
48+
logEnvironment()
4649
setGlobalStringifyFunction(inspectStringifyFn)
4750

4851
const { logger = console, noExit, registerUncaughtExceptionHandlers = true } = opt
@@ -86,3 +89,43 @@ export function runScript(fn: (...args: any[]) => any, opt: RunScriptOptions = {
8689
}
8790
})()
8891
}
92+
93+
function logEnvironment(): void {
94+
const {
95+
platform,
96+
arch,
97+
versions: { node },
98+
env: { CPU_LIMIT, NODE_OPTIONS = 'not defined' },
99+
} = process
100+
101+
const cpuLimit = Number(CPU_LIMIT) || undefined
102+
const availableParallelism = os.availableParallelism?.()
103+
const cpus = os.cpus().length
104+
console.log(
105+
dimGrey(
106+
Object.entries({
107+
node: `${node} ${platform} ${arch}`,
108+
NODE_OPTIONS,
109+
cpus,
110+
availableParallelism,
111+
cpuLimit,
112+
})
113+
.map(([k, v]) => `${k}: ${v}`)
114+
.join(', '),
115+
),
116+
)
117+
118+
if (!NODE_OPTIONS) {
119+
console.warn(
120+
`NODE_OPTIONS env variable is not defined. You may run into out-of-memory issues when running memory-intensive scripts. It's recommended to set it to:\n--max-old-space-size=12000`,
121+
)
122+
} else if (NODE_OPTIONS.includes('max_old')) {
123+
console.warn(
124+
`It looks like you're using "max_old_space_size" syntax with underscores instead of dashes - it's WRONG and doesn't work in environment variables. Strongly advised to rename it to "max-old-space-size"`,
125+
)
126+
}
127+
}
128+
129+
function dimGrey(s: string): string {
130+
return styleText(['dim', 'grey'], s)
131+
}

0 commit comments

Comments
 (0)