From e7ca5488de1f84ee86f01ad48c9475e41c103f67 Mon Sep 17 00:00:00 2001 From: Elio Struyf Date: Sun, 21 Jul 2024 15:21:39 +0200 Subject: [PATCH] Fix for finding folders with wildcards in the path #832 --- CHANGELOG.md | 1 + src/commands/Folders.ts | 26 +++++++++++++++++++++----- src/utils/index.ts | 1 + src/utils/lstatAsync.ts | 4 ++++ 4 files changed, 27 insertions(+), 5 deletions(-) create mode 100644 src/utils/lstatAsync.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 05d7b304..812b877e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ - [#827](https://github.com/estruyf/vscode-front-matter/issues/827): Fix for `frontmatter.json` file which gets created when already present in a sub-folder - [#830](https://github.com/estruyf/vscode-front-matter/issues/830): Fix for using the SEO title field setting to change the title field reference +- [#832](https://github.com/estruyf/vscode-front-matter/issues/832): Fix for finding folders with wildcards in the path ## [10.2.0] - 2024-06-12 - [Release notes](https://beta.frontmatter.codes/updates/v10.2.0) diff --git a/src/commands/Folders.ts b/src/commands/Folders.ts index bae0ee06..7ed2211b 100644 --- a/src/commands/Folders.ts +++ b/src/commands/Folders.ts @@ -29,10 +29,9 @@ import { parseWinPath } from '../helpers/parseWinPath'; import { MediaHelpers } from '../helpers/MediaHelpers'; import { MediaListener, PagesListener, SettingsListener } from '../listeners/dashboard'; import { DEFAULT_FILE_TYPES } from '../constants/DefaultFileTypes'; -import { Telemetry } from '../helpers/Telemetry'; import { glob } from 'glob'; import { mkdirAsync } from '../utils/mkdirAsync'; -import { existsAsync, isWindows } from '../utils'; +import { existsAsync, isWindows, lstatAsync } from '../utils'; import * as l10n from '@vscode/l10n'; import { LocalizationKey } from '../localization'; import { Preview } from './Preview'; @@ -828,9 +827,26 @@ export class Folders { try { pattern = isWindows() ? parseWinPath(pattern) : pattern; - const files = await glob(pattern, { ignore: '**/node_modules/**', dot: true }); - const allFolders = (files || []).map((file) => dirname(file)); - const uniqueFolders = [...new Set(allFolders)]; + const folders = await glob(pattern, { + ignore: '**/node_modules/**', + dot: true + }); + + const onlyFolders = []; + for (const folder of folders) { + try { + const stats = await lstatAsync(folder); + if (stats.isDirectory()) { + onlyFolders.push(folder); + } else { + onlyFolders.push(dirname(folder)); + } + } catch (e) { + continue; + } + } + + const uniqueFolders = [...new Set(onlyFolders)]; Logger.verbose(`Folders:findFolders:end - ${uniqueFolders.length}`); return uniqueFolders; } catch (e) { diff --git a/src/utils/index.ts b/src/utils/index.ts index 5342fb5a..b584b09d 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -12,6 +12,7 @@ export * from './getTitleField'; export * from './ignoreMsgCommand'; export * from './isWindows'; export * from './joinUrl'; +export * from './lstatAsync'; export * from './mkdirAsync'; export * from './readFileAsync'; export * from './readdirAsync'; diff --git a/src/utils/lstatAsync.ts b/src/utils/lstatAsync.ts new file mode 100644 index 00000000..2ba6c51f --- /dev/null +++ b/src/utils/lstatAsync.ts @@ -0,0 +1,4 @@ +import { promisify } from 'util'; +import { lstat as lstatCb } from 'fs'; + +export const lstatAsync = promisify(lstatCb);