Created
January 4, 2026 21:37
-
-
Save tanishqkancharla/7fa7b4ef95f361cac0e1594cc08d8034 to your computer and use it in GitHub Desktop.
OpenCode plugin: Bash preprocessor that converts absolute paths to relative paths
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import type { Plugin } from "@opencode-ai/plugin"; | |
| export const BashPreprocessor: Plugin = async ({ directory }) => { | |
| const repoPath = directory; | |
| return { | |
| "tool.execute.before": async (input, output) => { | |
| if (input.tool === "bash" && output.args?.command) { | |
| let command = output.args.command as string; | |
| // Escape special regex characters in the repo path | |
| const escapedRepoPath = repoPath.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); | |
| // Pattern 1: Remove "cd <repo-path> && " from the start | |
| const cdPrefixRegex = new RegExp(`^cd\\s+${escapedRepoPath}\\s+&&\\s+`); | |
| command = command.replace(cdPrefixRegex, ""); | |
| // Pattern 2: Replace all occurrences of absolute repo path with relative path | |
| // This handles cases like /path/to/repo/src/... -> src/... | |
| const repoPathRegex = new RegExp(escapedRepoPath + "/", "g"); | |
| command = command.replace(repoPathRegex, ""); | |
| // Pattern 3: Replace standalone repo path (not followed by /) | |
| // This handles cases where the repo path appears alone (e.g., as an argument) | |
| const standaloneRepoPathRegex = new RegExp( | |
| `\\b${escapedRepoPath}\\b(?!/)`, | |
| "g", | |
| ); | |
| command = command.replace(standaloneRepoPathRegex, "."); | |
| output.args.command = command; | |
| } | |
| }, | |
| }; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment