Skip to content

Commit

Permalink
refactor(glob): use latest glob module
Browse files Browse the repository at this point in the history
Closes #4538
  • Loading branch information
chemzqm committed Aug 31, 2023
1 parent b2ae10c commit 34f5d96
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 59 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
"@types/debounce": "^3.0.0",
"@types/fb-watchman": "^2.0.0",
"@types/follow-redirects": "^1.14.1",
"@types/glob": "^8.1.0",
"@types/jest": "^27.0.3",
"@types/marked": "^4.3.1",
"@types/minimatch": "^5.1.2",
Expand Down Expand Up @@ -103,7 +102,7 @@
"fast-diff": "^1.3.0",
"fb-watchman": "^2.0.1",
"follow-redirects": "^1.15.2",
"glob": "^8.1.0",
"glob": "^10.3.4",
"http-proxy-agent": "^5.0.0",
"https-proxy-agent": "^5.0.0",
"iconv-lite": "^0.6.3",
Expand Down
6 changes: 4 additions & 2 deletions src/__tests__/core/files.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,11 @@ describe('findFiles()', () => {
it('should cancel findFiles', async () => {
let source = new CancellationTokenSource()
let p = workspace.findFiles('**/*.ts', undefined, undefined, source.token)
source.cancel()
setTimeout(() => {
source.cancel()
}, 10)
let arr = await p
expect(arr.length).toBe(0)
expect(arr).toBeDefined()
})
})

Expand Down
39 changes: 21 additions & 18 deletions src/core/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ export interface TextDocumentWillSaveEvent {
*
* *Note:* This function can only be called during event dispatch and not
* in an asynchronous manner:
*
* @param thenable A thenable that resolves to [pre-save-edits](#TextEdit).
*/
waitUntil(thenable: Thenable<TextEdit[] | any>): void
Expand Down Expand Up @@ -82,14 +81,13 @@ export interface FileWillRenameEvent {
*
* ```ts
* workspace.onWillCreateFiles(event => {
* // async, will *throw* an error
* setTimeout(() => event.waitUntil(promise));
* // async, will *throw* an error
* setTimeout(() => event.waitUntil(promise));
*
* // sync, OK
* event.waitUntil(promise);
* // sync, OK
* event.waitUntil(promise);
* })
* ```
*
* @param thenable A thenable that delays saving.
*/
waitUntil(thenable: Thenable<WorkspaceEdit | any>): void
Expand Down Expand Up @@ -133,14 +131,13 @@ export interface FileWillCreateEvent {
*
* ```ts
* workspace.onWillCreateFiles(event => {
* // async, will *throw* an error
* setTimeout(() => event.waitUntil(promise));
* // async, will *throw* an error
* setTimeout(() => event.waitUntil(promise));
*
* // sync, OK
* event.waitUntil(promise);
* // sync, OK
* event.waitUntil(promise);
* })
* ```
*
* @param thenable A thenable that delays saving.
*/
waitUntil(thenable: Thenable<WorkspaceEdit | any>): void
Expand Down Expand Up @@ -179,14 +176,13 @@ export interface FileWillDeleteEvent {
*
* ```ts
* workspace.onWillCreateFiles(event => {
* // async, will *throw* an error
* setTimeout(() => event.waitUntil(promise));
* // async, will *throw* an error
* setTimeout(() => event.waitUntil(promise));
*
* // sync, OK
* event.waitUntil(promise);
* // sync, OK
* event.waitUntil(promise);
* })
* ```
*
* @param thenable A thenable that delays saving.
*/
waitUntil(thenable: Thenable<WorkspaceEdit | any>): void
Expand Down Expand Up @@ -631,14 +627,21 @@ export default class Files {
}
let res: URI[] = []
let exceed = false
const ac = new AbortController()
if (token) {
token.onCancellationRequested(() => {
ac.abort()
})
}
for (let root of roots) {
let files = await promisify(glob)(pattern, {
let files = await glob.glob(pattern, {
signal: ac.signal,
dot: true,
cwd: root,
nodir: true,
absolute: false
})
if (token?.isCancellationRequested) return []
if (token?.isCancellationRequested) break
for (let file of files) {
if (exclude && fileMatch(root, file, exclude)) continue
res.push(URI.file(path.join(root, file)))
Expand Down
27 changes: 14 additions & 13 deletions src/util/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,36 +186,37 @@ export function resolveRoot(folder: string, subs: ReadonlyArray<string>, cwd?: s
}

export function checkFolder(dir: string, patterns: string[], token?: CancellationToken): Promise<boolean> {
return new Promise((resolve, reject) => {
return new Promise(async (resolve, reject) => {
if (isFalsyOrEmpty(patterns)) return resolve(false)
let disposable: Disposable | undefined
const ac = new AbortController()
if (token) {
disposable = token.onCancellationRequested(() => {
ac.abort()
reject(new CancellationError())
})
}

let find = false
let pattern = patterns.length == 1 ? patterns[0] : `{${patterns.join(',')}}`
let gl = new glob.Glob(pattern, {
nosort: true,
signal: ac.signal,
ignore: ['node_modules/**', '.git/**'],
dot: true,
cwd: dir,
nodir: true,
absolute: false
}, _err => {
if (disposable) disposable.dispose()
resolve(find)
})
gl.on('match', () => {
if (disposable) disposable.dispose()
find = true
resolve(true)
})
gl.on('end', () => {
if (disposable) disposable.dispose()
resolve(find)
})
try {
for await (const _file of gl) {
find = true
break
}
} catch (e) {
logger.error(`Error on glob "${pattern}"`, dir, e)
}
resolve(find)
})
}

Expand Down
Loading

0 comments on commit 34f5d96

Please sign in to comment.