0.3.0 • Published 8 months ago

@rechunk/utils v0.3.0

Weekly downloads
-
License
MIT
Repository
-
Last release
8 months ago

@rechunk/utils

Utility functions and helpers for ReChunk packages and implementations.

Features

  • 🔍 File system utilities
  • 🔄 Process management
  • 🛠️ Path resolution
  • 🚀 Development server detection
  • 📦 Workspace utilities
  • ⚡️ Performance optimizations

Installation

npm install @rechunk/utils

# or with yarn
yarn add @rechunk/utils

# or with pnpm
pnpm add @rechunk/utils

API Reference

File System Utilities

getRealPath

Resolves the real native path for a given file path, handling case sensitivity issues.

import {getRealPath} from '@rechunk/utils';

const realPath = getRealPath('/path/to/file');

findClosestJSON

Recursively searches for the closest JSON file from a starting directory.

import {findClosestJSON} from '@rechunk/utils';

const config = findClosestJSON('.rechunkrc.json');
const pkg = findClosestJSON('package.json');

Workspace Utilities

findWorkspaceDir

Locates the workspace directory by finding package manager files.

import {findWorkspaceDir} from '@rechunk/utils';

const workspaceDir = findWorkspaceDir(process.cwd());

Process Management

ProcessInfo Interface

interface ProcessInfo {
  pid: number; // Process ID
  ppid: number; // Parent Process ID
  uid: number; // User ID
  cpu: number; // CPU usage percentage
  memory: number; // Memory usage percentage
  name: string; // Process name
  cmd: string; // Full command line
}

nonWindowsCall

Retrieves information about running processes on non-Windows systems.

import {nonWindowsCall} from '@rechunk/utils';

const processes = nonWindowsCall({all: true});

Development Server

isRechunkDevServerRunning

Checks if the ReChunk development server is currently running.

import {isRechunkDevServerRunning} from '@rechunk/utils';

if (isRechunkDevServerRunning()) {
  console.log('Dev server is running');
}

Use Cases

Configuration File Management

import {findClosestJSON, getRealPath} from '@rechunk/utils';

// Find and load configuration
const configPath = getRealPath('./config');
const config = findClosestJSON('.rechunkrc.json', configPath);

Workspace Detection

import {findWorkspaceDir} from '@rechunk/utils';

// Set up workspace environment
const workspaceDir = findWorkspaceDir(process.cwd());
process.env.WORKSPACE_DIR = workspaceDir;

Process Monitoring

import {nonWindowsCall, ProcessInfo} from '@rechunk/utils';

// Monitor specific processes
const processes: ProcessInfo[] = nonWindowsCall();
const nodeProcesses = processes.filter(p => p.name.includes('node'));

Development Environment

import {isRechunkDevServerRunning} from '@rechunk/utils';

// Configure based on dev server status
const isDev = isRechunkDevServerRunning();
const config = {
  mode: isDev ? 'development' : 'production',
  // ... other config
};

Best Practices

  1. Path Resolution

    // Recommended
    const path = getRealPath(filePath);
    
    // Avoid
    const path = filePath; // Might have case sensitivity issues
  2. Configuration Loading

    // Recommended
    const config = findClosestJSON('.rechunkrc.json');
    
    // Avoid
    const config = require('.rechunkrc.json'); // Might fail if not in exact location
  3. Process Management

    // Recommended
    const processes = nonWindowsCall({all: true});
    
    // Avoid
    const {execSync} = require('child_process');
    const output = execSync('ps aux'); // Less reliable and platform-dependent

Error Handling

The utilities include built-in error handling and fallbacks:

// File not found fallback
const config = findClosestJSON('missing.json'); // Returns {}

// Path resolution fallback
const path = getRealPath('invalid/path'); // Returns original path

// Process information error handling
try {
  const processes = nonWindowsCall();
} catch (error) {
  console.error('Failed to get process information:', error);
}

Contributing

Contributions are welcome! Please read our contributing guidelines first.

License

MIT