Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion eng/common/scripts/check-spelling-in-changed-files.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ foreach ($file in $changedFiles) {
$spellingErrors = &"$PSScriptRoot/../spelling/Invoke-Cspell.ps1" `
-CspellConfigPath $CspellConfigPath `
-SpellCheckRoot $SpellCheckRoot `
-ScanGlobs $changedFilePaths
-FileList $changedFilePaths

if ($spellingErrors) {
$errorLoggingFunction = Get-Item 'Function:LogWarning'
Expand Down
146 changes: 46 additions & 100 deletions eng/common/spelling/Invoke-Cspell.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ Invokes cspell using dependencies defined in adjacent ./package*.json
.PARAMETER JobType
Maps to cspell command (e.g. `lint`, `trace`, etc.). Default is `lint`

.PARAMETER ScanGlobs
List of glob expressions to be scanned. This list is not constrained by
npx/cmd's upper limit on command line length as the globs are inserted into the
cspell config's `files` property.
.PARAMETER FileList
List of file paths to be scanned. This is piped into cspell via stdin.

.PARAMETER CSpellConfigPath
Location of cspell.json file to use when scanning. Defaults to
Expand All @@ -30,28 +28,19 @@ calls to Invoke-Cspell.ps1 to prevent creating multiple working directories and
redundant calls `npm ci`.

.EXAMPLE
./eng/common/scripts/Invoke-Cspell.ps1 -ScanGlobs 'sdk/*/*/PublicAPI/**/*.md'

This will run spell check with the given globs

.EXAMPLE
./eng/common/scripts/Invoke-Cspell.ps1 -ScanGlobs @('sdk/storage/**', 'sdk/keyvault/**')

This will run spell check against multiple globs
./eng/common/scripts/Invoke-Cspell.ps1 -FileList @('./README.md', 'file2.txt')

.EXAMPLE
./eng/common/scripts/Invoke-Cspell.ps1 -ScanGlobs './README.md'

This will run spell check against a single file
git diff main --name-only | ./eng/common/spelling/Invoke-Cspell.ps1

#>
[CmdletBinding()]
param(
[Parameter()]
[string] $JobType = 'lint',

[Parameter()]
[array]$ScanGlobs = '**',
[Parameter(ValueFromPipeline)]
[array]$FileList,

[Parameter()]
[string] $CSpellConfigPath = (Resolve-Path "$PSScriptRoot/../../../.vscode/cspell.json"),
Expand All @@ -66,102 +55,59 @@ param(
[switch] $LeavePackageInstallCache
)

Set-StrictMode -Version 3.0
begin {
Set-StrictMode -Version 3.0

if (!(Get-Command npm -ErrorAction SilentlyContinue)) {
LogError "Could not locate npm. Install NodeJS (includes npm) https://nodejs.org/en/download/"
exit 1
}
if (!(Get-Command npm -ErrorAction SilentlyContinue)) {
LogError "Could not locate npm. Install NodeJS (includes npm) https://nodejs.org/en/download/"
exit 1
}

if (!(Test-Path $CSpellConfigPath)) {
LogError "Could not locate config file $CSpellConfigPath"
exit 1
}
if (!(Test-Path $CSpellConfigPath)) {
LogError "Could not locate config file $CSpellConfigPath"
exit 1
}

# Prepare the working directory if it does not already have requirements in
# place.
if (!(Test-Path $PackageInstallCache)) {
New-Item -ItemType Directory -Path $PackageInstallCache | Out-Null
}
# Prepare the working directory if it does not already have requirements in
# place.
if (!(Test-Path $PackageInstallCache)) {
New-Item -ItemType Directory -Path $PackageInstallCache | Out-Null
}

if (!(Test-Path "$PackageInstallCache/package.json")) {
Copy-Item "$PSScriptRoot/package.json" $PackageInstallCache
}
if (!(Test-Path "$PackageInstallCache/package.json")) {
Copy-Item "$PSScriptRoot/package.json" $PackageInstallCache
}

if (!(Test-Path "$PackageInstallCache/package-lock.json")) {
Copy-Item "$PSScriptRoot/package-lock.json" $PackageInstallCache
}
if (!(Test-Path "$PackageInstallCache/package-lock.json")) {
Copy-Item "$PSScriptRoot/package-lock.json" $PackageInstallCache
}

$deleteNotExcludedFile = $false
$notExcludedFile = ""
if (Test-Path "$SpellCheckRoot/LICENSE") {
$notExcludedFile = "$SpellCheckRoot/LICENSE"
} elseif (Test-Path "$SpellCheckRoot/LICENSE.txt") {
$notExcludedFile = "$SpellCheckRoot/LICENSE.txt"
} else {
# If there is no LICENSE file, fall back to creating a temporary file
# The "files" list must always contain a file which exists, is not empty, and is
# not excluded in ignorePaths. In this case it will be a file with the contents
# "1" (no spelling errors will be detected)
$notExcludedFile = Join-Path $SpellCheckRoot ([System.IO.Path]::GetRandomFileName())
"1" >> $notExcludedFile
$deleteNotExcludedFile = $true
}
$ScanGlobs += $notExcludedFile

$cspellConfigContent = Get-Content $CSpellConfigPath -Raw
$cspellConfig = ConvertFrom-Json $cspellConfigContent

# If the config has no "files" property this adds it. If the config has a
# "files" property this sets the value, overwriting the existing value. In this
# case, spell checking is only intended to check files from $ScanGlobs so
# preexisting entries in "files" will be overwritten.
Add-Member `
-MemberType NoteProperty `
-InputObject $cspellConfig `
-Name "files" `
-Value $ScanGlobs `
-Force

# Set the temporary config file with the mutated configuration. The temporary
# location is used to configure the command and the original file remains
# unchanged.
Write-Host "Setting config in: $CSpellConfigPath"
Set-Content `
-Path $CSpellConfigPath `
-Value (ConvertTo-Json $cspellConfig -Depth 100)

# Before changing the run location, resolve paths specified in parameters
$CSpellConfigPath = Resolve-Path $CSpellConfigPath
$SpellCheckRoot = Resolve-Path $SpellCheckRoot

$originalLocation = Get-Location

try {
Set-Location $PackageInstallCache
npm ci | Write-Host

# Use the mutated configuration file when calling cspell
$command = "npm exec --no -- cspell $JobType --config $CSpellConfigPath --no-must-find-files --root $SpellCheckRoot --relative"

$filesToCheck = @()
}
process {
$filesToCheck += $FileList
}
end {
npm --prefix $PackageInstallCache ci | Write-Host

$command = "npm --prefix $PackageInstallCache exec --no -- cspell $JobType --config $CSpellConfigPath --no-must-find-files --root $SpellCheckRoot --file-list stdin"
Write-Host $command
$cspellOutput = npm exec `
$cspellOutput = $filesToCheck | npm --prefix $PackageInstallCache `
exec `
--no `
-- `
'--' `
cspell `
$JobType `
--config $CSpellConfigPath `
--no-must-find-files `
--root $SpellCheckRoot `
--relative
} finally {
Set-Location $originalLocation

Write-Host "cspell run complete, restoring original configuration and removing temp file."
Set-Content -Path $CSpellConfigPath -Value $cspellConfigContent -NoNewLine
--file-list stdin

if ($deleteNotExcludedFile) {
Remove-Item -Path $notExcludedFile
if (!$LeavePackageInstallCache) {
Write-Host "Cleaning up package install cache at $PackageInstallCache"
Remove-Item -Path $PackageInstallCache -Recurse -Force | Out-Null
}
}

return $cspellOutput
return $cspellOutput
}
Loading
Loading