@@ -7,10 +7,8 @@ Invokes cspell using dependencies defined in adjacent ./package*.json
77. PARAMETER JobType
88Maps 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
1614Location 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
3028redundant 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 ()]
4938param (
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" ),
@@ -66,102 +55,59 @@ param(
6655 [switch ] $LeavePackageInstallCache
6756)
6857
69- Set-StrictMode - Version 3.0
58+ begin {
59+ Set-StrictMode - Version 3.0
7060
71- if (! (Get-Command npm - ErrorAction SilentlyContinue)) {
72- LogError " Could not locate npm. Install NodeJS (includes npm) https://nodejs.org/en/download/"
73- exit 1
74- }
61+ if (! (Get-Command npm - ErrorAction SilentlyContinue)) {
62+ LogError " Could not locate npm. Install NodeJS (includes npm) https://nodejs.org/en/download/"
63+ exit 1
64+ }
7565
76- if (! (Test-Path $CSpellConfigPath )) {
77- LogError " Could not locate config file $CSpellConfigPath "
78- exit 1
79- }
66+ if (! (Test-Path $CSpellConfigPath )) {
67+ LogError " Could not locate config file $CSpellConfigPath "
68+ exit 1
69+ }
8070
81- # Prepare the working directory if it does not already have requirements in
82- # place.
83- if (! (Test-Path $PackageInstallCache )) {
84- New-Item - ItemType Directory - Path $PackageInstallCache | Out-Null
85- }
71+ # Prepare the working directory if it does not already have requirements in
72+ # place.
73+ if (! (Test-Path $PackageInstallCache )) {
74+ New-Item - ItemType Directory - Path $PackageInstallCache | Out-Null
75+ }
8676
87- if (! (Test-Path " $PackageInstallCache /package.json" )) {
88- Copy-Item " $PSScriptRoot /package.json" $PackageInstallCache
89- }
77+ if (! (Test-Path " $PackageInstallCache /package.json" )) {
78+ Copy-Item " $PSScriptRoot /package.json" $PackageInstallCache
79+ }
9080
91- if (! (Test-Path " $PackageInstallCache /package-lock.json" )) {
92- Copy-Item " $PSScriptRoot /package-lock.json" $PackageInstallCache
93- }
81+ if (! (Test-Path " $PackageInstallCache /package-lock.json" )) {
82+ Copy-Item " $PSScriptRoot /package-lock.json" $PackageInstallCache
83+ }
9484
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"
85+
86+ $filesToCheck = @ ()
87+ }
88+ process {
89+ $filesToCheck += $FileList
90+ }
91+ end {
92+ npm -- prefix $PackageInstallCache ci | Write-Host
93+
94+ $command = " npm --prefix $PackageInstallCache exec --no -- cspell $JobType --config $CSpellConfigPath --no-must-find-files --root $SpellCheckRoot --file-list stdin"
14695 Write-Host $command
147- $cspellOutput = npm exec `
96+ $cspellOutput = $filesToCheck | npm -- prefix $PackageInstallCache `
97+ exec `
14898 -- no `
149- -- `
99+ ' -- ' `
150100 cspell `
151101 $JobType `
152102 -- config $CSpellConfigPath `
153103 -- no- must- find-files `
154104 -- 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
105+ -- file- list stdin
161106
162- if ($deleteNotExcludedFile ) {
163- Remove-Item - Path $notExcludedFile
107+ if (! $LeavePackageInstallCache ) {
108+ Write-Host " Cleaning up package install cache at $PackageInstallCache "
109+ Remove-Item - Path $PackageInstallCache - Recurse - Force | Out-Null
164110 }
165- }
166111
167- return $cspellOutput
112+ return $cspellOutput
113+ }
0 commit comments