Public/New-DevFile.ps1
|
function New-DevFile { <# .SYNOPSIS Creates a file and any missing parent directories. .DESCRIPTION Creates missing parent directories and the requested file. An existing file is left untouched. .PARAMETER Path Path of the file to create. .EXAMPLE mf ./src/example.ps1 #> [CmdletBinding(SupportsShouldProcess = $true)] [Alias('mf')] [OutputType([System.IO.FileInfo])] param( [Parameter(Mandatory = $true, Position = 0)] [string]$Path ) $fullPath = if ([System.IO.Path]::IsPathRooted($Path)) { [System.IO.Path]::GetFullPath($Path) } else { [System.IO.Path]::GetFullPath((Join-Path -Path (Get-Location).Path -ChildPath $Path)) } if (Test-Path -LiteralPath $fullPath) { Write-Verbose "File already exists: $fullPath" return Get-Item -LiteralPath $fullPath } if (-not $PSCmdlet.ShouldProcess($fullPath, 'Create file')) { return } $directory = Split-Path -Path $fullPath -Parent if (-not (Test-Path -LiteralPath $directory)) { New-DtDirectory -Path $directory -Force -Confirm:$false | Out-Null } New-Item -ItemType File -Path $fullPath -Force | Out-Null Get-Item -LiteralPath $fullPath } |