diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b24fde2..80b61165 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### 🎨 Enhancements - [#211](https://github.com/estruyf/vscode-front-matter/issues/211): Replace text selection on media inserts +- [#212](https://github.com/estruyf/vscode-front-matter/issues/212): Create folder watchers for content folders. When new content gets created, the dashboard updates. - [#213](https://github.com/estruyf/vscode-front-matter/issues/213): New media folder overview design ### 🐞 Fixes diff --git a/src/commands/Folders.ts b/src/commands/Folders.ts index 179fc4e1..f81e9e57 100644 --- a/src/commands/Folders.ts +++ b/src/commands/Folders.ts @@ -12,7 +12,7 @@ import { format } from 'date-fns'; import { Dashboard } from './Dashboard'; import { parseWinPath } from '../helpers/parseWinPath'; import { MediaHelpers } from '../helpers/MediaHelpers'; -import { MediaListener } from '../listeners'; +import { MediaListener, PagesListener } from '../listeners'; export const WORKSPACE_PLACEHOLDER = `[[workspace]]`; @@ -288,6 +288,9 @@ export class Folders { })); await Settings.update(SETTINGS_CONTENT_PAGE_FOLDERS, folderDetails, true); + + // Reinitialize the folder listeners + PagesListener.startWatchers(); } /** diff --git a/src/extension.ts b/src/extension.ts index 3b46f7f5..3b9b3563 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -18,6 +18,7 @@ import { Content } from './commands/Content'; import ContentProvider from './providers/ContentProvider'; import { Wysiwyg } from './commands/Wysiwyg'; import { Diagnostics } from './commands/Diagnostics'; +import { PagesListener } from './listeners'; let frontMatterStatusBar: vscode.StatusBarItem; let statusDebouncer: { (fnc: any, time: number): void; }; @@ -40,6 +41,10 @@ export async function activate(context: vscode.ExtensionContext) { SettingsHelper.checkToPromote(); + // Start listening to the folders for content changes. + // This will make sure the dashboard is up to date + PagesListener.startWatchers(); + collection = vscode.languages.createDiagnosticCollection('frontMatter'); // Pages dashboard diff --git a/src/listeners/PagesListener.ts b/src/listeners/PagesListener.ts index 7296a9f5..1b112278 100644 --- a/src/listeners/PagesListener.ts +++ b/src/listeners/PagesListener.ts @@ -1,7 +1,7 @@ import { isValidFile } from './../helpers/isValidFile'; import { existsSync } from "fs"; import { dirname, join } from "path"; -import { commands, Uri } from "vscode"; +import { commands, FileSystemWatcher, RelativePattern, Uri, workspace } from "vscode"; import { Dashboard } from "../commands/Dashboard"; import { Folders } from "../commands/Folders"; import { COMMAND_NAME, DefaultFields, SETTINGS_CONTENT_STATIC_FOLDER, SETTING_DATE_FIELD, SETTING_SEO_DESCRIPTION_FIELD } from "../constants"; @@ -16,6 +16,34 @@ import { BaseListener } from "./BaseListener"; export class PagesListener extends BaseListener { + private static watchers: { [path: string]: FileSystemWatcher } = {}; + + /** + * Start watching the folders in the current workspace for content changes + */ + public static async startWatchers() { + const folders = Folders.get(); + + if (!folders || folders.length === 0) { + return; + } + + // Dispose all the current watchers + const paths = Object.keys(this.watchers); + for (const path of paths) { + const watcher = this.watchers[path]; + watcher.dispose(); + delete this.watchers[path]; + } + + // Recreate all the watchers + for (const folder of folders) { + const folderUri = Uri.parse(folder.path); + let watcher = workspace.createFileSystemWatcher(new RelativePattern(folderUri, "*")); + watcher.onDidCreate(async (uri: Uri) => this.getPagesData); + this.watchers[folderUri.fsPath] = watcher; + } + } /** * Process the messages for the dashboard views