From 4c487401e4322de9504e85eda40fc4ee7b56bd85 Mon Sep 17 00:00:00 2001 From: Martin Strecker Date: Mon, 20 Apr 2020 21:42:29 +0100 Subject: [PATCH 1/4] add run command --- src/commands.ps1 | 1 + 1 file changed, 1 insertion(+) diff --git a/src/commands.ps1 b/src/commands.ps1 index 7fbc11c..cf741f1 100644 --- a/src/commands.ps1 +++ b/src/commands.ps1 @@ -41,6 +41,7 @@ $cmds = @( 'repo', 'restart', 'root', + 'run', 'run-script', 'search', 'set', From 2c15f004ff90816a3375d24ffb203d5e3e47a79d Mon Sep 17 00:00:00 2001 From: Martin Strecker Date: Mon, 20 Apr 2020 21:42:46 +0100 Subject: [PATCH 2/4] Add support for npm run scripts --- src/completions.ps1 | 47 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/src/completions.ps1 b/src/completions.ps1 index 198d308..07b5385 100644 --- a/src/completions.ps1 +++ b/src/completions.ps1 @@ -1,7 +1,52 @@ @('npm', 'npm.cmd') | ForEach-Object { - Register-ArgumentCompleter -Native -CommandName $_ -ScriptBlock { + Register-ArgumentCompleter -Native -CommandName $_ -ScriptBlock { param($wordToComplete, $commandAst, $cursorPosition) + # Search for a file "package.json" in the current directory or parent directories + function Get-PackageJsonPath { + [string]$packageJsonPath=$pwd; + while ($packageJsonPath.Length -gt 0 -and -not (Test-Path "$packageJsonPath\package.json")) { + # No package.json found. Look in parent directory next + $packageJsonPath=Split-Path $packageJsonPath -Parent + } + + return $packageJsonPath + } + + function Get-PackageJsonScripts { + param ( + [Parameter(Mandatory=$true, ValueFromPipeline=$true)] + [string] $packageJsonPath, + [string] $scriptFilter + ) + if ($packageJsonPath.Length -gt 0) { + $packageJson="$packageJsonPath/package.json" + $packageJsonContent = Get-Content $packageJson | ConvertFrom-Json + # Do we have a "script" section? + if ($packageJsonContent.scripts -ne $null) { + $allScripts=$packageJsonContent.scripts | Get-Member -MemberType NoteProperty | where Name -like "$scriptFilter" | select -ExpandProperty Name + $result=$allScripts | ForEach-Object { + # return the script name and the script text. The script text is passed as tooltip later. + [tuple]::Create($_, $packageJsonContent.scripts.$_) + } + + return $result + } + } + + return @(); + } + + if ($commandAst -match "npm(.cmd)?\s+run") { + $allScripts = Get-PackageJsonPath | Get-PackageJsonScripts -scriptFilter "$wordToComplete*" + $result=$allScripts | ForEach-Object { + # item1 is the script name, item2 is the script text (passed as tooltip) + [System.Management.Automation.CompletionResult]::new($_.item1, $_.item1, 'ParameterValue', $_.item2) + } + + return $result; + } + . $PSScriptRoot\commands.ps1 $cmds | Where-Object { $_ -like "$wordToComplete*" } | From e1d5348dfe357f34f87bd9ee41212e4270b89e39 Mon Sep 17 00:00:00 2001 From: Martin Strecker Date: Mon, 20 Apr 2020 22:05:52 +0100 Subject: [PATCH 3/4] Fix regex for npm run --- src/completions.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/completions.ps1 b/src/completions.ps1 index 07b5385..4a10bec 100644 --- a/src/completions.ps1 +++ b/src/completions.ps1 @@ -37,7 +37,7 @@ return @(); } - if ($commandAst -match "npm(.cmd)?\s+run") { + if ($commandAst -match "npm(.cmd)?\s+run(-script)?\s") { $allScripts = Get-PackageJsonPath | Get-PackageJsonScripts -scriptFilter "$wordToComplete*" $result=$allScripts | ForEach-Object { # item1 is the script name, item2 is the script text (passed as tooltip) From e13e94b052225e3b6e12dfe9f8ba303af0e27f81 Mon Sep 17 00:00:00 2001 From: Martin Strecker Date: Tue, 21 Apr 2020 13:35:31 +0200 Subject: [PATCH 4/4] commandAst may not be a string MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Saran Tanpituckpong ✅ <4688092+gluons@users.noreply.github.com> --- src/completions.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/completions.ps1 b/src/completions.ps1 index 4a10bec..eabf57e 100644 --- a/src/completions.ps1 +++ b/src/completions.ps1 @@ -37,7 +37,7 @@ return @(); } - if ($commandAst -match "npm(.cmd)?\s+run(-script)?\s") { + if ($commandAst.ToString() -match "npm(.cmd)?\s+run(-script)?\s*") { $allScripts = Get-PackageJsonPath | Get-PackageJsonScripts -scriptFilter "$wordToComplete*" $result=$allScripts | ForEach-Object { # item1 is the script name, item2 is the script text (passed as tooltip)