@bemoje/node
v2.0.0
Published
Node.js utilities for process execution, logging, timing, streams, and system monitoring.
Maintainers
Readme
@bemoje/node
Node.js utilities for process execution, logging, timing, streams, and system monitoring.
Exports
- StringStream: Extension of Node's native Readable class for converting a string into a Readable stream.
- argvHasHelpFlag.
- createLogger: Creates a logger instance with colored output and consistent formatting.
- execOutput: Helper function to execute a shell command and return stdout and stderr without throwing on error. If there was an error and nothing was sent to stderr, the error.message takes its place.
- execute: Execute one or multiple shell commands.
- formatTableForTerminal: Formats a 2D array of strings as a terminal table with optional headers and styling.
- getCurrentMemoryUsage: Get the current heap memory usage in megabytes.
- isTerminalColorSupported supported.
- memoryUsage: Returns the memory usage of the Node.js process with values converted from bytes to megabytes and rounded to the specified precision.
- prompt: Prompt the user for input.
- shellSpawnProgram: Spawns a program using child_process.spawn with promise-based interface and optional stdio inheritance control.
- spawnChildProcess: Spawn a child process.
- spawnNodeProcess: Spawn a child node process.
- startPowerShellScript: Executes a PowerShell script with arguments and returns stdout/stderr.
- streamToString: Drain a Readable into a string.
- timer: Executes a task and logs the execution time.
- toError: Converts the given value to an Error object. If the value is already an Error object, it is returned as is. If the value is not an Error object, it is converted to a string and used as the error message.
Installation
npm install @bemoje/nodeUsage
Execute Shell Commands
import { execute } from '@bemoje/node'
// Execute a single command with colored echo
execute('git status')
// Execute silently and capture output
const output = execute('git branch', { silent: true })
// Execute multiple commands
execute(['npm run build', 'npm test'])
// Execute in a specific directory
execute('ls -la', { cwd: '/some/path' })Create Logger
Colored, prefixed terminal logging:
import { createLogger } from '@bemoje/node'
const log = createLogger('MyApp')
log.start('Initializing...') // [MyApp] [START] Initializing...
log.info('Processing data') // [MyApp] [INFO] Processing data
log.done('Complete!') // [MyApp] [DONE] Complete!
log.warn('Disk space low') // [MyApp] [WARN] Disk space low
log.error(new Error('fail')) // [MyApp] [ERROR] Error: fail
log.debug('Debug info') // [MyApp] [DEBUG] Debug infoTimer
Measure and log task execution time:
import { timer } from '@bemoje/node'
// Sync
timer('build', (log) => {
// ... do work
log.info('Step 1 done')
})
// [build] [START]
// [build] [INFO] Step 1 done
// [build] [DONE] 1.23 seconds
// Async
await timer('deploy', async (log) => {
await deployToServer()
})Streams
import { streamToString, StringStream } from '@bemoje/node'
// Convert a Readable stream to string
const content = await streamToString(fs.createReadStream('file.txt'))
// Create a Readable from a string
const stream = new StringStream('hello world')Prompt User Input
import { prompt } from '@bemoje/node'
const name = await prompt('What is your name? ')
// With validation callback
const age = await prompt('Age: ', (input) => {
const n = parseInt(input)
return isNaN(n) ? '' : input // return empty to re-prompt
})Memory Usage
import { memoryUsage, getCurrentMemoryUsage } from '@bemoje/node'
const mem = memoryUsage(2) // rounded to 2 decimals
console.log(mem.heapUsed) // e.g. 45.23 (MB)
const heapMB = getCurrentMemoryUsage() // current heap in MBSpawn Processes
import { spawnChildProcess, spawnNodeProcess } from '@bemoje/node'
await spawnChildProcess('/usr/bin/python3', ['script.py'])
await spawnNodeProcess(['--version'])