consolex 0.1.0

Windows console utilities: probe, create, and release the console of the current process, plus a small CLI.
//! Launches `consolex` as a third-party program would, using
//! `child_process`, and reports its exit code and captured output.
//!
//! Usage:
//!     node tests/spawn.mjs [args...]
//!
//! Examples:
//!     node tests/spawn.mjs --version
//!     node tests/spawn.mjs --show --version
//!     node tests/spawn.mjs --hide --version
//!
//! `--show` should open a NEW console window; `--hide` should produce no
//! output. Note that child_process is not a console itself, so the binary
//! will behave as if double-clicked.

import { spawn } from 'node:child_process';
import { fileURLToPath } from 'node:url';
import path from 'node:path';

const root = (path.dirname(fileURLToPath(import.meta.url)));
const exe = path.join(root, 'target', 'debug', 'consolex.exe');
const args = process.argv.slice(2);

console.log(`spawning: ${exe} ${args.join(' ')}`);

const child = spawn(exe, args, { windowsHide: false });

let stdout = '';
let stderr = '';

child.stdout.on('data', (d) => (stdout += d));
child.stderr.on('data', (d) => (stderr += d));

child.on('close', (code, signal) => {
  console.log('exit code:', code, signal ? `signal: ${signal}` : '');
  if (stdout) console.log('stdout:', JSON.stringify(stdout));
  if (stderr) console.log('stderr:', JSON.stringify(stderr));
  if (!stdout && !stderr) console.log('(no captured output)');
});