@puku/puku-cli 1.8.21 → 1.8.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bin/puku-cli ADDED
@@ -0,0 +1,101 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { existsSync } from 'fs'
4
+ import { join, dirname } from 'path'
5
+ import { fileURLToPath, pathToFileURL } from 'url'
6
+ import { spawnSync } from 'child_process'
7
+
8
+ const __dirname = dirname(fileURLToPath(import.meta.url))
9
+ const distPath = join(__dirname, '..', 'dist', 'cli.mjs')
10
+
11
+ const HEAP_RELAUNCHED_ENV = 'PUKU_CLI_HEAP_RELAUNCHED'
12
+ const DISABLE_HEAP_RELAUNCH_ENV = 'PUKU_CLI_DISABLE_HEAP_RELAUNCH'
13
+ const HEAP_SIZE_ENV = 'PUKU_CLI_NODE_MAX_OLD_SPACE_SIZE_MB'
14
+ const MAX_MEMORY_ENV = 'PUKU_CLI_MAX_MEMORY_MB'
15
+ const DEFAULT_HEAP_SIZE_MB = 8192
16
+
17
+ function hasNodeFlag(args, flag) {
18
+ return args.some(arg => arg === flag || arg.startsWith(`${flag}=`))
19
+ }
20
+
21
+ function hasNodeOptionFlag(flag) {
22
+ return hasNodeFlag(
23
+ [
24
+ ...process.execArgv,
25
+ ...(process.env.NODE_OPTIONS || '').split(/\s+/).filter(Boolean),
26
+ ],
27
+ flag,
28
+ )
29
+ }
30
+
31
+ function getHeapSizeMb() {
32
+ // --max-memory=N flag overrides env var
33
+ const maxMemArg = process.argv.find(a => a.startsWith('--max-memory='))
34
+ if (maxMemArg) {
35
+ const mb = Number.parseInt(maxMemArg.split('=')[1] || '0', 10)
36
+ if (Number.isSafeInteger(mb) && mb > 0) {
37
+ process.env[HEAP_SIZE_ENV] = String(mb)
38
+ process.env[MAX_MEMORY_ENV] = String(mb)
39
+ return mb
40
+ }
41
+ }
42
+
43
+ const raw = process.env[HEAP_SIZE_ENV]
44
+ if (!raw) return DEFAULT_HEAP_SIZE_MB
45
+ const parsed = Number.parseInt(raw, 10)
46
+ return Number.isSafeInteger(parsed) && parsed > 0 ? parsed : DEFAULT_HEAP_SIZE_MB
47
+ }
48
+
49
+ function relaunchWithLongSessionHeapIfNeeded() {
50
+ if (process.env[DISABLE_HEAP_RELAUNCH_ENV] === '1') return
51
+ if (process.env[HEAP_RELAUNCHED_ENV] === '1') return
52
+ const hasHeapLimit = hasNodeOptionFlag('--max-old-space-size')
53
+ const hasExplicitGc = hasNodeOptionFlag('--expose-gc')
54
+ if (hasHeapLimit && hasExplicitGc) return
55
+
56
+ const execArgv = [...process.execArgv]
57
+ if (!hasHeapLimit) {
58
+ execArgv.push(`--max-old-space-size=${getHeapSizeMb()}`)
59
+ }
60
+
61
+ // --expose-gc enables global.gc() for periodic forced GC in long sessions.
62
+ // NODE_OPTIONS cannot carry --expose-gc, so the launcher must inject it here.
63
+ if (!hasExplicitGc) {
64
+ execArgv.push('--expose-gc')
65
+ }
66
+
67
+ // Strip --max-memory before forwarding — it's a launcher-only arg that
68
+ // Commander in the built CLI would reject as unknown.
69
+ const childArgs = process.argv
70
+ .slice(2)
71
+ .filter(arg => !arg.startsWith('--max-memory=') && arg !== '--max-memory')
72
+
73
+ const result = spawnSync(
74
+ process.execPath,
75
+ [...execArgv, fileURLToPath(import.meta.url), ...childArgs],
76
+ {
77
+ stdio: 'inherit',
78
+ env: {
79
+ ...process.env,
80
+ [HEAP_RELAUNCHED_ENV]: '1',
81
+ },
82
+ },
83
+ )
84
+
85
+ if (result.error) {
86
+ console.error(
87
+ `puku-cli: failed to restart with long-session heap: ${result.error.message}`,
88
+ )
89
+ process.exit(1)
90
+ }
91
+
92
+ process.exit(result.status ?? 1)
93
+ }
94
+
95
+ if (existsSync(distPath)) {
96
+ relaunchWithLongSessionHeapIfNeeded()
97
+ await import(pathToFileURL(distPath).href)
98
+ } else {
99
+ console.error('puku-cli: build not found. Please reinstall the package.')
100
+ process.exit(1)
101
+ }