From f33969753232be2adaa9d13022fe1f78643f9664 Mon Sep 17 00:00:00 2001 From: Chapman Pendery Date: Sat, 7 Oct 2023 20:28:52 -0700 Subject: [PATCH] feat: make cli executable & update all keybinding sets to support new cli surface & inject accepted results into history Signed-off-by: Chapman Pendery --- .gitignore | 9 ------- package-lock.json | 3 +++ package.json | 11 +++++++-- shell/key-bindings-powershell.ps1 | 26 ++++++++++++++++++++ shell/key-bindings-pwsh.ps1 | 26 ++++++++++++++++++++ shell/key-bindings.bash | 5 ++-- shell/key-bindings.ps1 | 40 ------------------------------- src/commands/root.ts | 1 - src/index.ts | 2 ++ 9 files changed, 68 insertions(+), 55 deletions(-) create mode 100644 shell/key-bindings-powershell.ps1 create mode 100644 shell/key-bindings-pwsh.ps1 delete mode 100644 shell/key-bindings.ps1 diff --git a/.gitignore b/.gitignore index 2541dd0..b51ea71 100644 --- a/.gitignore +++ b/.gitignore @@ -1,11 +1,2 @@ node_modules/ -.fig/ -*.log -__debug_*.exe -debug.test*.exe -clac -.env* -dist/ -t*.md -__pycache__ build/ \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index c319c61..7c4829b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,6 +16,9 @@ "react": "^18.2.0", "wrap-ansi": "^8.1.0" }, + "bin": { + "clac": "build/index.js" + }, "devDependencies": { "@tsconfig/node18": "^18.2.2", "@types/ink": "^2.0.3", diff --git a/package.json b/package.json index ab9328c..55b1a2a 100644 --- a/package.json +++ b/package.json @@ -2,8 +2,15 @@ "name": "@microsoft/clac", "version": "0.0.0", "description": "IDE style command line auto complete", - "main": "./build/index.js", "type": "module", + "bin": { + "clac": "./build/index.js" + }, + "files": [ + "build/**", + "*.md", + "LICENSE" + ], "scripts": { "build": "tsc", "start": "node ./build/index.js", @@ -39,4 +46,4 @@ "ts-jest": "^29.1.1", "typescript": "^5.2.2" } -} +} \ No newline at end of file diff --git a/shell/key-bindings-powershell.ps1 b/shell/key-bindings-powershell.ps1 new file mode 100644 index 0000000..d425454 --- /dev/null +++ b/shell/key-bindings-powershell.ps1 @@ -0,0 +1,26 @@ +Set-PSReadLineKeyHandler -Chord 'Ctrl+a' -ScriptBlock { + $command = $null + [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$command, [ref]$null) + + $oldPrompt = $function:prompt + function prompt {"`r"} + [Microsoft.PowerShell.PSConsoleReadLine]::InvokePrompt() + $prompt = $oldPrompt + + [Microsoft.PowerShell.PSConsoleReadLine]::ClearKillRing() + [Microsoft.PowerShell.PSConsoleReadLine]::BeginningOfLine() + [Microsoft.PowerShell.PSConsoleReadLine]::KillLine() + + $clac = "$env:USERPROFILE\AppData\Roaming\npm\node_modules\@microsoft\clac\build\index.js" + if ($command) { + Start-Process -NoNewWindow -Wait "node" "$clac -c $command -s powershell" + } else { + Start-Process -NoNewWindow -Wait "node" "$clac -s powershell" + } + + $executedCommand = node $clac --history + if ($executedCommand) { + [Microsoft.PowerShell.PSConsoleReadLine]::AddToHistory($executedCommand) + } + [Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine() +} \ No newline at end of file diff --git a/shell/key-bindings-pwsh.ps1 b/shell/key-bindings-pwsh.ps1 new file mode 100644 index 0000000..5000dd3 --- /dev/null +++ b/shell/key-bindings-pwsh.ps1 @@ -0,0 +1,26 @@ +Set-PSReadLineKeyHandler -Chord 'Ctrl+a' -ScriptBlock { + $command = $null + [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$command, [ref]$null) + + $oldPrompt = $function:prompt + function prompt {"`r"} + [Microsoft.PowerShell.PSConsoleReadLine]::InvokePrompt() + $prompt = $oldPrompt + + [Microsoft.PowerShell.PSConsoleReadLine]::ClearKillRing() + [Microsoft.PowerShell.PSConsoleReadLine]::BeginningOfLine() + [Microsoft.PowerShell.PSConsoleReadLine]::KillLine() + + $clac = "$env:USERPROFILE\AppData\Roaming\npm\node_modules\@microsoft\clac\build\index.js" + if ($command) { + Start-Process -NoNewWindow -Wait "node" "$clac -c $command -s pwsh" + } else { + Start-Process -NoNewWindow -Wait "node" "$clac -s pwsh" + } + + $executedCommand = node $clac --history + if ($executedCommand) { + [Microsoft.PowerShell.PSConsoleReadLine]::AddToHistory($executedCommand) + } + [Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine() +} \ No newline at end of file diff --git a/shell/key-bindings.bash b/shell/key-bindings.bash index a4ec0ee..a1b9cfd 100644 --- a/shell/key-bindings.bash +++ b/shell/key-bindings.bash @@ -1,7 +1,6 @@ __clac__() { - clac "$READLINE_LINE" - output=$(clac -o) - eval "$output" + clac -c "$READLINE_LINE" -s bash + history -s $(clac --history) } bind -x '"\C-a": __clac__' \ No newline at end of file diff --git a/shell/key-bindings.ps1 b/shell/key-bindings.ps1 deleted file mode 100644 index c00bfad..0000000 --- a/shell/key-bindings.ps1 +++ /dev/null @@ -1,40 +0,0 @@ -Set-PSReadLineKeyHandler -Chord 'Ctrl+a' -ScriptBlock { - $command = $null - [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$command, [ref]$null) - - $oldPrompt = $function:prompt - function prompt {" `n"} - [Microsoft.PowerShell.PSConsoleReadLine]::InvokePrompt() - $prompt = $oldPrompt - - [Microsoft.PowerShell.PSConsoleReadLine]::ClearKillRing() - [Microsoft.PowerShell.PSConsoleReadLine]::BeginningOfLine() - [Microsoft.PowerShell.PSConsoleReadLine]::KillLine() - - if ($command) { - Start-Process -NoNewWindow -Wait "clac" "$command" - } else { - Start-Process -NoNewWindow -Wait "clac" - } - - $pinfo = New-Object System.Diagnostics.ProcessStartInfo - $pinfo.FileName = "clac" - $pinfo.RedirectStandardError = $true - $pinfo.RedirectStandardOutput = $true - $pinfo.UseShellExecute = $false - $pinfo.Arguments = "-o" - $p = New-Object System.Diagnostics.Process - $p.StartInfo = $pinfo - $p.Start() | Out-Null - $p.WaitForExit() - $stdout = $p.StandardOutput.ReadToEnd() - - [Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine() - - $cmd,$args = $stdout -split ' ',2 - if ($cmd.Trim() -and $args.Trim()) { - Start-Process -NoNewWindow -Wait "$cmd" "$args" - } elseif ($cmd.Trim()) { - Start-Process -NoNewWindow -Wait "$cmd" - } -} \ No newline at end of file diff --git a/src/commands/root.ts b/src/commands/root.ts index d31b4fc..eb2fb9b 100644 --- a/src/commands/root.ts +++ b/src/commands/root.ts @@ -32,5 +32,4 @@ export const action = async (options: RootCommandOptions) => { } else { process.exit(0); } - // TODO: cache executed command to add to history }; diff --git a/src/index.ts b/src/index.ts index cf030db..6a9b034 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,5 @@ +#!/usr/bin/env node + import { Command } from "commander"; import bind from "./commands/bind.js";