@@ -11,11 +11,12 @@ import { fileToDocument, isBinaryFile as isUriBinaryFile } from 'cspell-lib';
11
11
import getStdin from 'get-stdin' ;
12
12
13
13
import { asyncAwait , asyncFlatten , asyncMap , asyncPipe , mergeAsyncIterables } from './async.js' ;
14
- import { FileProtocol , STDIN , STDINProtocol , UTF8 } from './constants.js' ;
14
+ import { FileUrlPrefix , STDIN , STDINProtocol , STDINUrlPrefix , UTF8 } from './constants.js' ;
15
15
import { IOError , toApplicationError , toError } from './errors.js' ;
16
16
import type { GlobOptions } from './glob.js' ;
17
17
import { globP } from './glob.js' ;
18
18
import { readStdin } from './stdin.js' ;
19
+ import { isStdinUrl , resolveStdinUrl } from './stdinUrl.js' ;
19
20
import { clean } from './util.js' ;
20
21
21
22
export interface ConfigInfo {
@@ -67,7 +68,7 @@ export function fileInfoToDocument(
67
68
languageId = languageId || undefined ;
68
69
locale = locale || undefined ;
69
70
70
- const uri = filenameToUrlString ( filename ) ;
71
+ const uri = filenameToUrl ( filename ) ;
71
72
72
73
if ( uri . href . startsWith ( STDINProtocol ) ) {
73
74
return clean ( {
@@ -81,18 +82,17 @@ export function fileInfoToDocument(
81
82
return fileToDocument ( uri . href , text , languageId , locale ) ;
82
83
}
83
84
84
- export function filenameToUrlString ( filename : string , cwd = '.' ) : URL {
85
+ export function filenameToUrl ( filename : string , cwd = '.' ) : URL {
85
86
const cwdURL = toFileDirURL ( cwd ) ;
86
87
if ( filename === STDIN ) return new URL ( 'stdin:///' ) ;
87
- if ( filename . startsWith ( STDINProtocol ) ) {
88
- const filePath = filename . slice ( STDINProtocol . length ) ;
89
- return toFileURL ( filePath , cwdURL ) ;
88
+ if ( isStdinUrl ( filename ) ) {
89
+ return new URL ( resolveStdinUrl ( filename , cwd ) ) ;
90
90
}
91
91
return toFileURL ( filename , cwdURL ) ;
92
92
}
93
93
94
94
export function filenameToUri ( filename : string , cwd ?: string ) : URL {
95
- return toURL ( filenameToUrlString ( filename , cwd ) ) ;
95
+ return toURL ( filenameToUrl ( filename , cwd ) ) ;
96
96
}
97
97
98
98
export function isBinaryFile ( filename : string , cwd ?: string ) : boolean {
@@ -107,15 +107,15 @@ export interface ReadFileInfoResult extends FileInfo {
107
107
108
108
export function resolveFilename ( filename : string , cwd ?: string ) : string {
109
109
cwd = cwd || process . cwd ( ) ;
110
- if ( filename === STDIN ) return STDINProtocol ;
111
- if ( filename . startsWith ( FileProtocol ) ) {
112
- const url = new URL ( filename . slice ( FileProtocol . length ) , pathToFileURL ( cwd + path . sep ) ) ;
110
+ if ( filename === STDIN ) return STDINUrlPrefix ;
111
+ if ( filename . startsWith ( FileUrlPrefix ) ) {
112
+ const url = new URL ( filename . slice ( FileUrlPrefix . length ) , pathToFileURL ( cwd + path . sep ) ) ;
113
113
return fileURLToPath ( url ) ;
114
114
}
115
- const scheme = filename . startsWith ( STDINProtocol ) ? STDINProtocol : '' ;
116
- const pathname = filename . slice ( scheme . length ) ;
117
-
118
- return scheme + path . resolve ( cwd , pathname ) ;
115
+ if ( isStdinUrl ( filename ) ) {
116
+ return resolveStdinUrl ( filename , cwd ) ;
117
+ }
118
+ return path . resolve ( cwd , filename ) ;
119
119
}
120
120
121
121
export function readFileInfo (
@@ -149,9 +149,7 @@ export function readFile(filename: string, encoding: BufferEncoding = UTF8): Pro
149
149
export async function findFiles ( globPatterns : string [ ] , options : GlobOptions ) : Promise < string [ ] > {
150
150
const stdin : string [ ] = [ ] ;
151
151
const globPats = globPatterns . filter ( ( filename ) =>
152
- filename !== STDIN && ! filename . startsWith ( STDINProtocol ) && ! filename . startsWith ( FileProtocol )
153
- ? true
154
- : ( stdin . push ( filename ) , false ) ,
152
+ ! isStdin ( filename ) && ! filename . startsWith ( FileUrlPrefix ) ? true : ( stdin . push ( filename ) , false ) ,
155
153
) ;
156
154
const globResults = globPats . length ? await globP ( globPats , options ) : [ ] ;
157
155
const cwd = options . cwd || process . cwd ( ) ;
@@ -205,7 +203,12 @@ export async function readFileListFile(listFile: string): Promise<string[]> {
205
203
}
206
204
}
207
205
206
+ function isStdin ( filename : string ) : boolean {
207
+ return filename === STDIN || isStdinUrl ( filename ) ;
208
+ }
209
+
208
210
export async function isFile ( filename : string ) : Promise < boolean > {
211
+ if ( isStdin ( filename ) ) return true ;
209
212
try {
210
213
const stat = await fsp . stat ( filename ) ;
211
214
return stat . isFile ( ) ;
0 commit comments