Run On Save for VS Code

repository·main·Indexed 42 days ago

https://github.com/emeraldwalk/vscode-runonsave

A VS Code extension (version 1.1.5) that automatically triggers shell commands when a file is saved. It features regex-based file filtering via match and notMatch options, asynchronous command execution, and dynamic command parameters using placeholder tokens for file paths and environment variables.

Tokens
1.9K
Snippets
5
Records
12
Agent score
52%

What's inside Run On Save

  1. How to use RegEx for file matching

    main

    The match and notMatch options use Regular Expressions against the absolute file path.

    • Avoid ^: Since the pattern matches against the absolute path, starting with ^ will likely fail unless you provide the full absolute path. Use "match": "somefile\.txt$" instead of "match": "^somefile\.txt$".
    • JSON Escaping: Because settings are in JSON, backslashes must be double-escaped. On Windows, a path like C:\folder requires "some\\folder\\.*" in your config.
  2. Configure Run On Save

    main

    To use the extension, add the emeraldwalk.runonsave configuration object to your VS Code user or workspace settings. Commands are triggered when saving an existing file. Note that creating new files or using 'Save as...' does not trigger commands.

    {
      "emeraldwalk.runonsave": {
        "commands": [
          {
            "match": ".*",
            "cmd": "echo 'Hello World'"
          }
        ]
      }
    }
  3. Reference: Global Configuration Options

    main

    These options are applied to the top-level emeraldwalk.runonsave configuration object.

    • shell (optional): Shell path to be used with child_process.exec.
    • autoClearConsole (optional): Clears the VSCode output console before commands run. Defaults to false.
    • ignoreUnchangedFiles (optional): Ignores files that haven't changed since the last save. Defaults to false.
    • message: Message to output before all commands start.
    • messageAfter: Message to output after all commands finish.
    • showElapsed: If true, shows total elapsed time after all commands finish.
  4. Reference: Command Object Options

    main

    Each object in the commands array defines a specific task to run.

    • match: A RegEx pattern for matching files to run this command on.
    • notMatch: A RegEx pattern for files to NOT run. This takes precedence over match.
    • cmd: The command to execute. Supports placeholder tokens.
    • isAsync (optional): If true, the next command in the array will start before this one finishes. Defaults to false.
    • message: Message to output before this specific command. Supports placeholders.
    • messageAfter: Message to output after this specific command finishes. Supports placeholders.
    • showElapsed: If true, shows elapsed time for this specific command.
    • autoShowOutputPanel: Controls output panel visibility:
      • never: Default behavior.
      • always: Shows the panel when the first command starts.
      • error: Shows the panel only when a command fails.
  5. Configure RunOnSave settings

    main

    The IConfig interface defines the top-level configuration for the extension. You can configure the shell used for execution, whether to clear the console automatically, and whether to ignore files that haven't changed. The core functionality is driven by the commands array.

    {
      "shell": "/bin/bash",
      "autoClearConsole": true,
      "ignoreUnchangedFiles": false,
      "showElapsed": true,
      "commands": [
        {
          "cmd": "echo 'hello'",
          "match": "\\.txt$",
          "isAsync": false
        }
      ]
    }
  6. Define a command in RunOnSave

    main

    Each command in the commands array is defined by the ICommand interface. You can specify which files to trigger the command using match (regex) or notMatch (regex exclusion), the command string itself (cmd), and whether it should run asynchronously (isAsync). You can also control how the output panel is displayed using autoShowOutputPanel.

    {
      "cmd": "npm test",
      "match": "\\.ts$",
      "notMatch": "\\.spec\.ts$",
      "isAsync": true,
      "autoShowOutputPanel": "error",
      "message": "Running tests...",
      "messageAfter": "Tests finished.",
      "showElapsed": true
    }
  7. Configure command messaging and output visibility

    main

    Both the global configuration and individual commands support messaging options via IMessageConfig. This allows you to provide feedback in the VS Code UI when commands run.

    • message: A string to display when a command starts.
    • messageAfter: A string to display after a command completes.
    • showElapsed: A boolean indicating whether to show the time taken for execution.
    • autoShowOutputPanel: (Command-specific) Controls the visibility of the output panel. Options are 'always', 'error', or 'never'.
  8. Reference: Placeholder Tokens

    main

    Commands and messages support placeholders for file and environment information.

    File Tokens:

    • ${workspaceFolder}: Path of the workspace folder of the saved file.
    • ${file}: Full path of the saved file.
    • ${fileBasename}: Filename including extension.
    • ${fileDirname}: Directory name of the saved file.
    • ${fileExtname}: File extension (including the dot).
    • ${fileBasenameNoExt}: Filename without the extension.
    • ${relativeFile}: Path of the file relative to ${workspaceFolder}.
    • ${cwd}: The current working directory VS Code is running in (not the project directory).

    Environment Tokens:

    • ${env.Name}: Access environment variables (e.g., ${env.USERNAME}).
  9. Manage Run On Save state via commands

    main

    You can manually control whether the extension is active using the following VS Code commands. These commands toggle the isEnabled state of the extension, allowing you to pause or resume automatic command execution without changing your global configuration.

    extension.emeraldwalk.enableRunOnSave
    extension.emeraldwalk.disableRunOnSave
    extension.emeraldwalk.toggleRunOnSave
  10. Available Placeholder Tokens for Configuration

    main

    When configuring commands in Run On Save, you can use placeholder tokens to dynamically inject file and workspace information into your commands. These tokens are replaced at runtime based on the file being saved.

    File and Workspace Tokens

    • ${file}: The absolute path of the current file.
    • ${workspaceRoot}: The path of the workspace root (deprecated, use ${workspaceFolder}).
    • ${workspaceFolder}: The path of the workspace folder containing the file.
    • ${fileBasename}: The filename including the extension (e.g., index.ts).
    • ${fileDirname}: The directory path of the current file.
    • ${fileExtname}: The file extension (e.g., .ts).
    • ${fileBasenameNoExt}: The filename without the extension (e.g., index).
    • ${relativeFile}: The path of the file relative to the workspace folder.
    • ${cwd}: The current working directory of the process.

    Environment Variable Tokens

    • ${env.VARIABLE_NAME}: Injects the value of an environment variable named VARIABLE_NAME.
    ${file}
    ${workspaceRoot}
    ${workspaceFolder}
    ${fileBasename}
    ${fileDirname}
    ${fileExtname}
    ${fileBasenameNoExt}
    ${relativeFile}
    ${cwd}
    ${env.NAME}