electronmon

repository·master·Indexed 41 days ago

https://github.com/catdad/electronmon

A tool to watch and automatically restart or reload Electron applications when files change. It distinguishes between main process changes (triggering a full restart) and renderer process changes (triggering a window reload). Supports CLI usage, configuration via package.json, and a programmatic API for controlling the application lifecycle via methods like reload(), restart(), close(), and destroy(). Compatible with Electron versions 8 through 23 and Node.js versions 10 through 18.

Tokens
1.8K
Snippets
7
Records
9
Agent score
52%

What's inside electronmon

  1. Use electronmon to watch and reload Electron applications

    master

    To use electronmon, replace the electron command with electronmon when launching your application. You can pass the same arguments to electronmon that you would normally pass to the electron CLI.

    electronmon automatically watches your files:

    • Changes to main process files trigger a full application restart.
    • Changes to renderer process files trigger a reload of the application's browser windows.

    It works in-process and will not exit on the first application relaunch.

    npx electronmon .
  2. Configure electronmon via package.json

    master

    You can configure electronmon by adding an electronmon object to your package.json.

    Currently, you can specify additional glob patterns to watch using the patterns key. This adds to the default patterns (['**/*', '!node_modules', '!node_modules/**/*', '!.*', '!**/*.map']). To ignore specific files or directories, prefix the glob with !.

    {
      "electronmon": {
        "patterns": ["!test/**"]
      }
    }
  3. Use the electronmon programmatic API

    master

    If you need to interact with the monitor process programmatically, you can require electronmon as a module. Calling the exported function returns an app object used to control the application lifecycle.

    Initialization

    const electronmon = require('electronmon');
    
    (async () => {
      const options = { ... };
      const app = await electronmon(options);
    })();

    Options

    All options are optional and use reasonable defaults:

    • cwd (String): The root directory of your application.
    • args (Array<String>): Arguments to pass to electron.
    • env (Object): Additional environment variables for the electron process.
    • patterns (Array<String>): Additional glob patterns to watch (added to defaults).
    • logLevel (String): Logging level. Possible values: verbose, info, error, quiet.
    • electronPath (String): Path to the electron binary.

    App Methods

    All methods are asynchronous and return a Promise:

    • app.reload(): Reloads all open web views of your application.
    • app.restart(): Restarts the entire electron process.
    • app.close(): Closes the entire electron process and waits for file changes to restart it.
    • app.destroy(): Closes the entire electron process and stops monitoring.
  4. Configure electronmon options

    master

    When initializing electronmon, you can pass an options object to customize the behavior:

    OptionTypeDefaultDescription
    cwdstringprocess.cwd()The current working directory for the application and watcher.
    argsstring[]['.']Arguments passed to the Electron executable.
    envobject{}Environment variables to merge with process.env.
    logLevelstring'info'Logging level (e.g., 'info', 'verbose'). Sets ELECTRONMON_LOGLEVEL.
    electronPathstringrequire('electron')Path to the Electron executable. If not provided, it attempts to require the electron package.
    stdioArray[process.stdin, process.stdout, process.stderr]The stdio configuration. Supports inheriting or piping streams.
    patternsstring[][]File patterns for the watcher to monitor.
  5. Configure the special signal via ELECTRONMON_SPECIAL_SIGNAL

    master

    You can customize the special signal used by electronmon by setting the ELECTRONMON_SPECIAL_SIGNAL environment variable. If this environment variable is not provided, the system defaults to the value 37. The value is cast to a Number.

    # Example: setting the signal to 42
    ELECTRONMON_SPECIAL_SIGNAL=42 electronmon
  6. Use electronmon programmatically

    master

    You can use electronmon as a module to wrap an Electron application. It provides a development environment that watches for file changes and automatically restarts the main process or reloads the renderer process based on the type of change detected.

    When you call the main function, it returns an object containing control methods to manage the lifecycle of the watched application.

    const electronmon = require('electronmon');
    
    // Initialize electronmon
    electronmon({
      cwd: process.cwd(),
      args: ['.'],
      env: { MY_VAR: 'value' },
      logLevel: 'info',
      electronPath: '/path/to/electron',
      patterns: ['**/*.js', '**/*.html']
    }).then((manager) => {
      // manager contains: close, destroy, reload, restart
      
      // Example: reload the app manually
      // manager.reload();
    
      // Example: clean up when your script exits
      // manager.destroy();
    });
  7. Manage the electronmon lifecycle with the returned manager

    master

    The electronmon function returns a Promise that resolves to a manager object. This object allows you to control the running Electron process and the file watcher:

    • close(): Sends a SIGINT to the Electron application and waits for it to exit.
    • destroy(): Closes the Electron application and stops the file watcher.
    • reload(): A convenience method to reload the renderer thread in the app. If the app isn't running, it performs a full restart().
    • restart(): Closes the current application and re-initializes the entire environment (watcher and app).
    const manager = await electronmon();
    
    await manager.reload();   // Reload renderer
    await manager.restart();  // Full restart
    await manager.close();    // Stop app
    await manager.destroy(); // Stop app and watcher
  8. Run electronmon via CLI

    master

    You can run electronmon directly from the command line. The CLI entrypoint automatically resolves the local electron installation in the current working directory and passes command-line arguments, the current working directory, and configured patterns to the core engine.

    Environment Variables

    VariableDefaultDescription
    ELECTRONMON_LOGLEVELinfoSets the logging verbosity.

    Configuration via package.json

    The CLI reads a electronmon configuration object from your package.json file. Specifically, it looks for a patterns array to define which files or directories to monitor.

    {
      "electronmon": {
        "patterns": ["src/**/*.js", "dist/**/*.js"]
      }
    }
    # Example usage (assuming electronmon is installed globally or via npx)
    
    npx electronmon
    
    # Or with custom log level
    
    ELECTRONMON_LOGLEVEL=debug npx electronmon