From 64fc1e4b76544a14f090518f0e0e5a559e351690 Mon Sep 17 00:00:00 2001 From: Elio Struyf Date: Thu, 23 May 2024 10:40:38 +0200 Subject: [PATCH] Refactoring of command registration --- src/commands/Content.ts | 17 ++++- src/commands/Diagnostics.ts | 16 ++++- src/commands/Folders.ts | 14 +++- src/commands/Project.ts | 19 ++++++ src/commands/Settings.ts | 27 +++++++- src/commands/Template.ts | 23 ++++++- src/extension.ts | 121 ++++------------------------------ src/helpers/ContentType.ts | 28 ++++++++ src/helpers/SettingsHelper.ts | 7 ++ 9 files changed, 155 insertions(+), 117 deletions(-) diff --git a/src/commands/Content.ts b/src/commands/Content.ts index b5201a24..9f056681 100644 --- a/src/commands/Content.ts +++ b/src/commands/Content.ts @@ -1,10 +1,25 @@ import { commands, QuickPickItem, window } from 'vscode'; import { COMMAND_NAME, SETTING_TEMPLATES_ENABLED } from '../constants'; -import { Settings } from '../helpers'; +import { Extension, Settings } from '../helpers'; import * as l10n from '@vscode/l10n'; import { LocalizationKey } from '../localization'; export class Content { + /** + * Registers the commands for the Content class. + */ + public static async registerCommands() { + const ext = Extension.getInstance(); + const subscriptions = ext.subscriptions; + + subscriptions.push(commands.registerCommand(COMMAND_NAME.createContent, Content.create)); + } + + /** + * Creates content based on user selection. + * If templates are enabled, shows a quick pick menu to choose between content type and template. + * If templates are disabled, executes the createByContentType command directly. + */ public static async create() { const templatesEnabled = await Settings.get(SETTING_TEMPLATES_ENABLED); if (!templatesEnabled) { diff --git a/src/commands/Diagnostics.ts b/src/commands/Diagnostics.ts index b76a03ba..8268dbd6 100644 --- a/src/commands/Diagnostics.ts +++ b/src/commands/Diagnostics.ts @@ -1,12 +1,24 @@ import { Folders } from './Folders'; -import { ViewColumn, workspace } from 'vscode'; +import { ViewColumn, commands, workspace } from 'vscode'; import ContentProvider from '../providers/ContentProvider'; import { join } from 'path'; import { ContentFolder } from '../models'; import { Settings } from '../helpers/SettingsHelper'; -import { DEFAULT_FILE_TYPES, SETTING_CONTENT_SUPPORTED_FILETYPES } from '../constants'; +import { + COMMAND_NAME, + DEFAULT_FILE_TYPES, + SETTING_CONTENT_SUPPORTED_FILETYPES +} from '../constants'; +import { Extension } from '../helpers'; export class Diagnostics { + public static async registerCommands() { + const ext = Extension.getInstance(); + const subscriptions = ext.subscriptions; + + subscriptions.push(commands.registerCommand(COMMAND_NAME.diagnostics, Diagnostics.show)); + } + public static async show() { const folders = await Folders.get(); const projectName = Folders.getProjectFolderName(); diff --git a/src/commands/Folders.ts b/src/commands/Folders.ts index 7d8149b6..9b0cc27c 100644 --- a/src/commands/Folders.ts +++ b/src/commands/Folders.ts @@ -1,6 +1,7 @@ import { STATIC_FOLDER_PLACEHOLDER } from './../constants/StaticFolderPlaceholder'; import { Questions } from './../helpers/Questions'; import { + COMMAND_NAME, SETTING_CONTENT_I18N, SETTING_CONTENT_PAGE_FOLDERS, SETTING_CONTENT_STATIC_FOLDER, @@ -14,7 +15,7 @@ import { ContentFolder, FileInfo, FolderInfo, I18nConfig, StaticFolder } from '. import uniqBy = require('lodash.uniqby'); import { Template } from './Template'; import { Notifications } from '../helpers/Notifications'; -import { Logger, Settings, processTimePlaceholders } from '../helpers'; +import { Extension, Logger, Settings, processTimePlaceholders } from '../helpers'; import { existsSync } from 'fs'; import { format } from 'date-fns'; import { Dashboard } from './Dashboard'; @@ -34,6 +35,13 @@ export const WORKSPACE_PLACEHOLDER = `[[workspace]]`; export class Folders { private static _folders: ContentFolder[] = []; + public static async registerCommands() { + const ext = Extension.getInstance(); + const subscriptions = ext.subscriptions; + + subscriptions.push(commands.registerCommand(COMMAND_NAME.createByTemplate, Folders.create)); + } + public static clearCached() { Logger.verbose(`Folders:clearCached`); Folders._folders = []; @@ -717,7 +725,9 @@ export class Folders { let foundFiles = await Folders.findFiles(filePath); // Make sure these file are coming from the folder path (this could be an issue in multi-root workspaces) - foundFiles = foundFiles.filter((f) => parseWinPath(f.fsPath).startsWith(parseWinPath(folderUri.fsPath))); + foundFiles = foundFiles.filter((f) => + parseWinPath(f.fsPath).startsWith(parseWinPath(folderUri.fsPath)) + ); files = [...files, ...foundFiles]; } diff --git a/src/commands/Project.ts b/src/commands/Project.ts index 0521a3f6..e4047e21 100644 --- a/src/commands/Project.ts +++ b/src/commands/Project.ts @@ -42,6 +42,25 @@ categories: [] const ext = Extension.getInstance(); const subscriptions = ext.subscriptions; + // Initialize command + subscriptions.push( + commands.registerCommand(COMMAND_NAME.init, async (cb: Function) => { + await Project.init(); + + if (cb) { + cb(); + } + }) + ); + + subscriptions.push( + commands.registerCommand(COMMAND_NAME.initTemplate, () => Project.createSampleTemplate(true)) + ); + + subscriptions.push(commands.registerCommand(COMMAND_NAME.registerFolder, Folders.register)); + subscriptions.push(commands.registerCommand(COMMAND_NAME.unregisterFolder, Folders.unregister)); + subscriptions.push(commands.registerCommand(COMMAND_NAME.createFolder, Folders.addMediaFolder)); + subscriptions.push(commands.registerCommand(COMMAND_NAME.switchProject, Project.switchProject)); } diff --git a/src/commands/Settings.ts b/src/commands/Settings.ts index a125ca4b..d9fc3ec2 100644 --- a/src/commands/Settings.ts +++ b/src/commands/Settings.ts @@ -1,14 +1,37 @@ import { TaxonomyHelper } from './../helpers/TaxonomyHelper'; import * as vscode from 'vscode'; import { TaxonomyType } from '../models'; -import { EXTENSION_NAME } from '../constants'; -import { ArticleHelper, FilesHelper } from '../helpers'; +import { COMMAND_NAME, EXTENSION_NAME } from '../constants'; +import { ArticleHelper, Extension, FilesHelper } from '../helpers'; import { FrontMatterParser } from '../parsers'; import { Notifications } from '../helpers/Notifications'; import * as l10n from '@vscode/l10n'; import { LocalizationKey } from '../localization'; export class Settings { + public static async registerCommands() { + const ext = Extension.getInstance(); + const subscriptions = ext.subscriptions; + + subscriptions.push( + vscode.commands.registerCommand(COMMAND_NAME.createTag, () => { + Settings.create(TaxonomyType.Tag); + }) + ); + + subscriptions.push( + vscode.commands.registerCommand(COMMAND_NAME.createCategory, () => { + Settings.create(TaxonomyType.Category); + }) + ); + + subscriptions.push( + vscode.commands.registerCommand(COMMAND_NAME.exportTaxonomy, Settings.export) + ); + + subscriptions.push(vscode.commands.registerCommand(COMMAND_NAME.remap, Settings.remap)); + } + /** * Create a new taxonomy * diff --git a/src/commands/Template.ts b/src/commands/Template.ts index aaa176c0..29fff89e 100644 --- a/src/commands/Template.ts +++ b/src/commands/Template.ts @@ -2,12 +2,13 @@ import { Questions } from './../helpers/Questions'; import * as vscode from 'vscode'; import * as path from 'path'; import { + COMMAND_NAME, SETTING_CONTENT_DEFAULT_FILETYPE, SETTING_TEMPLATES_FOLDER, TelemetryEvent } from '../constants'; -import { ArticleHelper, Settings } from '../helpers'; -import { Article } from '.'; +import { ArticleHelper, Extension, Settings } from '../helpers'; +import { Article, Folders } from '.'; import { Notifications } from '../helpers/Notifications'; import { Project } from './Project'; import { ContentType } from '../helpers/ContentType'; @@ -20,6 +21,24 @@ import * as l10n from '@vscode/l10n'; import { LocalizationKey } from '../localization'; export class Template { + public static async registerCommands() { + const ext = Extension.getInstance(); + const subscriptions = ext.subscriptions; + + subscriptions.push( + vscode.commands.registerCommand(COMMAND_NAME.createTemplate, Template.generate) + ); + + subscriptions.push( + vscode.commands.registerCommand(COMMAND_NAME.createFromTemplate, (folder: vscode.Uri) => { + const folderPath = Folders.getFolderPath(folder); + if (folderPath) { + Template.create(folderPath); + } + }) + ); + } + /** * Generate a template */ diff --git a/src/extension.ts b/src/extension.ts index 600fc712..857c680b 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -16,7 +16,6 @@ import { PagesListener } from './listeners/dashboard'; import { ModeSwitch } from './services/ModeSwitch'; import { PagesParser } from './services/PagesParser'; import { ContentType, Telemetry, Extension } from './helpers'; -import { TaxonomyType } from './models'; import * as l10n from '@vscode/l10n'; import { Backers, @@ -93,8 +92,13 @@ export async function activate(context: vscode.ExtensionContext) { Dashboard.init(); Dashboard.registerCommands(); + // Multilingual commands i18n.register(); + // Setting commands + Settings.registerCommands(); + SettingsHelper.registerCommands(); + if (!extension.getVersion().usedVersion) { vscode.commands.executeCommand(COMMAND_NAME.dashboard); } @@ -129,101 +133,23 @@ export async function activate(context: vscode.ExtensionContext) { } ); - const createTag = vscode.commands.registerCommand(COMMAND_NAME.createTag, () => { - Settings.create(TaxonomyType.Tag); - }); - - const createCategory = vscode.commands.registerCommand(COMMAND_NAME.createCategory, () => { - Settings.create(TaxonomyType.Category); - }); - - const exportTaxonomy = vscode.commands.registerCommand( - COMMAND_NAME.exportTaxonomy, - Settings.export - ); - - const remap = vscode.commands.registerCommand(COMMAND_NAME.remap, Settings.remap); - // Register all the article commands Article.registerCommands(subscriptions); - subscriptions.push( - vscode.commands.registerCommand(COMMAND_NAME.initTemplate, () => - Project.createSampleTemplate(true) - ) - ); - - // Register project folders - const registerFolder = vscode.commands.registerCommand( - COMMAND_NAME.registerFolder, - Folders.register - ); - - const unregisterFolder = vscode.commands.registerCommand( - COMMAND_NAME.unregisterFolder, - Folders.unregister - ); - - const createFolder = vscode.commands.registerCommand( - COMMAND_NAME.createFolder, - Folders.addMediaFolder - ); - /** * Template creation */ - const createTemplate = vscode.commands.registerCommand( - COMMAND_NAME.createTemplate, - Template.generate - ); - const createFromTemplate = vscode.commands.registerCommand( - COMMAND_NAME.createFromTemplate, - (folder: vscode.Uri) => { - const folderPath = Folders.getFolderPath(folder); - if (folderPath) { - Template.create(folderPath); - } - } - ); + Template.registerCommands(); /** * Content creation */ - const createByContentType = vscode.commands.registerCommand( - COMMAND_NAME.createByContentType, - ContentType.createContent - ); - const createByTemplate = vscode.commands.registerCommand( - COMMAND_NAME.createByTemplate, - Folders.create - ); - const createContent = vscode.commands.registerCommand(COMMAND_NAME.createContent, Content.create); + ContentType.registerCommands(); + Content.registerCommands(); + Folders.registerCommands(); - subscriptions.push( - vscode.commands.registerCommand(COMMAND_NAME.generateContentType, ContentType.generate) - ); - - subscriptions.push( - vscode.commands.registerCommand(COMMAND_NAME.addMissingFields, ContentType.addMissingFields) - ); - - subscriptions.push( - vscode.commands.registerCommand(COMMAND_NAME.setContentType, ContentType.setContentType) - ); - - // Initialize command - subscriptions.push( - vscode.commands.registerCommand(COMMAND_NAME.init, async (cb: Function) => { - await Project.init(); - - if (cb) { - cb(); - } - }) - ); - - // Settings promotion command - subscriptions.push(vscode.commands.registerCommand(COMMAND_NAME.promote, SettingsHelper.promote)); + // Project commands + Project.registerCommands(); // Collapse all sections in the webview const collapseAll = vscode.commands.registerCommand(COMMAND_NAME.collapseSections, () => { @@ -303,7 +229,7 @@ export async function activate(context: vscode.ExtensionContext) { ModeSwitch.register(); // Diagnostics - subscriptions.push(vscode.commands.registerCommand(COMMAND_NAME.diagnostics, Diagnostics.show)); + Diagnostics.registerCommands(); // Git GitListener.init(); @@ -315,29 +241,8 @@ export async function activate(context: vscode.ExtensionContext) { // Cache commands Cache.registerCommands(); - // Project switching - Project.registerCommands(); - // Subscribe all commands - subscriptions.push( - insertTags, - PanelView, - insertCategories, - createTag, - createCategory, - exportTaxonomy, - remap, - createFromTemplate, - createTemplate, - registerFolder, - unregisterFolder, - createContent, - createByContentType, - createByTemplate, - collapseAll, - createFolder, - fmStatusBarItem - ); + subscriptions.push(insertTags, PanelView, insertCategories, collapseAll, fmStatusBarItem); console.log(`π–₯π—‹π—ˆπ—‡π— 𝖬𝖺𝗍𝗍𝖾𝗋 𝖒𝖬𝖲 𝖺𝖼𝗍𝗂𝗏𝖺𝗍𝖾𝖽! 𝖱𝖾𝖺𝖽𝗒 π—π—ˆ π—Œπ—π–Ίπ—‹π— 𝗐𝗋𝗂𝗍𝗂𝗇𝗀... πŸ‘©β€πŸ’»πŸ§‘β€πŸ’»πŸ‘¨β€πŸ’»`); } diff --git a/src/helpers/ContentType.ts b/src/helpers/ContentType.ts index 2edf19fc..ad9cca86 100644 --- a/src/helpers/ContentType.ts +++ b/src/helpers/ContentType.ts @@ -3,12 +3,14 @@ import { PagesListener } from './../listeners/dashboard'; import { ArticleHelper, CustomScript, + Extension, Logger, Settings, processArticlePlaceholdersFromData, processTimePlaceholders } from '.'; import { + COMMAND_NAME, DefaultFieldValues, EXTENSION_NAME, FEATURE_FLAG, @@ -40,6 +42,32 @@ import * as l10n from '@vscode/l10n'; import { LocalizationKey } from '../localization'; export class ContentType { + /** + * Registers the commands related to content types. + * + * @param subscriptions - The array of subscriptions to which the commands will be added. + */ + public static async registerCommands() { + const ext = Extension.getInstance(); + const subscriptions = ext.subscriptions; + + subscriptions.push( + commands.registerCommand(COMMAND_NAME.createByContentType, ContentType.createContent) + ); + + subscriptions.push( + commands.registerCommand(COMMAND_NAME.generateContentType, ContentType.generate) + ); + + subscriptions.push( + commands.registerCommand(COMMAND_NAME.addMissingFields, ContentType.addMissingFields) + ); + + subscriptions.push( + commands.registerCommand(COMMAND_NAME.setContentType, ContentType.setContentType) + ); + } + /** * Retrieve the draft field * @returns diff --git a/src/helpers/SettingsHelper.ts b/src/helpers/SettingsHelper.ts index e6c3e0d2..11093072 100644 --- a/src/helpers/SettingsHelper.ts +++ b/src/helpers/SettingsHelper.ts @@ -78,6 +78,13 @@ export class Settings { private static project: Project | undefined = undefined; private static configDebouncer = debounceCallback(); + public static async registerCommands() { + const ext = Extension.getInstance(); + const subscriptions = ext.subscriptions; + + subscriptions.push(commands.registerCommand(COMMAND_NAME.promote, Settings.promote)); + } + public static async init() { const allCommands = await commands.getCommands(true); await Settings.readConfig();