From c63310a2db2179e2830fcebdbed17a21ee9bf1a8 Mon Sep 17 00:00:00 2001 From: Elio Struyf Date: Wed, 25 May 2022 12:51:39 +0200 Subject: [PATCH] Initialize template folder --- CHANGELOG.md | 1 - package.json | 7 +++++- src/commands/Project.ts | 47 +++++++++++++++++++++++--------------- src/constants/Extension.ts | 1 + src/extension.ts | 4 ++++ 5 files changed, 39 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d42abf37..f323bdd0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,6 @@ ### 🎨 Enhancements - JSON schema enhancements for working with data files -- Remove template creation from project initialization - [#330](https://github.com/estruyf/vscode-front-matter/issues/330): Allow custom scripts to easily update front matter - [#331](https://github.com/estruyf/vscode-front-matter/issues/331): Added functionality to run other type of scripts - [#332](https://github.com/estruyf/vscode-front-matter/issues/332): New `dataFile` field which allows you to create data file references diff --git a/package.json b/package.json index eb37ac39..c4e9a49f 100644 --- a/package.json +++ b/package.json @@ -1365,9 +1365,14 @@ "dark": "assets/icons/close-dark.svg" } }, + { + "command": "frontMatter.initTemplate", + "title": "Initialize the template folder", + "category": "Front matter" + }, { "command": "frontMatter.createTemplate", - "title": "Create a template from current file", + "title": "Create template from current file", "category": "Front matter" }, { diff --git a/src/commands/Project.ts b/src/commands/Project.ts index ff5a43b2..e488f116 100644 --- a/src/commands/Project.ts +++ b/src/commands/Project.ts @@ -36,25 +36,7 @@ categories: [] Settings.createTeamSettings(); if (sampleTemplate !== undefined) { - const fileType = Settings.get(SETTING_CONTENT_DEFAULT_FILETYPE); - - const folder = Template.getSettings(); - const templatePath = Project.templatePath(); - - if (!folder || !templatePath) { - return; - } - - const article = Uri.file(join(templatePath.fsPath, `article.${fileType}`)); - - if (!fs.existsSync(templatePath.fsPath)) { - await workspace.fs.createDirectory(templatePath); - } - - if (sampleTemplate) { - fs.writeFileSync(article.fsPath, Project.content, { encoding: "utf-8" }); - Notifications.info("Project initialized successfully."); - } + await Project.createSampleTemplate(); } else { Notifications.info("Project initialized successfully."); } @@ -76,6 +58,33 @@ categories: [] } } + /** + * Creates the templates folder + sample if needed + * @param sampleTemplate + * @returns + */ + public static async createSampleTemplate(sampleTemplate?: boolean) { + const fileType = Settings.get(SETTING_CONTENT_DEFAULT_FILETYPE); + + const folder = Template.getSettings(); + const templatePath = Project.templatePath(); + + if (!folder || !templatePath) { + return; + } + + const article = Uri.file(join(templatePath.fsPath, `article.${fileType}`)); + + if (!fs.existsSync(templatePath.fsPath)) { + await workspace.fs.createDirectory(templatePath); + } + + if (sampleTemplate) { + fs.writeFileSync(article.fsPath, Project.content, { encoding: "utf-8" }); + Notifications.info("Sample template created."); + } + } + /** * Get the template path for the current project */ diff --git a/src/constants/Extension.ts b/src/constants/Extension.ts index 5d946737..385b8641 100644 --- a/src/constants/Extension.ts +++ b/src/constants/Extension.ts @@ -25,6 +25,7 @@ export const COMMAND_NAME = { createByContentType: getCommandName("createByContentType"), createByTemplate: getCommandName("createByTemplate"), createTemplate: getCommandName("createTemplate"), + initTemplate: getCommandName("initTemplate"), collapseSections: getCommandName("collapseSections"), preview: getCommandName("preview"), dashboard: getCommandName("dashboard"), diff --git a/src/extension.ts b/src/extension.ts index 40095bd8..b8d17d55 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -136,6 +136,10 @@ export async function activate(context: vscode.ExtensionContext) { }); let createTemplate = vscode.commands.registerCommand(COMMAND_NAME.createTemplate, Template.generate); + + subscriptions.push( + vscode.commands.registerCommand(COMMAND_NAME.initTemplate, () => Project.createSampleTemplate(true)) + ); const toggleDraftCommand = COMMAND_NAME.toggleDraft; const toggleDraft = vscode.commands.registerCommand(toggleDraftCommand, async () => {