Page 4 of 4
yes please
<file>
00001| #!/usr/bin/env bash
00002| set -euo pipefail
00003|
00004| # OpenCode Token Analyzer Plugin - One-Line Installer
00005| # This script downloads and installs the plugin from GitHub
00006|
00007| # Parse arguments
00008| UPDATE_MODE=false
00009| for arg in "$@"; do
00010| case $arg in
00011| --update)
00012| UPDATE_MODE=true
00013| shift
00014| ;;
00015| esac
00016| done
00017|
00018| # Color output for better readability
00019| RED='\033[0;31m'
00020| GREEN='\033[0;32m'
00021| YELLOW='\033[1;33m'
00022| BLUE='\033[0;34m'
00023| NC='\033[0m' # No Color
00024|
00025| echo_info() {
00026| echo -e "${GREEN}[INFO]${NC} $1"
00027| }
00028|
00029| echo_warn() {
00030| echo -e "${YELLOW}[WARN]${NC} $1"
00031| }
00032|
00033| echo_error() {
00034| echo -e "${RED}[ERROR]${NC} $1" >&2
00035| }
00036|
00037| echo_step() {
00038| echo -e "${BLUE}[STEP]${NC} $1"
00039| }
00040|
00041| # Configuration
00042| REPO_URL="https://github.com/ramtinJ95/opencode-tokenscope"
00043| OPENCODE_DIR="${HOME}/.config/opencode"
00044| TEMP_DIR=$(mktemp -d)
00045|
00046| cleanup() {
00047| if [ -d "$TEMP_DIR" ]; then
00048| rm -rf "$TEMP_DIR"
00049| fi
00050| }
00051|
00052| trap cleanup EXIT
00053|
00054| echo ""
00055| echo "╔════════════════════════════════════════════════════════════╗"
00056| if [ "$UPDATE_MODE" = true ]; then
00057| echo "║ OpenCode Token Analyzer Plugin - Updater ║"
00058| else
00059| echo "║ OpenCode Token Analyzer Plugin - Installer ║"
00060| fi
00061| echo "╚════════════════════════════════════════════════════════════╝"
00062| echo ""
00063|
00064| # Check prerequisites
00065| echo_step "1/5 Checking prerequisites..."
00066|
00067| if [ ! -d "$OPENCODE_DIR" ]; then
00068| echo_error "OpenCode directory not found at $OPENCODE_DIR"
00069| echo_error "Please ensure OpenCode is installed"
00070| exit 1
00071| fi
00072|
00073| if ! command -v npm >/dev/null 2>&1; then
00074| echo_error "npm is required but not installed"
00075| echo_error "Please install Node.js and npm first"
00076| exit 1
00077| fi
00078|
00079| if ! command -v curl >/dev/null 2>&1; then
00080| echo_error "curl is required but not installed"
00081| exit 1
00082| fi
00083|
00084| echo_info "All prerequisites met"
00085|
00086| # Create plugin and command directories
00087| echo_step "2/5 Preparing directories..."
00088| mkdir -p "$OPENCODE_DIR/plugin"
00089| mkdir -p "$OPENCODE_DIR/plugin/tokenscope-lib"
00090| mkdir -p "$OPENCODE_DIR/command"
00091| echo_info "Directories ready"
00092|
00093| # Download files
00094| echo_step "3/5 Downloading plugin files..."
00095|
00096| FILES=(
00097| "plugin/tokenscope.ts"
00098| "plugin/tokenscope-lib/types.ts"
00099| "plugin/tokenscope-lib/config.ts"
00100| "plugin/tokenscope-lib/tokenizer.ts"
00101| "plugin/tokenscope-lib/analyzer.ts"
00102| "plugin/tokenscope-lib/cost.ts"
00103| "plugin/tokenscope-lib/subagent.ts"
00104| "plugin/tokenscope-lib/formatter.ts"
00105| "plugin/models.json"
00106| "plugin/package.json"
00107| "plugin/install.sh"
00108| "command/tokenscope.md"
00109| )
00110|
00111| for file in "${FILES[@]}"; do
00112| filename=$(basename "$file")
00113| dir=$(dirname "$file")
00114|
00115| echo_info "Downloading $filename..."
00116|
00117| if curl -fsSL "$REPO_URL/raw/main/$file" -o "$TEMP_DIR/$filename" 2>/dev/null; then
00118| # Move to appropriate directory based on path
00119| if [ "$dir" = "plugin" ]; then
00120| mv "$TEMP_DIR/$filename" "$OPENCODE_DIR/plugin/$filename"
00121| elif [ "$dir" = "plugin/tokenscope-lib" ]; then
00122| mv "$TEMP_DIR/$filename" "$OPENCODE_DIR/plugin/tokenscope-lib/$filename"
00123| else
00124| mv "$TEMP_DIR/$filename" "$OPENCODE_DIR/command/$filename"
00125| fi
00126| else
00127| echo_error "Failed to download $file"
00128| echo_error "Please check your internet connection and try again"
00129| exit 1
00130| fi
00131| done
00132|
00133| echo_info "All files downloaded successfully"
00134|
00135| # Install dependencies
00136| echo_step "4/5 Installing dependencies..."
00137|
00138| DEPS_EXIST=false
00139| if [ -d "$OPENCODE_DIR/plugin/node_modules/js-tiktoken" ] && [ -d "$OPENCODE_DIR/plugin/node_modules/@huggingface/transformers" ]; then
00140| DEPS_EXIST=true
00141| fi
00142|
00143| if [ "$UPDATE_MODE" = true ] && [ "$DEPS_EXIST" = true ]; then
00144| echo_info "Update mode: Dependencies already installed, skipping..."
00145| else
00146| echo_info "This may take 1-2 minutes..."
00147| cd "$OPENCODE_DIR/plugin"
00148| if npm install --prefix "$OPENCODE_DIR/plugin" js-tiktoken@1.0.15 @huggingface/transformers@3.1.2 --save; then
00149| echo_info "Dependencies installed successfully"
00150| else
00151| echo_error "Failed to install dependencies"
00152| echo_error "You can try running manually: cd ~/.config/opencode/plugin && npm install"
00153| exit 1
00154| fi
00155| fi
00156|
00157| # Verify installation
00158| echo_step "5/5 Verifying installation..."
00159|
00160| REQUIRED_FILES=(
00161| "$OPENCODE_DIR/plugin/tokenscope.ts"
00162| "$OPENCODE_DIR/plugin/tokenscope-lib/types.ts"
00163| "$OPENCODE_DIR/plugin/tokenscope-lib/config.ts"
00164| "$OPENCODE_DIR/plugin/tokenscope-lib/tokenizer.ts"
00165| "$OPENCODE_DIR/plugin/tokenscope-lib/analyzer.ts"
00166| "$OPENCODE_DIR/plugin/tokenscope-lib/cost.ts"
00167| "$OPENCODE_DIR/plugin/tokenscope-lib/subagent.ts"
00168| "$OPENCODE_DIR/plugin/tokenscope-lib/formatter.ts"
00169| "$OPENCODE_DIR/plugin/models.json"
00170| "$OPENCODE_DIR/plugin/node_modules/js-tiktoken"
00171| "$OPENCODE_DIR/plugin/node_modules/@huggingface/transformers"
00172| "$OPENCODE_DIR/command/tokenscope.md"
00173| )
00174|
00175| all_present=true
00176| for file in "${REQUIRED_FILES[@]}"; do
00177| if [ ! -e "$file" ]; then
00178| echo_warn "Missing: $file"
00179| all_present=false
00180| fi
00181| done
00182|
00183| if [ "$all_present" = true ]; then
00184| echo_info "All files verified"
00185| else
00186| echo_error "Some files are missing"
00187| exit 1
00188| fi
00189|
00190| # Get installed version
00191| INSTALLED_VERSION=$(grep -o '"version": *"[^"]*"' "$OPENCODE_DIR/plugin/package.json" | cut -d'"' -f4)
00192|
00193| echo ""
00194| echo "╔════════════════════════════════════════════════════════════╗"
00195| if [ "$UPDATE_MODE" = true ]; then
00196| echo "║ Update Complete! ║"
00197| else
00198| echo "║ Installation Complete! ║"
00199| fi
00200| echo "╚════════════════════════════════════════════════════════════╝"
00201| echo ""
00202| echo_info "Version: $INSTALLED_VERSION"
00203| echo_info "Plugin installed at: $OPENCODE_DIR/plugin/tokenscope.ts"
00204| echo_info "Command installed at: $OPENCODE_DIR/command/tokenscope.md"
00205| echo ""
00206| echo_step "Next steps:"
00207| echo " 1. Restart OpenCode"
00208| echo " 2. Type /tokenscope in any session"
00209| echo " 3. View full report: cat token-usage-output.txt"
00210| echo ""
00211| echo_info "For help and documentation, visit:"
00212| echo_info "$REPO_URL"
00213| echo ""
00214|
(End of file - total 214 lines)
</file>
if [ "$all_present" = true ]; then
echo_info "All files verified"
else
echo_error "Some files are missing"
exit 1
fi
if [ "$all_present" = true ]; then
echo_info "All files verified"
else
echo_error "Some files are missing"
exit 1
fi
# Make install.sh executable for future local updates
chmod +x "$OPENCODE_DIR/plugin/install.sh"
Now updating the README:
## Updating
```bash
curl -sSL https://raw.githubusercontent.com/ramtinJ95/opencode-tokenscope/main/plugin/install.sh | bash -s -- --update
```
The `--update` flag skips dependency installation for faster updates.
## Updating
**Option 1: Local script** (if you have the plugin installed)
```bash
bash ~/.config/opencode/plugin/install.sh --update
```
**Option 2: Remote script** (always works)
```bash
curl -sSL https://raw.githubusercontent.com/ramtinJ95/opencode-tokenscope/main/plugin/install.sh | bash -s -- --update
```
The `--update` flag skips dependency installation for faster updates.
[refactor-the-code 4fe10d3] fix: make install.sh executable for local updates
2 files changed, 9 insertions(+)
Done! Now:
1. install.sh adds chmod +x to itself after installation, so future local updates work
2. README shows both options:
- Local: bash ~/.config/opencode/plugin/install.sh --update
- Remote: curl -sSL ... | bash -s -- --update
Using bash <script> explicitly works regardless of execute permissions, so it's reliable for everyone.
all these commits to the readme were done on the old refactor-the-code branch which I had already merged to main and deleted the upstream. How do we create a new branch from main then move these latest readme commits to that branch that we then can make a pr from?
Good catch. Let me help you sort this out. Here's the plan:
1. Fetch/update main
2. Create a new branch from main
3. Cherry-pick the recent commits onto it
Let me first see what commits we need to move:
4fe10d3 fix: make install.sh executable for local updates
5b7aaa8 docs: reorganize README, simplify install/update instructions
99826bd refactor: rename lib/ to tokenscope-lib/ to avoid conflicts
c85d3df docs: update README with new multi-file architecture
238eea2 chore: bump version to 1.3.0
d1fe439 refactor: update install.sh for multi-file plugin structure
6c221ca refactor: simplify tokenscope.ts to use lib modules
7c6e17c refactor: extract OutputFormatter to lib/formatter.ts
a386101 refactor: extract SubagentAnalyzer to lib/subagent.ts
22fa67d refactor: extract CostCalculator to lib/cost.ts
ssh_askpass: exec(/usr/lib/ssh/ssh-askpass): No such file or directory
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
* refactor-the-code
main
remotes/origin/HEAD -> origin/main
remotes/origin/main
Tool execution aborted
Now you have permissions to read from the repo try again
732c88f Merge pull request #1 from ramtinJ95/refactor-the-code
99826bd refactor: rename lib/ to tokenscope-lib/ to avoid conflicts
c85d3df docs: update README with new multi-file architecture
238eea2 chore: bump version to 1.3.0
d1fe439 refactor: update install.sh for multi-file plugin structure
So origin/main already has up to commit 99826bd. We need to move these 2 commits that are only on your local branch:
- 4fe10d3 - fix: make install.sh executable for local updates
- 5b7aaa8 - docs: reorganize README, simplify install/update instructions
Let me create a new branch from main and cherry-pick those:
Switched to branch 'main'
Your branch is up to date with 'origin/main'.
From github.com:ramtinJ95/opencode-tokenscope
* branch main -> FETCH_HEAD
Already up to date.
Switched to a new branch 'docs/update-install-instructions'
[docs/update-install-instructions 7e2c7ea] docs: reorganize README, simplify install/update instructions
Date: Sat Dec 13 14:02:52 2025 +0100
1 file changed, 111 insertions(+), 141 deletions(-)
[docs/update-install-instructions d689404] fix: make install.sh executable for local updates
Date: Sat Dec 13 14:04:13 2025 +0100
2 files changed, 9 insertions(+)
Done! Now let's push and create the PR:
remote:
remote: Create a pull request for 'docs/update-install-instructions' on GitHub by visiting:
remote: https://github.com/ramtinJ95/opencode-tokenscope/pull/new/docs/update-install-instructions
remote:
To github.com:ramtinJ95/opencode-tokenscope.git
* [new branch] docs/update-install-instructions -> docs/update-install-instructions
branch 'docs/update-install-instructions' set up to track 'origin/docs/update-install-instructions'.
Branch is pushed. Would you like me to create the PR for you, or would you prefer to do it manually via the link?
If you want me to create it, I can use:
gh pr create --title "..." --body "..."