vim-prettier
vim-prettier copied to clipboard
Windows's support for powershell and scripts entrypoints.
Summary
Windows's support for powershell and scripts entrypoints. This way prettier can be executed over a container or wsl.
For example, the following script can be used as entrypoint to use a containerized version of prettier:
# See https://github.com/prettier/vim-prettier/pull/357
# Using .ps1 entrypoints under cmd.exe requires file association changes:
# cmd> assoc .ps1=Microsoft.PowerShellScript.1
# cmd> ftype Microsoft.PowerShellScript.1="C:\Program Files\PowerShell\7\pwsh.exe" "-File" "%1" %*
$params = @{
Name = "prettierImageName"
Value = "jauderho/prettier"
Description = "The docker image to run prettier cli"
Option = "Constant"
Scope = "Script"
}
Set-Variable @params
$encoding = '$OutputEncoding = New-Object System.Text.UTF8Encoding;'
$encoding += '[Console]::InputEncoding = $OutputEncoding;'
$encoding += '[Console]::OutputEncoding = $OutputEncoding;'
$encoding += '$PSDefaultParameterValues["*:Encoding"] = "utf8";'
Invoke-Expression -Command $encoding
# check docker
if (-not (gcm docker -ErrorAction SilentlyContinue))
{
Write-Error "Docker is not installed"
exit 1
}
# check for the image
$image = docker images --format '{{json .}}' |
? { $_ -notmatch "failed to get console mode for (stdout|stdin)"} |
ConvertFrom-Json | ? Repository -eq $prettierImageName
if (-not $image)
{
Write-Error "Docker image jauderho/prettier is not installed"
exit 2
}
# retrieve arguments (workaround because passing @args directly to docker fails)
$cmdline = [System.Environment]::CommandLine
$ScriptName = (gi $PSCommandPath).Name
if ($cmdline -match $ScriptName)
{
$filtered = $cmdline -replace ('^.*' + $ScriptName + '\s+'),'' # remove invocation
$filtered = $filtered -replace '\s*(}\s*)?\d?>.*$','' # remove redirection
$use_input = $filtered | sls '--stdin-filepath=\S*'
$filtered = $filtered -replace '--stdin-filepath=\S*','' # remove filepath
$filtered = $filtered.trim() -split '\s+'
}
else
{
$filtered = $args | ? { $_ -notmatch '--stdin-filepath' }
$use_input = $args -ne $filtered
}
# run docker or delegate to blocking script
if ($use_input -ne $null)
{
$input | docker run -i --rm $prettierImageName @filtered 2>$null
}
else
{
docker run --rm $prettierImageName @args 2>$null
}
The .vimrc will require the following config:
if has('win32')
" Lure vim into identify powershell scripts as exes
if !($PATHEXT=~'\.PS1')
let $PATHEXT.=';.PS1'
endif
let g:prettier#exec_cmd_path = "<script path>/docker-prettier.ps1"
let g:prettier#win_async_shell_cmds = (executable('pwsh') ? 'pwsh' : 'powershell') . ' -File'
endif
" enable autocommands
let g:prettier#autoformat_config_present = 1