also looking at this
chore(deps): update all non-major dependencies
#2997
29 files
+1047
-444
| @@ -1,18 +1,12 @@ | ||
| 1 | 1 | // Fast path — skip CLI framework for maximum startup speed |
| 2 | 2 | import type { LogLevel } from '../src/logger' |
| 3 | 3 | import type { DtsGenerationConfig, DtsGenerationOption } from '../src/types' |
| 4 | import { resolve } from 'node:path' | |
| 5 | 4 | import process from 'node:process' |
| 6 | import { CLI } from '@stacksjs/clapp' | |
| 7 | import { version } from '../../../package.json' | |
| 8 | import { getConfig } from '../src/config' | |
| 9 | import { generate, processSource, watch } from '../src/generator' | |
| 10 | 5 | |
| 11 | 6 | const _cmd = process.argv[2] |
| 12 | if (_cmd === 'stdin' || _cmd === 'emit') { | |
| 13 | const { processSource } = await import('../src/process-source') | |
| 14 | ||
| 7 | if (_cmd === 'stdin' || _cmd === 'emit' || _cmd === '--project' || _cmd === '--project-worker') { | |
| 15 | 8 | if (_cmd === 'stdin') { |
| 9 | const { processSource } = await import('../src/process-source') | |
| 16 | 10 | const chunks: Buffer[] = [] |
| 17 | 11 | for await (const chunk of process.stdin) { |
| 18 | 12 | chunks.push(chunk) |
| @@ -23,7 +17,105 @@ if (_cmd === 'stdin' || _cmd === 'emit') { | ||
| 23 | 17 | process.stdout.write('\n') |
| 24 | 18 | } |
| 25 | 19 | } |
| 20 | else if (_cmd === '--project-worker') { | |
| 21 | // Worker subprocess: receives file list on stdin, processes & writes output | |
| 22 | const { processSourceDirect } = await import('../src/process-source') | |
| 23 | const { join } = await import('node:path') | |
| 24 | const { mkdirSync } = await import('node:fs') | |
| 25 | const wArgs = process.argv.slice(3) | |
| 26 | let wDir = '' | |
| 27 | let wOut = '' | |
| 28 | for (let i = 0; i < wArgs.length; i++) { | |
| 29 | if (wArgs[i] === '--outdir' && wArgs[i + 1]) { wOut = wArgs[i + 1]; i++ } | |
| 30 | else if (!wDir) { wDir = wArgs[i] } | |
| 31 | } | |
| 32 | mkdirSync(wOut, { recursive: true }) | |
| 33 | ||
| 34 | // Read file list from stdin | |
| 35 | const wChunks: Buffer[] = [] | |
| 36 | for await (const chunk of process.stdin) { wChunks.push(chunk) } | |
| 37 | const wFiles = Buffer.concat(wChunks).toString('utf-8').trim().split('\n').filter(Boolean) | |
| 38 | if (wFiles.length === 0) { process.exit(0) } | |
| 39 | ||
| 40 | // JIT warmup | |
| 41 | processSourceDirect('export interface W{a:string}\nexport function f(x:W):void{}\nexport class C{m():void{}}\nexport type T=string\nexport const v:number=1\nexport enum E{A,B}', 'warmup.ts') | |
| 42 | ||
| 43 | // Pipeline: read → process → write overlapped | |
| 44 | const wReadPromises = wFiles.map(f => Bun.file(join(wDir, f)).text()) | |
| 45 | const wWritePromises: Promise<number>[] = new Array(wFiles.length) | |
| 46 | for (let i = 0; i < wFiles.length; i++) { | |
| 47 | const wSource = await wReadPromises[i] | |
| 48 | wWritePromises[i] = Bun.write( | |
| 49 | join(wOut, wFiles[i].replace(/\.ts$/, '.d.ts')), | |
| 50 | processSourceDirect(wSource, wFiles[i]), | |
| 51 | ) | |
| 52 | } | |
| 53 | await Promise.all(wWritePromises) | |
| 54 | } | |
| 55 | else if (_cmd === '--project') { | |
| 56 | // Usage: dtsx --project <dir> --outdir <outdir> | |
| 57 | const { readdirSync, mkdirSync } = await import('node:fs') | |
| 58 | const { join } = await import('node:path') | |
| 59 | const args = process.argv.slice(3) | |
| 60 | let dir = '' | |
| 61 | let out = '' | |
| 62 | for (let i = 0; i < args.length; i++) { | |
| 63 | if (args[i] === '--outdir' && args[i + 1]) { | |
| 64 | out = args[i + 1] | |
| 65 | i++ | |
| 66 | } | |
| 67 | else if (!dir) { | |
| 68 | dir = args[i] | |
| 69 | } | |
| 70 | } | |
| 71 | mkdirSync(out, { recursive: true }) | |
| 72 | const files = readdirSync(dir).filter((f: string) => f.endsWith('.ts') && !f.endsWith('.d.ts')) | |
| 73 | ||
| 74 | // For large file counts, parallelize across CPU cores using worker subprocesses | |
| 75 | // Use conservative worker count to balance spawn overhead vs parallelism | |
| 76 | const PARALLEL_THRESHOLD = 200 | |
| 77 | if (files.length >= PARALLEL_THRESHOLD) { | |
| 78 | const os = await import('node:os') | |
| 79 | const numCPUs = os.availableParallelism?.() ?? os.cpus().length | |
| 80 | const numWorkers = Math.min(Math.min(numCPUs, 6), Math.ceil(files.length / 50)) | |
| 81 | const batchSize = Math.ceil(files.length / numWorkers) | |
| 82 | const procs: Array<ReturnType<typeof Bun.spawn>> = [] | |
| 83 | for (let w = 0; w < numWorkers; w++) { | |
| 84 | const start = w * batchSize | |
| 85 | const batch = files.slice(start, start + batchSize) | |
| 86 | if (batch.length === 0) break | |
| 87 | const proc = Bun.spawn([process.execPath, '--project-worker', dir, '--outdir', out], { | |
| 88 | stdin: 'pipe', | |
| 89 | stdout: 'ignore', | |
| 90 | stderr: 'inherit', | |
| 91 | }) | |
| 92 | proc.stdin!.write(batch.join('\n')) | |
| 93 | proc.stdin!.end() | |
| 94 | procs.push(proc) | |
| 95 | } | |
| 96 | ||
| 97 | const exitCodes = await Promise.all(procs.map(p => p.exited)) | |
| 98 | if (exitCodes.some(c => c !== 0)) { process.exit(1) } | |
| 99 | } | |
| 100 | else { | |
| 101 | // Sequential batch mode for small file counts | |
| 102 | const { processSourceDirect } = await import('../src/process-source') | |
| 103 | ||
| 104 | // JIT warmup | |
| 105 | processSourceDirect('export interface W{a:string}\nexport function f(x:W):void{}\nexport class C{m():void{}}\nexport type T=string\nexport const v:number=1\nexport enum E{A,B}', 'warmup.ts') | |
| 106 | ||
| 107 | // Read all → Process all → Write all (batch approach avoids per-file await overhead) | |
| 108 | const sources = await Promise.all(files.map(f => Bun.file(join(dir, f)).text())) | |
| 109 | const results: string[] = new Array(files.length) | |
| 110 | for (let i = 0; i < files.length; i++) { | |
| 111 | results[i] = processSourceDirect(sources[i], files[i]) | |
| 112 | } | |
| 113 | await Promise.all(files.map((f, i) => Bun.write(join(out, f.replace(/\.ts$/, '.d.ts')), results[i]))) | |
| 114 | } | |
| 115 | } | |
| 26 | 116 | else { |
| 117 | // emit command | |
| 118 | const { processSource } = await import('../src/process-source') | |
| 27 | 119 | const { readFileSync, writeFileSync, mkdirSync } = await import('node:fs') |
| 28 | 120 | const filePath = process.argv[3]! |
| 29 | 121 | const source = readFileSync(filePath, 'utf-8') |
| @@ -41,6 +133,13 @@ if (_cmd === 'stdin' || _cmd === 'emit') { | ||
| 41 | 133 | process.exit(0) |
| 42 | 134 | } |
| 43 | 135 | |
| 136 | // Heavy imports — only loaded when CLI framework is needed (not on fast path) | |
| 137 | const { resolve } = await import('node:path') | |
| 138 | const { CLI } = await import('@stacksjs/clapp') | |
| 139 | const { version } = await import('../../../package.json') | |
| 140 | const { getConfig } = await import('../src/config') | |
| 141 | const { generate, processSource, watch } = await import('../src/generator') | |
| 142 | ||
| 44 | 143 | const cli = new CLI('dtsx') |
| 45 | 144 | |
| 46 | 145 | const defaultOptions: DtsGenerationConfig = { |