cspell-lib
Advanced tools
Comparing version 8.17.2 to 8.17.3
import type { VFileSystem } from '../../../fileSystem.js'; | ||
export declare class ConfigSearch { | ||
readonly searchPlaces: readonly string[]; | ||
#private; | ||
/** | ||
* @param searchPlaces - The list of file names to search for. | ||
* @param allowedExtensionsByProtocol - Map of allowed extensions by protocol, '*' is used to match all protocols. | ||
* @param fs - The file system to use. | ||
*/ | ||
constructor(searchPlaces: readonly string[], allowedExtensionsByProtocol: Map<string, readonly string[]>, fs: VFileSystem); | ||
searchForConfig(searchFromURL: URL): Promise<URL | undefined>; | ||
clearCache(): void; | ||
} | ||
/** | ||
* A Scanner that searches for a config file in a directory. It caches the results to speed up future requests. | ||
*/ | ||
export declare class DirConfigScanner { | ||
#private; | ||
readonly allowedExtensionsByProtocol: Map<string, readonly string[]>; | ||
private fs; | ||
private searchCache; | ||
private searchDirCache; | ||
private searchPlacesByProtocol; | ||
/** | ||
@@ -15,11 +26,11 @@ * @param searchPlaces - The list of file names to search for. | ||
constructor(searchPlaces: readonly string[], allowedExtensionsByProtocol: Map<string, readonly string[]>, fs: VFileSystem); | ||
searchForConfig(searchFromURL: URL): Promise<URL | undefined>; | ||
clearCache(): void; | ||
private findUpConfigPath; | ||
private hasConfig; | ||
private createHasFileDirSearch; | ||
private readDir; | ||
private createHasFileStatCheck; | ||
private hasConfigDir; | ||
/** | ||
* | ||
* @param dir - the directory to search for a config file. | ||
* @param visited - a callback to be called for each directory visited. | ||
* @returns A promise that resolves to the url of the config file or `undefined`. | ||
*/ | ||
scanDirForConfigFile(dir: URL): Promise<URL | undefined>; | ||
} | ||
//# sourceMappingURL=configSearch.d.ts.map |
@@ -6,9 +6,11 @@ import { extname } from 'node:path/posix'; | ||
export class ConfigSearch { | ||
searchPlaces; | ||
allowedExtensionsByProtocol; | ||
fs; | ||
searchCache = new Map(); | ||
searchDirCache = new Map(); | ||
searchPlacesByProtocol; | ||
/** | ||
* Cache of search results. | ||
*/ | ||
#searchCache = new Map(); | ||
/** | ||
* The scanner to use to search for config files. | ||
*/ | ||
#scanner; | ||
/** | ||
* @param searchPlaces - The list of file names to search for. | ||
@@ -19,61 +21,83 @@ * @param allowedExtensionsByProtocol - Map of allowed extensions by protocol, '*' is used to match all protocols. | ||
constructor(searchPlaces, allowedExtensionsByProtocol, fs) { | ||
this.searchPlaces = searchPlaces; | ||
this.allowedExtensionsByProtocol = allowedExtensionsByProtocol; | ||
this.fs = fs; | ||
this.searchPlacesByProtocol = setupSearchPlacesByProtocol(searchPlaces, allowedExtensionsByProtocol); | ||
this.searchPlaces = this.searchPlacesByProtocol.get('*') || searchPlaces; | ||
this.#scanner = new DirConfigScanner(searchPlaces, allowedExtensionsByProtocol, fs); | ||
} | ||
searchForConfig(searchFromURL) { | ||
const dirUrl = new URL('.', searchFromURL); | ||
const searchHref = dirUrl.href; | ||
const searchCache = this.searchCache; | ||
const cached = searchCache.get(searchHref); | ||
const dirUrl = searchFromURL.pathname.endsWith('/') ? searchFromURL : new URL('.', searchFromURL); | ||
return this.#findUp(dirUrl); | ||
} | ||
clearCache() { | ||
this.#searchCache.clear(); | ||
this.#scanner.clearCache(); | ||
} | ||
#findUp(fromDir) { | ||
const searchDirCache = this.#searchCache; | ||
const cached = searchDirCache.get(fromDir.href); | ||
if (cached) { | ||
return cached; | ||
} | ||
const toPatchCache = []; | ||
const pFoundUrl = this.findUpConfigPath(dirUrl, storeVisit); | ||
this.searchCache.set(searchHref, pFoundUrl); | ||
const searchDirCache = this.searchDirCache; | ||
const patch = async () => { | ||
try { | ||
await pFoundUrl; | ||
for (const dir of toPatchCache) { | ||
searchDirCache.set(dir.href, searchDirCache.get(dir.href) || pFoundUrl); | ||
searchCache.set(dir.href, searchCache.get(dir.href) || pFoundUrl); | ||
} | ||
const result = searchCache.get(searchHref) || pFoundUrl; | ||
searchCache.set(searchHref, result); | ||
const visited = []; | ||
let result = undefined; | ||
const predicate = (dir) => { | ||
visit(dir); | ||
return this.#scanner.scanDirForConfigFile(dir); | ||
}; | ||
result = findUpFromUrl(predicate, fromDir, { type: 'file' }); | ||
searchDirCache.set(fromDir.href, result); | ||
visited.forEach((dir) => searchDirCache.set(dir.href, result)); | ||
return result; | ||
/** | ||
* Record directories that are visited while walking up the directory tree. | ||
* This will help speed up future searches. | ||
* @param dir - the directory that was visited. | ||
*/ | ||
function visit(dir) { | ||
if (!result) { | ||
visited.push(dir); | ||
return; | ||
} | ||
catch { | ||
// ignore | ||
} | ||
}; | ||
patch(); | ||
return pFoundUrl; | ||
function storeVisit(dir) { | ||
toPatchCache.push(dir); | ||
searchDirCache.set(dir.href, searchDirCache.get(dir.href) || result); | ||
} | ||
} | ||
} | ||
/** | ||
* A Scanner that searches for a config file in a directory. It caches the results to speed up future requests. | ||
*/ | ||
export class DirConfigScanner { | ||
allowedExtensionsByProtocol; | ||
fs; | ||
#searchDirCache = new Map(); | ||
#searchPlacesByProtocol; | ||
#searchPlaces; | ||
/** | ||
* @param searchPlaces - The list of file names to search for. | ||
* @param allowedExtensionsByProtocol - Map of allowed extensions by protocol, '*' is used to match all protocols. | ||
* @param fs - The file system to use. | ||
*/ | ||
constructor(searchPlaces, allowedExtensionsByProtocol, fs) { | ||
this.allowedExtensionsByProtocol = allowedExtensionsByProtocol; | ||
this.fs = fs; | ||
this.#searchPlacesByProtocol = setupSearchPlacesByProtocol(searchPlaces, allowedExtensionsByProtocol); | ||
this.#searchPlaces = this.#searchPlacesByProtocol.get('*') || searchPlaces; | ||
} | ||
clearCache() { | ||
this.searchCache.clear(); | ||
this.searchDirCache.clear(); | ||
this.#searchDirCache.clear(); | ||
} | ||
findUpConfigPath(cwd, visit) { | ||
const searchDirCache = this.searchDirCache; | ||
const cached = searchDirCache.get(cwd.href); | ||
if (cached) | ||
/** | ||
* | ||
* @param dir - the directory to search for a config file. | ||
* @param visited - a callback to be called for each directory visited. | ||
* @returns A promise that resolves to the url of the config file or `undefined`. | ||
*/ | ||
scanDirForConfigFile(dir) { | ||
const searchDirCache = this.#searchDirCache; | ||
const href = dir.href; | ||
const cached = searchDirCache.get(href); | ||
if (cached) { | ||
return cached; | ||
return findUpFromUrl((dir) => this.hasConfig(dir, visit), cwd, { type: 'file' }); | ||
} | ||
hasConfig(dir, visited) { | ||
const cached = this.searchDirCache.get(dir.href); | ||
if (cached) | ||
return cached; | ||
visited(dir); | ||
const result = this.hasConfigDir(dir); | ||
this.searchDirCache.set(dir.href, result); | ||
} | ||
const result = this.#scanDirForConfig(dir); | ||
searchDirCache.set(href, result); | ||
return result; | ||
} | ||
createHasFileDirSearch() { | ||
#createHasFileDirSearch() { | ||
const dirInfoCache = createAutoResolveCache(); | ||
@@ -93,3 +117,3 @@ const hasFile = async (filename) => { | ||
const dirUrlHref = dir.href; | ||
const dirInfo = await dirInfoCache.get(dirUrlHref, async () => await this.readDir(dir)); | ||
const dirInfo = await dirInfoCache.get(dirUrlHref, async () => await this.#readDir(dir)); | ||
const name = urlBasename(filename); | ||
@@ -101,3 +125,3 @@ const found = dirInfo.get(name); | ||
} | ||
async readDir(dir) { | ||
async #readDir(dir) { | ||
try { | ||
@@ -111,3 +135,3 @@ const dirInfo = await this.fs.readDirectory(dir); | ||
} | ||
createHasFileStatCheck() { | ||
#createHasFileStatCheck() { | ||
const hasFile = async (filename) => { | ||
@@ -119,7 +143,12 @@ const stat = await this.fs.stat(filename).catch(() => undefined); | ||
} | ||
async hasConfigDir(dir) { | ||
/** | ||
* Scan the directory for the first matching config file. | ||
* @param dir - url of the directory to scan. | ||
* @returns A promise that resolves to the url of the config file or `undefined`. | ||
*/ | ||
async #scanDirForConfig(dir) { | ||
const hasFile = this.fs.getCapabilities(dir).readDirectory | ||
? this.createHasFileDirSearch() | ||
: this.createHasFileStatCheck(); | ||
const searchPlaces = this.searchPlacesByProtocol.get(dir.protocol) || this.searchPlaces; | ||
? this.#createHasFileDirSearch() | ||
: this.#createHasFileStatCheck(); | ||
const searchPlaces = this.#searchPlacesByProtocol.get(dir.protocol) || this.#searchPlaces; | ||
for (const searchPlace of searchPlaces) { | ||
@@ -126,0 +155,0 @@ const file = new URL(searchPlace, dir); |
@@ -9,5 +9,5 @@ import type { VFileSystem } from '../fileSystem.js'; | ||
} | ||
type FindUpPredicate = (dir: URL) => URL | undefined | Promise<URL | undefined>; | ||
export type FindUpPredicate = (dir: URL) => URL | undefined | Promise<URL | undefined>; | ||
export declare function findUpFromUrl(name: string | string[] | FindUpPredicate, from: URL, options?: FindUpURLOptions): Promise<URL | undefined>; | ||
export {}; | ||
//# sourceMappingURL=findUpFromUrl.d.ts.map |
@@ -7,3 +7,3 @@ { | ||
}, | ||
"version": "8.17.2", | ||
"version": "8.17.3", | ||
"description": "A library of useful functions used across various cspell tools.", | ||
@@ -68,18 +68,18 @@ "type": "module", | ||
"dependencies": { | ||
"@cspell/cspell-bundled-dicts": "8.17.2", | ||
"@cspell/cspell-pipe": "8.17.2", | ||
"@cspell/cspell-resolver": "8.17.2", | ||
"@cspell/cspell-types": "8.17.2", | ||
"@cspell/dynamic-import": "8.17.2", | ||
"@cspell/filetypes": "8.17.2", | ||
"@cspell/strong-weak-map": "8.17.2", | ||
"@cspell/url": "8.17.2", | ||
"@cspell/cspell-bundled-dicts": "8.17.3", | ||
"@cspell/cspell-pipe": "8.17.3", | ||
"@cspell/cspell-resolver": "8.17.3", | ||
"@cspell/cspell-types": "8.17.3", | ||
"@cspell/dynamic-import": "8.17.3", | ||
"@cspell/filetypes": "8.17.3", | ||
"@cspell/strong-weak-map": "8.17.3", | ||
"@cspell/url": "8.17.3", | ||
"clear-module": "^4.1.2", | ||
"comment-json": "^4.2.5", | ||
"cspell-config-lib": "8.17.2", | ||
"cspell-dictionary": "8.17.2", | ||
"cspell-glob": "8.17.2", | ||
"cspell-grammar": "8.17.2", | ||
"cspell-io": "8.17.2", | ||
"cspell-trie-lib": "8.17.2", | ||
"cspell-config-lib": "8.17.3", | ||
"cspell-dictionary": "8.17.3", | ||
"cspell-glob": "8.17.3", | ||
"cspell-grammar": "8.17.3", | ||
"cspell-io": "8.17.3", | ||
"cspell-trie-lib": "8.17.3", | ||
"env-paths": "^3.0.0", | ||
@@ -105,3 +105,3 @@ "fast-equals": "^5.2.2", | ||
"@cspell/dict-nl-nl": "^2.3.3", | ||
"@cspell/dict-python": "^4.2.14", | ||
"@cspell/dict-python": "^4.2.15", | ||
"@types/configstore": "^6.0.2", | ||
@@ -114,3 +114,3 @@ "configstore": "^7.0.0", | ||
}, | ||
"gitHead": "160c982e8d1e3b4951acb6fc003d013f3b0597e0" | ||
"gitHead": "9a0d2b4584112b33d137faa98e9931ad4e7b6050" | ||
} |
453423
10973
+ Added@cspell/cspell-bundled-dicts@8.17.3(transitive)
+ Added@cspell/cspell-pipe@8.17.3(transitive)
+ Added@cspell/cspell-resolver@8.17.3(transitive)
+ Added@cspell/cspell-service-bus@8.17.3(transitive)
+ Added@cspell/cspell-types@8.17.3(transitive)
+ Added@cspell/dynamic-import@8.17.3(transitive)
+ Added@cspell/filetypes@8.17.3(transitive)
+ Added@cspell/strong-weak-map@8.17.3(transitive)
+ Added@cspell/url@8.17.3(transitive)
+ Addedcspell-config-lib@8.17.3(transitive)
+ Addedcspell-dictionary@8.17.3(transitive)
+ Addedcspell-glob@8.17.3(transitive)
+ Addedcspell-grammar@8.17.3(transitive)
+ Addedcspell-io@8.17.3(transitive)
+ Addedcspell-trie-lib@8.17.3(transitive)
- Removed@cspell/cspell-bundled-dicts@8.17.2(transitive)
- Removed@cspell/cspell-pipe@8.17.2(transitive)
- Removed@cspell/cspell-resolver@8.17.2(transitive)
- Removed@cspell/cspell-service-bus@8.17.2(transitive)
- Removed@cspell/cspell-types@8.17.2(transitive)
- Removed@cspell/dynamic-import@8.17.2(transitive)
- Removed@cspell/filetypes@8.17.2(transitive)
- Removed@cspell/strong-weak-map@8.17.2(transitive)
- Removed@cspell/url@8.17.2(transitive)
- Removedcspell-config-lib@8.17.2(transitive)
- Removedcspell-dictionary@8.17.2(transitive)
- Removedcspell-glob@8.17.2(transitive)
- Removedcspell-grammar@8.17.2(transitive)
- Removedcspell-io@8.17.2(transitive)
- Removedcspell-trie-lib@8.17.2(transitive)
Updated@cspell/cspell-pipe@8.17.3
Updated@cspell/cspell-types@8.17.3
Updated@cspell/filetypes@8.17.3
Updated@cspell/url@8.17.3
Updatedcspell-config-lib@8.17.3
Updatedcspell-dictionary@8.17.3
Updatedcspell-glob@8.17.3
Updatedcspell-grammar@8.17.3
Updatedcspell-io@8.17.3
Updatedcspell-trie-lib@8.17.3