# shbat **Repository Path**: nodets/shbat ## Basic Information - **Project Name**: shbat - **Description**: ShBat is a TypeScript library that allows you to generate both Windows Batch (.bat) and Unix Shell (.sh) scripts using a simple chained API - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-09-17 - **Last Updated**: 2025-09-17 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # ShBat - Cross-Platform Script Generator ShBat is a TypeScript library that allows you to generate both Windows Batch (.bat) and Unix Shell (.sh) scripts using a simple chained API. It supports variables, output, directory operations, file copy/delete, program launching, conditional statements, and loops. ## Features - Generate both Windows Batch and Unix Shell scripts simultaneously - Simple chained API for building scripts - Automatic variable handling with cross-platform path support - Support for conditional statements and loops - Built-in methods for common operations (mkdir, copy, delete, etc.) ## Installation ```bash npm install shbat ``` ## Usage ### Basic Usage ```typescript import { shbat } from 'shbat'; // Create a cross-platform script shbat.set('SOURCE', 'C:\\MyProject') .set('DEST', 'D:\\Backup') .log('Starting backup process...') .log('Source: {SOURCE}') .log('Destination: {DEST}') .mkdir('{DEST}') .copy('{SOURCE}\\*.txt', '{DEST}') .log('Backup completed!') .saveBoth('backup'); // Generates both backup.bat and backup.sh ``` ### Platform-Specific Scripts ```typescript import { bat, sh } from 'shbat'; // Windows Batch script bat.set('PROJECT', 'C:\\MyProject') .log('Project path: {PROJECT}') .mkdir('{PROJECT}\\logs') .save('windows-setup.bat'); // Unix Shell script sh.set('PROJECT', '/home/user/myproject') .log('Project path: {PROJECT}') .mkdir('{PROJECT}/logs') .save('unix-setup.sh'); ``` ### Advanced Features #### Conditional Statements ```typescript shbat.set('SOURCE', 'C:\\MyProject') .ifExist('{SOURCE}', (script) => { script.log('Source directory exists') .copy('{SOURCE}\\*', '{SOURCE}\\backup'); }) .saveBoth('conditional-backup'); ``` #### File Loops ```typescript shbat.set('LOG_DIR', '/var/logs') .forFile('{LOG_DIR}/*.log', (file) => { shbat.log('Processing log file: ' + file) .copy(file, '{LOG_DIR}/archive/'); }) .saveBoth('process-logs'); ``` #### User Input ```typescript shbat.input('USERNAME', 'Enter your name: ') .log('Hello, {USERNAME}!') .saveBoth('greeting'); ``` ## API Reference ### Core Methods | Method | Description | |--------|-------------| | `set(name, value)` | Define a variable | | `log(text)` | Output text (supports variable substitution with {VAR}) | | `blank()` | Add a blank line | | `mkdir(dir)` | Create directory (if not exists) | | `copy(from, to)` | Copy files/directories recursively | | `del(file)` | Delete files (supports wildcards) | | `start(app)` | Start an application | | `pause()` | Pause execution (wait for user input) | | `input(varName, prompt)` | Get user input and store in variable | ### Control Flow | Method | Description | |--------|-------------| | `ifExist(path, callback)` | Execute callback if path exists | | `forFile(pattern, handler)` | Loop through matching files | ### Output Methods | Method | Description | |--------|-------------| | `save(path)` | Save script to file (extension determines type) | | `saveBoth(baseName)` | Save both .bat and .sh versions | ## Variable Substitution Variables are defined with `set()` and referenced using curly braces: ```typescript shbat.set('SOURCE', 'C:\\MyProject') .log('Source path: {SOURCE}'); // Outputs: Source path: C:\MyProject ``` ## Examples ### Complete Backup Script ```typescript import { shbat } from 'shbat'; shbat.set('SOURCE', 'C:\\MyProject') .set('BACKUP', 'D:\\Backups\\MyProject') .log('=== Backup Process Started ===') .log('Source: {SOURCE}') .log('Backup: {BACKUP}') .blank() .ifExist('{SOURCE}', (script) => { script.log('Creating backup directory...') .mkdir('{BACKUP}') .log('Copying files...') .copy('{SOURCE}\\*', '{BACKUP}') .log('Backup completed successfully!'); }) .ifExist('{BACKUP}', (script) => { script.log('Verifying backup...') .forFile('{BACKUP}\\*.txt', (file) => { script.log('Verified file: ' + file); }); }) .blank() .log('=== Process Complete ===') .pause() .saveBoth('backup-project'); ``` This will generate both `backup-project.bat` and `backup-project.sh` with appropriate platform-specific syntax. ## License MIT