|
| 1 | +[CmdletBinding()] |
| 2 | +Param ( |
| 3 | + [Parameter()] |
| 4 | + [string] $TargetBranch, |
| 5 | + |
| 6 | + [Parameter()] |
| 7 | + [string] $SourceBranch, |
| 8 | + |
| 9 | + [Parameter()] |
| 10 | + [string] $CspellConfigPath = "./.vscode/cspell.json" |
| 11 | +) |
| 12 | + |
| 13 | +$ErrorActionPreference = "Continue" |
| 14 | +. $PSScriptRoot/logging.ps1 |
| 15 | + |
| 16 | +if ((Get-Command git | Measure-Object).Count -eq 0) { |
| 17 | + LogError "Could not locate git. Install git https://git-scm.com/downloads" |
| 18 | + exit 1 |
| 19 | +} |
| 20 | + |
| 21 | +if ((Get-Command npx | Measure-Object).Count -eq 0) { |
| 22 | + LogError "Could not locate npx. Install NodeJS (includes npm and npx) https://nodejs.org/en/download/" |
| 23 | + exit 1 |
| 24 | +} |
| 25 | + |
| 26 | +if (!(Test-Path $CspellConfigPath)) { |
| 27 | + LogError "Could not locate config file $CspellConfigPath" |
| 28 | + exit 1 |
| 29 | +} |
| 30 | + |
| 31 | +# Lists names of files that were in some way changed between the |
| 32 | +# current $SourceBranch and $TargetBranch. Excludes files that were deleted to |
| 33 | +# prevent errors in Resolve-Path |
| 34 | +Write-Host "git diff --diff-filter=d --name-only $TargetBranch $SourceBranch" |
| 35 | +$changedFiles = git diff --diff-filter=d --name-only $TargetBranch $SourceBranch ` |
| 36 | + | Resolve-Path |
| 37 | + |
| 38 | +$changedFilesCount = ($changedFiles | Measure-Object).Count |
| 39 | +Write-Host "Git Detected $changedFilesCount changed file(s). Files checked by cspell may exclude files according to cspell.json" |
| 40 | + |
| 41 | +if ($changedFilesCount -eq 0) { |
| 42 | + Write-Host "No changes detected" |
| 43 | + exit 0 |
| 44 | +} |
| 45 | + |
| 46 | +$changedFilesString = $changedFiles -join ' ' |
| 47 | + |
| 48 | +Write-Host "npx cspell --config $CspellConfigPath $changedFilesString" |
| 49 | +$spellingErrors = Invoke-Expression "npx cspell --config $CspellConfigPath $changedFilesString" |
| 50 | + |
| 51 | +if ($spellingErrors) { |
| 52 | + foreach ($spellingError in $spellingErrors) { |
| 53 | + LogWarning $spellingError |
| 54 | + } |
| 55 | + LogWarning "Spelling errors detected. To correct false positives or learn about spell checking see: https://aka.ms/azsdk/engsys/spellcheck" |
| 56 | +} |
| 57 | + |
| 58 | +exit 0 |
| 59 | + |
| 60 | +<# |
| 61 | +.SYNOPSIS |
| 62 | +Uses cspell (from NPM) to check spelling of recently changed files |
| 63 | +
|
| 64 | +.DESCRIPTION |
| 65 | +This script checks files that have changed relative to a base branch (default |
| 66 | +`master`) for spelling errors. Dictionaries and spelling configurations reside |
| 67 | +in a configurable `cspell.json` location. |
| 68 | +
|
| 69 | +This script uses `npx` and assumes that NodeJS (and by extension `npm` and `npx` |
| 70 | +) are installed on the machine. If it does not detect `npx` it will warn the |
| 71 | +user and exit with an error. |
| 72 | +
|
| 73 | +The entire file is scanned, not just changed sections. Spelling errors in parts |
| 74 | +of the file not touched will still be shown. |
| 75 | +
|
| 76 | +Running this on the local machine will trigger tests |
| 77 | +
|
| 78 | +.PARAMETER TargetBranch |
| 79 | +Git ref to compare changes. This is usually the "base" (GitHub) or "target" |
| 80 | +(DevOps) branch for which a pull request would be opened. |
| 81 | +
|
| 82 | +.PARAMETER SouceBranch |
| 83 | +Git ref to use instead of changes in current repo state. Use `HEAD` here to |
| 84 | +check spelling of files that have been committed and exlcude any new files or |
| 85 | +modified files that are not committed. This is most useful in CI scenarios where |
| 86 | +builds may have modified the state of the repo. Leaving this parameter blank |
| 87 | +includes files for whom changes have not been committed. |
| 88 | +
|
| 89 | +.PARAMETER CspellConfigPath |
| 90 | +Optional location to use for cspell.json path. Default value is |
| 91 | +`./.vscode/cspell.json` |
| 92 | +
|
| 93 | +.EXAMPLE |
| 94 | +./eng/common/scripts/check-spelling-in-changed-files.ps1 -TargetBranch 'target_branch_name' |
| 95 | +
|
| 96 | +This will run spell check with changes in the current branch with respect to |
| 97 | +`target_branch_name` |
| 98 | +
|
| 99 | +#> |
0 commit comments