2025-07-02 11:32:06 -07:00
#!/usr/bin/env node
/ * *
* CLI Wrapper Script for @ soulcraft / brainy - cli
*
* This script serves as a wrapper for the Brainy CLI , ensuring that command - line arguments
* are properly passed to the CLI when invoked through the globally installed package .
* /
2025-07-14 11:12:51 -07:00
// CRITICAL: Apply TensorFlow.js environment patch before importing any other modules
// This prevents the "TextEncoder is not a constructor" error in Node.js environments
// by ensuring the global.PlatformNode class is defined before TensorFlow.js loads
function applyTensorFlowPatch ( ) {
try {
// Define a custom Platform class that works in Node.js environments
class Platform {
constructor ( ) {
// Create a util object with necessary methods and constructors
this . util = {
// Use native TextEncoder and TextDecoder constructors
TextEncoder : global . TextEncoder || TextEncoder ,
TextDecoder : global . TextDecoder || TextDecoder
}
// Initialize using native constructors directly
this . textEncoder = new TextEncoder ( )
this . textDecoder = new TextDecoder ( )
}
// Define isTypedArray directly on the instance
isTypedArray ( arr ) {
return ! ! ( ArrayBuffer . isView ( arr ) && ! ( arr instanceof DataView ) )
}
}
// Assign the Platform class to the global object as PlatformNode
global . PlatformNode = Platform
// Also create an instance and assign it to global.platformNode (lowercase p)
global . platformNode = new Platform ( )
console . log ( 'Applied TensorFlow.js platform patch in CLI wrapper' )
} catch ( error ) {
console . warn ( 'Failed to apply TensorFlow.js platform patch:' , error )
}
}
// Apply the patch immediately
applyTensorFlowPatch ( )
2025-07-02 11:32:06 -07:00
import { spawn } from 'child_process'
import { fileURLToPath } from 'url'
import { dirname , join } from 'path'
import fs from 'fs'
2025-07-14 11:12:51 -07:00
// Node.js v24+ compatibility patches are now applied above,
// before any imports, to ensure TensorFlow.js can correctly
// detect and use the TextEncoder/TextDecoder in the environment.
2025-07-02 12:54:50 -07:00
2025-07-02 11:32:06 -07:00
// Get the directory of the current module
const _ _filename = fileURLToPath ( import . meta . url )
const _ _dirname = dirname ( _ _filename )
// Find the main package
const mainPackagePath = join ( _ _dirname , 'node_modules' , '@soulcraft' , 'brainy' )
2025-07-02 11:55:44 -07:00
// Path to the actual CLI script in this package
const cliPath = join ( _ _dirname , 'dist' , 'cli.js' )
2025-07-02 11:32:06 -07:00
// Check if the CLI script exists
if ( ! fs . existsSync ( cliPath ) ) {
console . error ( ` Error: CLI script not found at ${ cliPath } ` )
2025-07-02 16:16:19 -07:00
console . error (
'This is likely because the CLI was not built during package installation.'
)
2025-07-02 11:32:06 -07:00
console . error ( 'Please reinstall the package with:' )
console . error ( 'npm uninstall -g @soulcraft/brainy-cli' )
2025-07-02 11:55:44 -07:00
console . error ( 'npm install -g @soulcraft/brainy-cli' )
2025-07-02 11:32:06 -07:00
process . exit ( 1 )
}
// Special handling for version flags
if ( process . argv . includes ( '--version' ) || process . argv . includes ( '-V' ) ) {
// Read version directly from package.json to ensure it's always correct
try {
const packageJsonPath = join ( _ _dirname , 'package.json' )
const packageJson = JSON . parse ( fs . readFileSync ( packageJsonPath , 'utf8' ) )
console . log ( packageJson . version )
process . exit ( 0 )
} catch ( error ) {
console . error ( 'Error loading version information:' , error . message )
process . exit ( 1 )
}
}
// Forward all arguments to the CLI script
const args = process . argv . slice ( 2 )
// Check if npm is passing --force flag
// When npm runs with --force, it sets the npm_config_force environment variable
2025-07-02 16:16:19 -07:00
if (
process . env . npm _config _force === 'true' &&
args . includes ( 'clear' ) &&
! args . includes ( '--force' ) &&
! args . includes ( '-f' )
) {
2025-07-02 11:32:06 -07:00
args . push ( '--force' )
}
const cli = spawn ( 'node' , [ cliPath , ... args ] , { stdio : 'inherit' } )
cli . on ( 'close' , ( code ) => {
process . exit ( code )
} )