Ā 

nalaka/auto_reset

0.1.0
Ballerimon šŸš€

The nodemon for Ballerina - Auto-restart your Ballerina applications during development.

Ballerimon is a command-line tool that monitors your Ballerina files and automatically restarts your application when changes are detected. Just like nodemon for Node.js, but for Ballerina!

Features

  • šŸ” Auto-monitoring: Watches .bal and .toml files automatically
  • šŸš€ Auto-restart: Instantly restarts your app when files change
  • šŸŽÆ Smart filtering: Ignores build directories and common artifacts
  • āš™ļø CLI-friendly: Simple command-line interface
  • šŸ–„ļø Cross-platform: Works on Windows, macOS, and Linux
  • ļæ½ Beautiful output: Clean, emoji-rich console messages

Installation

Copy
bal tool install nalaka/auto_reset

Option 2: From Source

Copy
git clone <repository>
cd ballerimon
bal build

Usage

Ballerimon works exactly like nodemon - just prefix your Ballerina command with ballerimon:

Copy
# Instead of: bal run
ballerimon bal run

# Instead of: bal run service.bal  
ballerimon bal run service.bal

# Instead of: bal run --debug=5005
ballerimon bal run --debug=5005

# Works with any Ballerina command
ballerimon bal test

Examples

Basic HTTP Service

Copy
# Start your service with auto-restart
ballerimon bal run service.bal

Debug Mode

Copy
# Run with debugging enabled
ballerimon bal run --debug=5005 service.bal

Custom Package

Copy
# Run a specific package
ballerimon bal run mypackage

Testing

Copy
# Auto-run tests when files change
ballerimon bal test

How It Works

  1. Start Monitoring: Ballerimon starts watching your current directory
  2. Run Command: Executes your Ballerina command (e.g., bal run)
  3. Watch for Changes: Monitors .bal and .toml files
  4. Auto-restart: When changes are detected:
    • Stops the current process
    • Waits for additional changes (debounce)
    • Restarts your application

Output Example

šŸš€ Ballerimon - Auto-restart tool for Ballerina applications
Similar to nodemon for Node.js

šŸ“ Watching: ./
šŸ”§ Command: bal run service.bal
šŸ‘€ Monitoring .bal files for changes...

Press Ctrl+C to stop watching

šŸ” Starting file watcher...
ā–¶ļø  Starting application...
āœ… Application started successfully

šŸ“ File changed: service.bal

šŸ”„ Changes detected - restarting...
ā¹ļø  Stopping application...
ā–¶ļø  Starting application...  
āœ… Application started successfully

Comparison with nodemon

If you're familiar with nodemon for Node.js, here's how Ballerimon compares:

FeaturenodemonBallerimon
Auto-restartāœ…āœ…
File watchingāœ…āœ…
CLI interfaceāœ…āœ…
Cross-platformāœ…āœ…
LanguageNode.jsBallerina
Configurationnodemon.jsonCommand line

Migration from Manual Workflow

Before (Manual):

Copy
# Edit your files...
# Ctrl+C to stop
bal run service.bal
# Make more changes...
# Ctrl+C to stop again
bal run service.bal
# Repeat...

After (Ballerimon):

Copy
ballerimon bal run service.bal
# Edit files and see automatic restarts! šŸŽ‰

Configuration

Ballerimon uses sensible defaults but can be customized:

  • Watch Extensions: .bal, .toml
  • Ignore Paths: target/, .ballerina/, tests/, .git/, node_modules/
  • Debounce Time: 1000ms (1 second)
  • Working Directory: Current directory

Real-World Examples

Web API Development

Copy
# Start your REST API with auto-restart
ballerimon bal run api.bal

# Your service.bal file:
service /api on new http:Listener(8080) {
    resource function get hello() returns string {
        return "Hello, World!";
    }
}

Microservice Development

Copy
# Start multiple services (use different terminals)
ballerimon bal run user-service.bal    # Terminal 1
ballerimon bal run order-service.bal   # Terminal 2
ballerimon bal run payment-service.bal # Terminal 3

Integration Testing

Copy
# Auto-run tests when code changes
ballerimon bal test

# Specific test file
ballerimon bal test tests/integration_test.bal

Best Practices

1. Use with Version Control

Add Ballerimon to your development workflow:

Copy
# Start development server
ballerimon bal run service.bal

# In another terminal, continue with git workflow
git add .
git commit -m "Add new feature"

2. Environment Variables

Copy
# Set environment variables before starting
export DATABASE_URL=localhost:5432
ballerimon bal run service.bal

3. Debug Mode

Copy
# Enable debugging and auto-restart
ballerimon bal run --debug=5005 service.bal

4. Production vs Development

Copy
# Development (with Ballerimon)
ballerimon bal run service.bal

# Production (without Ballerimon)  
bal run service.bal

Configuration Options

OptionTypeDefaultDescription
projectPathstring"./"Path to the directory to watch
watchExtensionsstring[][".bal"]File extensions to monitor
ignorePathsstring[]["target/", ".ballerina/", "tests/"]Paths to ignore during monitoring
debounceMsint500Delay in milliseconds before restarting after detecting changes
ballerinaCmdstring"bal run"Command to execute when restarting
verbosebooleanfalseEnable detailed logging

Use Cases

1. Web Service Development

Monitor your Ballerina HTTP service and restart automatically:

Copy
auto_reset:WatchConfig config = {
    projectPath: "./",
    ballerinaCmd: "bal run service.bal",
    verbose: true,
    debounceMs: 800
};

2. Multi-Module Projects

Watch specific modules or the entire project:

Copy
auto_reset:WatchConfig config = {
    projectPath: "./modules/core",
    watchExtensions: [".bal", ".toml"],
    ballerinaCmd: "bal build && bal run target/bin/myapp.jar"
};

3. Integration Testing

Monitor test files and run tests automatically:

Copy
auto_reset:WatchConfig config = {
    projectPath: "./tests",
    ballerinaCmd: "bal test",
    ignorePaths: ["target/", ".ballerina/"]
};

API Reference

Types

WatchConfig

Configuration record for the file watcher:

Copy
public type WatchConfig record {|
    string projectPath = "./";
    string[] watchExtensions = [".bal"];
    string[] ignorePaths = ["target/", ".ballerina/", "tests/"];
    int debounceMs = 500;
    string ballerinaCmd = "bal run";
    boolean verbose = false;
|};

Functions

startWithConfig(WatchConfig) returns error?

Start watching with a custom configuration.

createWatcher(WatchConfig) returns BallerinaWatcher

Create a watcher instance with custom configuration.

Classes

BallerinaWatcher

Main watcher class with the following methods:

  • startWatching() returns error? - Start monitoring files
  • stop() returns error? - Stop the watcher and cleanup

Examples

Example 1: Basic HTTP Service Watcher

Copy
// watcher.bal
import nalaka/auto_reset;

public function main() returns error? {
    auto_reset:WatchConfig config = {
        ballerinaCmd: "bal run http_service.bal",
        verbose: true
    };
    
    return auto_reset:startWithConfig(config);
}

Example 2: Custom Development Workflow

Copy
// dev-watcher.bal
import nalaka/auto_reset;
import ballerina/log;

public function main() returns error? {
    log:printInfo("Starting development watcher...");
    
    auto_reset:WatchConfig config = {
        projectPath: "./src",
        watchExtensions: [".bal", ".json", ".yaml"],
        ignorePaths: ["target/", ".ballerina/", "tests/", "docs/", "*.log"],
        debounceMs: 1000,
        ballerinaCmd: "bal build && bal run target/bin/myapp.jar --config=dev.yaml",
        verbose: true
    };
    
    auto_reset:BallerinaWatcher watcher = auto_reset:createWatcher(config);
    return watcher.startWatching();
}

Best Practices

  1. Use Appropriate Debounce Time: Set debounceMs to a reasonable value (500-1000ms) to avoid excessive restarts during rapid file changes.

  2. Ignore Build Artifacts: Always include build directories in ignorePaths to prevent infinite restart loops.

  3. Specific Watch Extensions: Only watch file types that actually affect your application to reduce unnecessary restarts.

  4. Environment-Specific Commands: Use different ballerinaCmd for development vs. production-like testing.

Troubleshooting

Common Issues

Q: The watcher keeps restarting infinitely A: Check your ignorePaths configuration. Make sure to ignore build directories like target/ and .ballerina/.

Q: Changes aren't being detected A: Verify that your file extensions are included in watchExtensions and the files aren't in ignored paths.

Q: The application doesn't start A: Check that your ballerinaCmd is correct and can be executed from the projectPath directory.

Q: Too many restarts happening A: Increase the debounceMs value to allow more time for multiple file changes to settle.

Debug Mode

Enable verbose logging to troubleshoot issues:

Copy
auto_reset:WatchConfig config = {
    verbose: true  // Enable detailed logging
};

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Submit a pull request

License

This project is licensed under the MIT License. See the LICENSE file for details.

Changelog

v0.1.0

  • Initial release
  • Basic file watching functionality
  • Configurable watch patterns and ignore paths
  • Auto-restart capability
  • Verbose logging support

Support

If you encounter any issues or have questions:

  1. Check the troubleshooting section above
  2. Search existing issues in the repository
  3. Create a new issue with detailed information about your problem

Happy coding with auto-restart! šŸš€

Import

import nalaka/auto_reset;Copy

Other versions

Metadata

Released date:Ā about 1 year ago

Version:Ā 0.1.0


Compatibility

Platform:Ā any

Ballerina version:Ā 2201.12.3

GraalVM compatible:Ā Yes


Pull count

Total:Ā 1

Current verison:Ā 0


Weekly downloads