Skip to content

Commit d134909

Browse files
danieljurekazure-sdk
authored andcommitted
Use stdin for cspell input
1 parent a8f9225 commit d134909

File tree

2 files changed

+21
-89
lines changed

2 files changed

+21
-89
lines changed

eng/common/scripts/check-spelling-in-changed-files.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ foreach ($file in $changedFiles) {
114114
$spellingErrors = &"$PSScriptRoot/../spelling/Invoke-Cspell.ps1" `
115115
-CspellConfigPath $CspellConfigPath `
116116
-SpellCheckRoot $SpellCheckRoot `
117-
-ScanGlobs $changedFilePaths
117+
-FileList $changedFilePaths
118118

119119
if ($spellingErrors) {
120120
$errorLoggingFunction = Get-Item 'Function:LogWarning'

eng/common/spelling/Invoke-Cspell.ps1

Lines changed: 20 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@ Invokes cspell using dependencies defined in adjacent ./package*.json
77
.PARAMETER JobType
88
Maps to cspell command (e.g. `lint`, `trace`, etc.). Default is `lint`
99
10-
.PARAMETER ScanGlobs
11-
List of glob expressions to be scanned. This list is not constrained by
12-
npx/cmd's upper limit on command line length as the globs are inserted into the
13-
cspell config's `files` property.
10+
.PARAMETER FileList
11+
List of file paths to be scanned. This is piped into cspell via stdin.
1412
1513
.PARAMETER CSpellConfigPath
1614
Location of cspell.json file to use when scanning. Defaults to
@@ -30,28 +28,19 @@ calls to Invoke-Cspell.ps1 to prevent creating multiple working directories and
3028
redundant calls `npm ci`.
3129
3230
.EXAMPLE
33-
./eng/common/scripts/Invoke-Cspell.ps1 -ScanGlobs 'sdk/*/*/PublicAPI/**/*.md'
34-
35-
This will run spell check with the given globs
36-
37-
.EXAMPLE
38-
./eng/common/scripts/Invoke-Cspell.ps1 -ScanGlobs @('sdk/storage/**', 'sdk/keyvault/**')
39-
40-
This will run spell check against multiple globs
31+
./eng/common/scripts/Invoke-Cspell.ps1 -FileList @('./README.md', 'file2.txt')
4132
4233
.EXAMPLE
43-
./eng/common/scripts/Invoke-Cspell.ps1 -ScanGlobs './README.md'
44-
45-
This will run spell check against a single file
34+
git diff main --name-only | ./eng/common/spelling/Invoke-Cspell.ps1
4635
4736
#>
4837
[CmdletBinding()]
4938
param(
5039
[Parameter()]
5140
[string] $JobType = 'lint',
5241

53-
[Parameter()]
54-
[array]$ScanGlobs = '**',
42+
[Parameter(ValueFromPipeline)]
43+
[array]$FileList,
5544

5645
[Parameter()]
5746
[string] $CSpellConfigPath = (Resolve-Path "$PSScriptRoot/../../../.vscode/cspell.json"),
@@ -92,76 +81,19 @@ if (!(Test-Path "$PackageInstallCache/package-lock.json")) {
9281
Copy-Item "$PSScriptRoot/package-lock.json" $PackageInstallCache
9382
}
9483

95-
$deleteNotExcludedFile = $false
96-
$notExcludedFile = ""
97-
if (Test-Path "$SpellCheckRoot/LICENSE") {
98-
$notExcludedFile = "$SpellCheckRoot/LICENSE"
99-
} elseif (Test-Path "$SpellCheckRoot/LICENSE.txt") {
100-
$notExcludedFile = "$SpellCheckRoot/LICENSE.txt"
101-
} else {
102-
# If there is no LICENSE file, fall back to creating a temporary file
103-
# The "files" list must always contain a file which exists, is not empty, and is
104-
# not excluded in ignorePaths. In this case it will be a file with the contents
105-
# "1" (no spelling errors will be detected)
106-
$notExcludedFile = Join-Path $SpellCheckRoot ([System.IO.Path]::GetRandomFileName())
107-
"1" >> $notExcludedFile
108-
$deleteNotExcludedFile = $true
109-
}
110-
$ScanGlobs += $notExcludedFile
111-
112-
$cspellConfigContent = Get-Content $CSpellConfigPath -Raw
113-
$cspellConfig = ConvertFrom-Json $cspellConfigContent
114-
115-
# If the config has no "files" property this adds it. If the config has a
116-
# "files" property this sets the value, overwriting the existing value. In this
117-
# case, spell checking is only intended to check files from $ScanGlobs so
118-
# preexisting entries in "files" will be overwritten.
119-
Add-Member `
120-
-MemberType NoteProperty `
121-
-InputObject $cspellConfig `
122-
-Name "files" `
123-
-Value $ScanGlobs `
124-
-Force
125-
126-
# Set the temporary config file with the mutated configuration. The temporary
127-
# location is used to configure the command and the original file remains
128-
# unchanged.
129-
Write-Host "Setting config in: $CSpellConfigPath"
130-
Set-Content `
131-
-Path $CSpellConfigPath `
132-
-Value (ConvertTo-Json $cspellConfig -Depth 100)
133-
134-
# Before changing the run location, resolve paths specified in parameters
135-
$CSpellConfigPath = Resolve-Path $CSpellConfigPath
136-
$SpellCheckRoot = Resolve-Path $SpellCheckRoot
137-
138-
$originalLocation = Get-Location
139-
140-
try {
141-
Set-Location $PackageInstallCache
142-
npm ci | Write-Host
143-
144-
# Use the mutated configuration file when calling cspell
145-
$command = "npm exec --no -- cspell $JobType --config $CSpellConfigPath --no-must-find-files --root $SpellCheckRoot --relative"
146-
Write-Host $command
147-
$cspellOutput = npm exec `
148-
--no `
149-
-- `
150-
cspell `
151-
$JobType `
152-
--config $CSpellConfigPath `
153-
--no-must-find-files `
154-
--root $SpellCheckRoot `
155-
--relative
156-
} finally {
157-
Set-Location $originalLocation
158-
159-
Write-Host "cspell run complete, restoring original configuration and removing temp file."
160-
Set-Content -Path $CSpellConfigPath -Value $cspellConfigContent -NoNewLine
161-
162-
if ($deleteNotExcludedFile) {
163-
Remove-Item -Path $notExcludedFile
164-
}
165-
}
84+
npm --prefix $PackageInstallCache ci | Write-Host
85+
86+
$command = "npm --prefix $PackageInstallCache exec --no -- cspell $JobType --config $CSpellConfigPath --no-must-find-files --root $SpellCheckRoot --file-list stdin"
87+
Write-Host $command
88+
$cspellOutput = $FileList | npm --prefix $PackageInstallCache `
89+
exec `
90+
--no `
91+
-- `
92+
cspell `
93+
$JobType `
94+
--config $CSpellConfigPath `
95+
--no-must-find-files `
96+
--root $SpellCheckRoot `
97+
--file-list stdin
16698

16799
return $cspellOutput

0 commit comments

Comments
 (0)