From d19e632f804555a29d00572f4964dfd8ca642fa7 Mon Sep 17 00:00:00 2001 From: Elio Struyf Date: Fri, 15 Oct 2021 16:11:59 +0200 Subject: [PATCH] #151 #152 - Framework detection + preset public folder --- package.json | 5 ++ src/commands/Dashboard.ts | 32 ++++++++- src/constants/FrameworkDetectors.ts | 21 ++++++ src/constants/settings.ts | 2 + src/dashboardWebView/DashboardMessage.ts | 3 +- .../components/Steps/Step.tsx | 41 +++++++---- .../components/Steps/StepsToGetStarted.tsx | 71 +++++++++++++++++-- .../components/WelcomeScreen.tsx | 4 +- src/dashboardWebView/models/Settings.ts | 4 +- src/helpers/FrameworkDetector.ts | 43 +++++++++++ src/models/Framework.ts | 8 +++ src/models/index.ts | 4 ++ 12 files changed, 215 insertions(+), 23 deletions(-) create mode 100644 src/constants/FrameworkDetectors.ts create mode 100644 src/helpers/FrameworkDetector.ts create mode 100644 src/models/Framework.ts diff --git a/package.json b/package.json index f37e4b85..a74b7c0b 100644 --- a/package.json +++ b/package.json @@ -180,6 +180,11 @@ "markdownDescription": "Specify if you want to open the dashboard when you start VS Code. [Check in the docs](https://frontmatter.codes/docs/settings#frontmatter.dashboard.openonstart)", "scope": "Dashboard" }, + "frontMatter.framework.id": { + "type": "string", + "default": "", + "markdownDescription": "Specify the ID of your static site generator or framework you are using for your website. [Check in the docs](https://frontmatter.codes/docs/settings#frontMatter.framework.id)" + }, "frontMatter.panel.freeform": { "type": "boolean", "default": true, diff --git a/src/commands/Dashboard.ts b/src/commands/Dashboard.ts index c0487931..232eb560 100644 --- a/src/commands/Dashboard.ts +++ b/src/commands/Dashboard.ts @@ -1,10 +1,10 @@ -import { SETTINGS_CONTENT_STATIC_FOLDER, SETTING_DATE_FIELD, SETTING_SEO_DESCRIPTION_FIELD, SETTINGS_DASHBOARD_OPENONSTART, SETTINGS_DASHBOARD_MEDIA_SNIPPET, SETTING_TAXONOMY_CONTENT_TYPES, DefaultFields, HOME_PAGE_NAVIGATION_ID, ExtensionState, COMMAND_NAME } from '../constants'; +import { SETTINGS_CONTENT_STATIC_FOLDER, SETTING_DATE_FIELD, SETTING_SEO_DESCRIPTION_FIELD, SETTINGS_DASHBOARD_OPENONSTART, SETTINGS_DASHBOARD_MEDIA_SNIPPET, SETTING_TAXONOMY_CONTENT_TYPES, DefaultFields, HOME_PAGE_NAVIGATION_ID, ExtensionState, COMMAND_NAME, SETTINGS_FRAMEWORK_ID } from '../constants'; import { ArticleHelper } from './../helpers/ArticleHelper'; import { basename, dirname, extname, join, parse } from "path"; import { existsSync, readdirSync, statSync, unlinkSync, writeFileSync } from "fs"; import { commands, Uri, ViewColumn, Webview, WebviewPanel, window, workspace, env, Position } from "vscode"; import { Settings as SettingsHelper } from '../helpers'; -import { TaxonomyType } from '../models'; +import { Framework, TaxonomyType } from '../models'; import { Folders } from './Folders'; import { DashboardCommand } from '../dashboardWebView/DashboardCommand'; import { DashboardMessage } from '../dashboardWebView/DashboardMessage'; @@ -24,6 +24,7 @@ import { MediaLibrary } from '../helpers/MediaLibrary'; import imageSize from 'image-size'; import { parseWinPath } from '../helpers/parseWinPath'; import { DateHelper } from '../helpers/DateHelper'; +import { FrameworkDetector } from '../helpers/FrameworkDetector'; export class Dashboard { private static webview: WebviewPanel | null = null; @@ -187,6 +188,9 @@ export class Dashboard { case DashboardMessage.createMediaFolder: await commands.executeCommand(COMMAND_NAME.createFolder, msg?.data); break; + case DashboardMessage.setFramework: + Dashboard.setFramework(msg?.data); + break; } }); } @@ -257,6 +261,8 @@ export class Dashboard { private static async getSettings() { const ext = Extension.getInstance(); const wsFolder = Folders.getWorkspaceFolder(); + // const isInitialized = await Template.isInitialized(); + const isInitialized = false; Dashboard.postWebviewMessage({ command: DashboardCommand.settings, @@ -265,7 +271,7 @@ export class Dashboard { wsFolder: wsFolder ? wsFolder.fsPath : '', staticFolder: SettingsHelper.get(SETTINGS_CONTENT_STATIC_FOLDER), folders: Folders.get(), - initialized: await Template.isInitialized(), + initialized: isInitialized, tags: SettingsHelper.getTaxonomy(TaxonomyType.Tag), categories: SettingsHelper.getTaxonomy(TaxonomyType.Category), openOnStart: SettingsHelper.get(SETTINGS_DASHBOARD_OPENONSTART), @@ -274,10 +280,30 @@ export class Dashboard { mediaSnippet: SettingsHelper.get(SETTINGS_DASHBOARD_MEDIA_SNIPPET) || [], contentTypes: SettingsHelper.get(SETTING_TAXONOMY_CONTENT_TYPES) || [], contentFolders: Folders.get().map(f => f.path), + crntFramework: SettingsHelper.get(SETTINGS_FRAMEWORK_ID), + framework: (!isInitialized && wsFolder) ? FrameworkDetector.get(wsFolder.fsPath) : null, } as Settings }); } + /** + * Set the current site-generator or framework + related settings + * @param frameworkId + */ + private static setFramework(frameworkId: string | null) { + SettingsHelper.update(SETTINGS_FRAMEWORK_ID, frameworkId, true); + + if (frameworkId) { + const allFrameworks = FrameworkDetector.getAll(); + const framework = allFrameworks.find((f: Framework) => f.name === frameworkId); + if (framework) { + SettingsHelper.update(SETTINGS_CONTENT_STATIC_FOLDER, framework.static, true); + } else { + SettingsHelper.update(SETTINGS_CONTENT_STATIC_FOLDER, "", true); + } + } + } + /** * Update a setting from the dashboard */ diff --git a/src/constants/FrameworkDetectors.ts b/src/constants/FrameworkDetectors.ts new file mode 100644 index 00000000..5433233d --- /dev/null +++ b/src/constants/FrameworkDetectors.ts @@ -0,0 +1,21 @@ +export const FrameworkDetectors = [ + { + "framework": {"name": "gatsby", "dist": "public", "static": "static", "build": "gatsby build"}, + "requiredFiles": ["gatsby-config.js"], + "requiredDependencies": ["gatsby"] + }, + { + "framework": {"name": "hugo", "dist": "public", "static": "static", "build": "hugo"}, + "requiredFiles": ["config.toml", "config.yaml", "config.yml"] + }, + { + "framework": {"name": "next", "dist": ".next", "static": "public", "build": "next build"}, + "requiredFiles": ["next.config.js"], + "requiredDependencies": ["next"] + }, + { + "framework": {"name": "nuxt", "dist": "dist", "static": "static", "build": "nuxt"}, + "requiredFiles": ["nuxt.config.js"], + "requiredDependencies": ["nuxt"] + } +]; \ No newline at end of file diff --git a/src/constants/settings.ts b/src/constants/settings.ts index fa0f92b2..4643dbc0 100644 --- a/src/constants/settings.ts +++ b/src/constants/settings.ts @@ -42,6 +42,8 @@ export const SETTINGS_CONTENT_FRONTMATTER_HIGHLIGHT = "content.fmHighlight"; export const SETTINGS_DASHBOARD_OPENONSTART = "dashboard.openOnStart"; export const SETTINGS_DASHBOARD_MEDIA_SNIPPET = "dashboard.mediaSnippet"; +export const SETTINGS_FRAMEWORK_ID = "framework.id"; + /** * @deprecated */ diff --git a/src/dashboardWebView/DashboardMessage.ts b/src/dashboardWebView/DashboardMessage.ts index cb86a9c6..242bec0c 100644 --- a/src/dashboardWebView/DashboardMessage.ts +++ b/src/dashboardWebView/DashboardMessage.ts @@ -17,5 +17,6 @@ export enum DashboardMessage { deleteMedia = 'deleteMedia', insertPreviewImage = 'insertPreviewImage', updateMediaMetadata = 'updateMediaMetadata', - createMediaFolder = 'createMediaFolder' + createMediaFolder = 'createMediaFolder', + setFramework = 'setFramework', } \ No newline at end of file diff --git a/src/dashboardWebView/components/Steps/Step.tsx b/src/dashboardWebView/components/Steps/Step.tsx index 7e961ef4..553f40f3 100644 --- a/src/dashboardWebView/components/Steps/Step.tsx +++ b/src/dashboardWebView/components/Steps/Step.tsx @@ -4,22 +4,17 @@ import { Status } from '../../models/Status'; export interface IStepProps { name: string; - description: string; + description: JSX.Element; status: Status; showLine: boolean; - onClick?: () => void; + onClick?: () => void | undefined; } export const Step: React.FunctionComponent = ({name, description, status, showLine, onClick}: React.PropsWithChildren) => { - return ( - <> - { - showLine ? ( -