From cac009b77371840b984aa0a99f79df4d769d339c Mon Sep 17 00:00:00 2001 From: Elio Struyf Date: Fri, 1 Oct 2021 10:18:48 +0200 Subject: [PATCH] #110 - Support for workspaces with multiple folders --- CHANGELOG.md | 3 ++- src/commands/Folders.ts | 27 +++++++++++++++++++++++++-- src/commands/Project.ts | 1 - src/helpers/SettingsHelper.ts | 7 +++++-- 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 47339d28..a2fc1fbe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Change Log -## [4.1.0] - 2021-XX-XX +## [4.1.0] - 2021-XX-XX - [Release Notes](https://beta.frontmatter.codes/updates/v4.1.0) ### ✨ New features @@ -8,6 +8,7 @@ ### 🎨 Enhancements +- [#110](https://github.com/estruyf/vscode-front-matter/issues/110): Add support for workspaces with multiple folders - [#117](https://github.com/estruyf/vscode-front-matter/issues/117): Allow to specify a singleline of text in the metadata fields - [#119](https://github.com/estruyf/vscode-front-matter/issues/119): Multi-select support for choice fields - [#121](https://github.com/estruyf/vscode-front-matter/issues/121): Choice fields support ID/title objects as well as a regular string diff --git a/src/commands/Folders.ts b/src/commands/Folders.ts index d7079b16..66097bb1 100644 --- a/src/commands/Folders.ts +++ b/src/commands/Folders.ts @@ -6,6 +6,7 @@ import uniqBy = require("lodash.uniqby"); import { Template } from "./Template"; import { Notifications } from "../helpers/Notifications"; import { Settings } from "../helpers"; +import { existsSync } from 'fs'; export const WORKSPACE_PLACEHOLDER = `[[workspace]]`; @@ -114,8 +115,31 @@ export class Folders { */ public static getWorkspaceFolder(): Uri | undefined { const folders = workspace.workspaceFolders; - if (folders && folders.length > 0) { + + if (folders && folders.length === 1) { return folders[0].uri; + } else if (folders && folders.length > 1) { + let projectFolder = undefined; + + for (const folder of folders) { + if (!projectFolder && existsSync(join(folder.uri.fsPath, Settings.globalFile))) { + projectFolder = folder.uri; + } + } + + if (!projectFolder) { + window.showWorkspaceFolderPick({ + placeHolder: `Please select the main workspace folder for Front Matter to use.` + }).then(selectedFolder => { + if (selectedFolder) { + Settings.createGlobalFile(selectedFolder.uri); + // Full reload to make sure the whole extension is reloaded correctly + commands.executeCommand(`workbench.action.reloadWindow`); + } + }); + } + + return projectFolder; } return undefined; } @@ -126,7 +150,6 @@ export class Folders { public static getProjectFolderName(): string { const wsFolder = Folders.getWorkspaceFolder(); if (wsFolder) { - // const projectFolder = wsFolder?.fsPath.split('\\').join('/').split('/').pop(); return basename(wsFolder.fsPath); } return ""; diff --git a/src/commands/Project.ts b/src/commands/Project.ts index 23d08556..d71d3ebf 100644 --- a/src/commands/Project.ts +++ b/src/commands/Project.ts @@ -1,5 +1,4 @@ import { workspace, Uri } from "vscode"; -import { CONFIG_KEY, SETTING_TEMPLATES_FOLDER } from "../constants"; import { join } from "path"; import * as fs from "fs"; import { Notifications } from "../helpers/Notifications"; diff --git a/src/helpers/SettingsHelper.ts b/src/helpers/SettingsHelper.ts index cdabfc6b..35526850 100644 --- a/src/helpers/SettingsHelper.ts +++ b/src/helpers/SettingsHelper.ts @@ -1,5 +1,5 @@ import { Notifications } from './Notifications'; -import { workspace } from 'vscode'; +import { Uri, workspace } from 'vscode'; import * as vscode from 'vscode'; import { TaxonomyType } from '../models'; import { SETTING_TAXONOMY_TAGS, SETTING_TAXONOMY_CATEGORIES, CONFIG_KEY } from '../constants'; @@ -9,8 +9,8 @@ import { existsSync, readFileSync, watch, writeFileSync } from 'fs'; import { Extension } from './Extension'; export class Settings { + public static globalFile = "frontmatter.json"; private static config: vscode.WorkspaceConfiguration; - private static globalFile = "frontmatter.json"; private static globalConfig: any; public static init() { @@ -131,7 +131,10 @@ export class Settings { */ public static createTeamSettings() { const wsFolder = Folders.getWorkspaceFolder(); + this.createGlobalFile(wsFolder); + } + public static createGlobalFile(wsFolder: Uri | undefined | null) { const initialConfig = { "$schema": `https://${Extension.getInstance().isBetaVersion() ? `beta.` : ``}frontmatter.codes/frontmatter.schema.json` };