Public/mni.ps1
|
function mni { [CmdletBinding()] param( [Parameter(Mandatory = $true)] [string]$Path, [string]$EditorCommand = 'code', [switch]$NoOpen ) $fullPath = Resolve-Path -LiteralPath $Path -ErrorAction SilentlyContinue if (-not $fullPath) { if ([System.IO.Path]::IsPathRooted($Path)) { $fullPath = [System.IO.Path]::GetFullPath($Path) } else { $fullPath = [System.IO.Path]::GetFullPath( (Join-Path -Path (Get-Location).Path -ChildPath $Path) ) } } else { $fullPath = $fullPath.Path } $directory = Split-Path -Path $fullPath -Parent if (-not (Test-Path -LiteralPath $directory)) { New-Item -ItemType Directory -Path $directory -Force | Out-Null } if (-not (Test-Path -LiteralPath $fullPath)) { New-Item -ItemType File -Path $fullPath -Force | Out-Null } $file = Get-Item -LiteralPath $fullPath if (-not $NoOpen) { $editor = Get-Command -Name $EditorCommand -CommandType Application -ErrorAction SilentlyContinue if ($editor) { & $editor.Source $file.FullName } else { Write-Warning "File created, but editor command '$EditorCommand' was not found." } } return $file } |