Skip to content

Commit

Permalink
#137 - Ask to move the templates foler into frontmatter
Browse files Browse the repository at this point in the history
  • Loading branch information
estruyf committed Oct 6, 2021
1 parent 2e6a466 commit 1ea0999
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
- [#131](https://github.com/estruyf/vscode-front-matter/issues/131): Folder creation support added on media dashboard
- [#134](https://github.com/estruyf/vscode-front-matter/issues/134): On startup, the extension checks if local settings can be promoted
- [#135](https://github.com/estruyf/vscode-front-matter/issues/135): `Hidden` property added for field configuration
- [#137](https://github.com/estruyf/vscode-front-matter/issues/137): Ask to move the `.templates` folder to the new `.frontmatter` folder

### 🐞 Fixes

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@
},
"frontMatter.templates.folder": {
"type": "string",
"default": ".templates",
"default": ".frontmatter/templates",
"markdownDescription": "Specify the folder to use for your article templates. [Check in the docs](https://frontmatter.codes/docs/settings#frontmatter.templates.folder)",
"scope": "Templates"
},
Expand Down
3 changes: 2 additions & 1 deletion src/constants/ExtensionState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ export const ExtensionState = {
PagesView: `frontMatter:Pages:ViewType`,
SelectedFolder: `frontMatter:SelectedFolder`,
Version: `frontMatter:Version`,
SettingPromoted: `frontMatter:Settings:Promoted`
SettingPromoted: `frontMatter:Settings:Promoted`,
MoveTemplatesFolder: `frontMatter:Templates:Move`,
};
8 changes: 8 additions & 0 deletions src/constants/LocalStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@


export const LocalStore = {
rootFolder: ".frontmatter",
contentFolder: "content",
templatesFolder: "templates",
mediaDatabaseFile: "mediaDb.json"
}
1 change: 1 addition & 0 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from './DefaultFields';
export * from './Extension';
export * from './ExtensionState';
export * from './Links';
export * from './LocalStore';
export * from './Navigation';
export * from './charMap';
export * from './context';
Expand Down
34 changes: 32 additions & 2 deletions src/helpers/Extension.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { basename } from "path";
import { existsSync, renameSync } from "fs";
import { basename, join } from "path";
import { extensions, Uri, ExtensionContext, window, workspace, commands } from "vscode";
import { Folders, WORKSPACE_PLACEHOLDER } from "../commands/Folders";
import { EXTENSION_NAME, GITHUB_LINK, SETTINGS_CONTENT_FOLDERS, SETTINGS_CONTENT_PAGE_FOLDERS, SETTING_DATE_FIELD, SETTING_MODIFIED_FIELD, SETTING_SEO_DESCRIPTION_FIELD, SETTING_TAXONOMY_CONTENT_TYPES, DEFAULT_CONTENT_TYPE_NAME, EXTENSION_BETA_ID, EXTENSION_ID, ExtensionState, DefaultFields } from "../constants";
import { EXTENSION_NAME, GITHUB_LINK, SETTINGS_CONTENT_FOLDERS, SETTINGS_CONTENT_PAGE_FOLDERS, SETTING_DATE_FIELD, SETTING_MODIFIED_FIELD, SETTING_SEO_DESCRIPTION_FIELD, SETTING_TAXONOMY_CONTENT_TYPES, DEFAULT_CONTENT_TYPE_NAME, EXTENSION_BETA_ID, EXTENSION_ID, ExtensionState, DefaultFields, LocalStore, SETTING_TEMPLATES_FOLDER } from "../constants";
import { ContentType } from "../models";
import { Notifications } from "./Notifications";
import { parseWinPath } from "./parseWinPath";
import { Settings } from "./SettingsHelper";


Expand Down Expand Up @@ -183,6 +185,34 @@ export class Extension {
}
}
}

// Migration to version 5
if (major <= 5) {
const isMoved = await Extension.getInstance().getState<boolean | undefined>(ExtensionState.MoveTemplatesFolder);
if (!isMoved) {
const wsFolder= Folders.getWorkspaceFolder();
if (wsFolder) {
const templateFolder = join(parseWinPath(wsFolder.fsPath), `.templates`);
if (existsSync(templateFolder)) {
window.showInformationMessage(`Would you like to move your ".templates" folder to the new ".frontmatter" folder?`, 'Yes', 'No').then(async (result) => {
if (result === "Yes") {
const newFolderPath = join(parseWinPath(wsFolder.fsPath), LocalStore.rootFolder, LocalStore.templatesFolder);
renameSync(templateFolder, newFolderPath);
commands.executeCommand(`workbench.action.reloadWindow`);
Settings.update(SETTING_TEMPLATES_FOLDER, undefined, true);
Settings.update(SETTING_TEMPLATES_FOLDER, undefined);
} else if (result === "No") {
Settings.update(SETTING_TEMPLATES_FOLDER, `.templates`, true);
}

if (result === "No" || result === "Yes") {
Extension.getInstance().setState(ExtensionState.MoveTemplatesFolder, true);
}
});
}
}
}
}
}

public async setState<T>(propKey: string, propValue: T, type: "workspace" | "global" = "global"): Promise<void> {
Expand Down
3 changes: 2 additions & 1 deletion src/helpers/MediaLibrary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Folders, WORKSPACE_PLACEHOLDER } from '../commands/Folders';
import { existsSync, renameSync } from 'fs';
import { Notifications } from './Notifications';
import { parseWinPath } from './parseWinPath';
import { LocalStore } from '../constants';

interface MediaRecord {
description: string;
Expand All @@ -18,7 +19,7 @@ export class MediaLibrary {

private constructor() {
const wsFolder = Folders.getWorkspaceFolder();
this.db = new JsonDB(join(parseWinPath(wsFolder?.fsPath || ""), '.frontmatter/mediaDb.json'), true, false, '/');
this.db = new JsonDB(join(parseWinPath(wsFolder?.fsPath || ""), LocalStore.rootFolder, LocalStore.contentFolder, LocalStore.mediaDatabaseFile), true, false, '/');

workspace.onDidRenameFiles(e => {
e.files.forEach(f => {
Expand Down

0 comments on commit 1ea0999

Please sign in to comment.