From 89aab6c74e4e807cf5a66bc7b05d5b4739348764 Mon Sep 17 00:00:00 2001 From: Elio Struyf Date: Wed, 27 Jul 2022 11:56:51 +0200 Subject: [PATCH] #352 - Added notification + progress notification --- src/helpers/ArticleHelper.ts | 52 ++++++++++++++----------- src/helpers/ContentType.ts | 74 +++++++++++++++++++----------------- 2 files changed, 69 insertions(+), 57 deletions(-) diff --git a/src/helpers/ArticleHelper.ts b/src/helpers/ArticleHelper.ts index 0100a50f..aa054757 100644 --- a/src/helpers/ArticleHelper.ts +++ b/src/helpers/ArticleHelper.ts @@ -407,36 +407,42 @@ export class ArticleHelper { for (const placeholder of placeholders) { if (value.includes(`{{${placeholder.id}}}`)) { - let placeHolderValue = placeholder.value || ""; - if (placeholder.script) { - const wsFolder = Folders.getWorkspaceFolder(); - const script = { title: placeholder.id, script: placeholder.script, command: placeholder.command }; - let output: string | any = await CustomScript.executeScript(script, wsFolder?.fsPath || "", `'${filePath}' '${title}'`); + try { + let placeHolderValue = placeholder.value || ""; + if (placeholder.script) { + const wsFolder = Folders.getWorkspaceFolder(); + const script = { title: placeholder.id, script: placeholder.script, command: placeholder.command }; + let output: string | any = await CustomScript.executeScript(script, wsFolder?.fsPath || "", `'${wsFolder?.fsPath}' '${filePath}' '${title}'`); - if (output) { - // Check if the output needs to be parsed - if (output.includes("{") && output.includes("}")) { - try { - output = JSON.parse(output); - } catch (e) { - // Do nothing + if (output) { + // Check if the output needs to be parsed + if (output.includes("{") && output.includes("}")) { + try { + output = JSON.parse(output); + } catch (e) { + // Do nothing + } + } else { + output = output.split("\n"); } - } else { - output = output.split("\n"); + + placeHolderValue = output; } - - placeHolderValue = output; } - } - const regex = new RegExp(`{{${placeholder.id}}}`, "g"); - const updatedValue = processKnownPlaceholders(placeHolderValue, title, dateFormat); + const regex = new RegExp(`{{${placeholder.id}}}`, "g"); + const updatedValue = processKnownPlaceholders(placeHolderValue, title, dateFormat); - if (value === `{{${placeholder.id}}}`) { - value = updatedValue; - } else { - value = value.replace(regex, updatedValue); + if (value === `{{${placeholder.id}}}`) { + value = updatedValue; + } else { + value = value.replace(regex, updatedValue); + } + } catch (e) { + Notifications.error(`Error while processing the ${placeholder.id} placeholder`); + Logger.error((e as Error).message); } + } } } diff --git a/src/helpers/ContentType.ts b/src/helpers/ContentType.ts index 263a4e7a..5b1845db 100644 --- a/src/helpers/ContentType.ts +++ b/src/helpers/ContentType.ts @@ -3,7 +3,7 @@ import { PagesListener } from './../listeners/dashboard'; import { ArticleHelper, Settings } from "."; import { FEATURE_FLAG, SETTING_CONTENT_DRAFT_FIELD, SETTING_DATE_FORMAT, SETTING_FRAMEWORK_ID, SETTING_TAXONOMY_CONTENT_TYPES, SETTING_TAXONOMY_FIELD_GROUPS, TelemetryEvent } from "../constants"; import { ContentType as IContentType, DraftField, Field, FieldGroup, FieldType } from '../models'; -import { Uri, commands, window } from 'vscode'; +import { Uri, commands, window, ProgressLocation } from 'vscode'; import { Folders } from "../commands/Folders"; import { Questions } from "./Questions"; import { existsSync, writeFileSync } from "fs"; @@ -499,53 +499,59 @@ export class ContentType { * @returns */ private static async create(contentType: IContentType, folderPath: string) { - const titleValue = await Questions.ContentTitle(); - if (!titleValue) { - return; - } + window.withProgress({ + location: ProgressLocation.Notification, + title: "Front Matter: Creating content...", + cancellable: false + }, async () => { + const titleValue = await Questions.ContentTitle(); + if (!titleValue) { + return; + } - let templatePath = contentType.template; - let templateData: ParsedFrontMatter | null = null; - if (templatePath) { - templatePath = Folders.getAbsFilePath(templatePath); - templateData = ArticleHelper.getFrontMatterByPath(templatePath); - } + let templatePath = contentType.template; + let templateData: ParsedFrontMatter | null = null; + if (templatePath) { + templatePath = Folders.getAbsFilePath(templatePath); + templateData = ArticleHelper.getFrontMatterByPath(templatePath); + } - let newFilePath: string | undefined = ArticleHelper.createContent(contentType, folderPath, titleValue); - if (!newFilePath) { - return; - } + let newFilePath: string | undefined = ArticleHelper.createContent(contentType, folderPath, titleValue); + if (!newFilePath) { + return; + } - if (contentType.name === "default") { - const crntFramework = Settings.get(SETTING_FRAMEWORK_ID); - if (crntFramework?.toLowerCase() === "jekyll") { - const idx = contentType.fields.findIndex(f => f.name === "draft"); - if (idx > -1) { - contentType.fields.splice(idx, 1); + if (contentType.name === "default") { + const crntFramework = Settings.get(SETTING_FRAMEWORK_ID); + if (crntFramework?.toLowerCase() === "jekyll") { + const idx = contentType.fields.findIndex(f => f.name === "draft"); + if (idx > -1) { + contentType.fields.splice(idx, 1); + } } } - } - let data: any = await this.processFields(contentType, titleValue, templateData?.data || {}, newFilePath); + let data: any = await this.processFields(contentType, titleValue, templateData?.data || {}, newFilePath); - data = ArticleHelper.updateDates(Object.assign({}, data)); + data = ArticleHelper.updateDates(Object.assign({}, data)); - if (contentType.name !== DEFAULT_CONTENT_TYPE_NAME) { - data['type'] = contentType.name; - } + if (contentType.name !== DEFAULT_CONTENT_TYPE_NAME) { + data['type'] = contentType.name; + } - const content = ArticleHelper.stringifyFrontMatter(templateData?.content || ``, data); + const content = ArticleHelper.stringifyFrontMatter(templateData?.content || ``, data); - writeFileSync(newFilePath, content, { encoding: "utf8" }); + writeFileSync(newFilePath, content, { encoding: "utf8" }); - await commands.executeCommand('vscode.open', Uri.file(newFilePath)); + await commands.executeCommand('vscode.open', Uri.file(newFilePath)); - Notifications.info(`Your new content has been created.`); + Notifications.info(`Your new content has been created.`); - Telemetry.send(TelemetryEvent.createContentFromContentType); + Telemetry.send(TelemetryEvent.createContentFromContentType); - // Trigger a refresh for the dashboard - PagesListener.refresh(); + // Trigger a refresh for the dashboard + PagesListener.refresh(); + }) } /**