#106 - new init for team config

This commit is contained in:
Elio Struyf
2021-09-19 18:54:38 +02:00
parent 25201e6859
commit 8e699db62d
3 changed files with 71 additions and 25 deletions
+4 -1
View File
@@ -5,6 +5,7 @@ import * as fs from "fs";
import { Notifications } from "../helpers/Notifications";
import { Template } from "./Template";
import { Folders } from "./Folders";
import { Settings } from "../helpers";
export class Project {
@@ -26,6 +27,8 @@ categories: []
*/
public static async init(sampleTemplate: boolean = true) {
try {
Settings.createTeamSettings();
const folder = Template.getSettings();
const templatePath = Project.templatePath();
@@ -43,7 +46,7 @@ categories: []
fs.writeFileSync(article.fsPath, Project.content, { encoding: "utf-8" });
Notifications.info("Project initialized successfully.");
}
} catch (err) {
} catch (err: any) {
Notifications.error(`Sorry, something went wrong - ${err?.message || err}`);
}
}
+3
View File
@@ -80,6 +80,9 @@ export class Extension {
await Settings.update(SETTINGS_CONTENT_PAGE_FOLDERS, paths);
}
// Create team settings
Settings.createTeamSettings();
// Migration to version 3.2.0
const dateField = Settings.get<string>(SETTING_DATE_FIELD);
const lastModField = Settings.get<string>(SETTING_MODIFIED_FIELD);
+64 -24
View File
@@ -10,15 +10,17 @@ export class Settings {
private static config: vscode.WorkspaceConfiguration;
private static globalFile = "frontmatter.json";
private static globalConfig: any;
private static initialConfig = {
"$schema": "https://frontmatter.codes/frontmatter.schema.json"
};
public static init() {
const wsFolder = Folders.getWorkspaceFolder();
if (wsFolder) {
const fmConfig = join(wsFolder.fsPath, Settings.globalFile);
if (existsSync(fmConfig)) {
const localConfig = readFileSync(fmConfig, 'utf8');
Settings.globalConfig = JSON.parse(localConfig);
}
const fmConfig = Settings.projectConfigPath;
if (fmConfig && existsSync(fmConfig)) {
const localConfig = readFileSync(fmConfig, 'utf8');
Settings.globalConfig = JSON.parse(localConfig);
} else {
Settings.globalConfig = undefined;
}
Settings.config = vscode.workspace.getConfiguration(CONFIG_KEY);
@@ -43,7 +45,7 @@ export class Settings {
workspace.onDidSaveTextDocument(async (e) => {
const filename = e.uri.fsPath;
if (filename && basename(filename).toLowerCase() === Settings.globalFile.toLowerCase()) {
if (Settings.checkProjectConfig(filename)) {
const file = await workspace.openTextDocument(e.uri);
if (file) {
const fileContents = file.getText();
@@ -52,6 +54,13 @@ export class Settings {
}
}
});
workspace.onDidDeleteFiles((e) => {
const needCallback = e?.files.find(f => Settings.checkProjectConfig(f.fsPath));
if (needCallback) {
callback();
}
});
}
/**
@@ -63,7 +72,7 @@ export class Settings {
let setting = undefined;
const settingKey = `${CONFIG_KEY}.${name}`;
if (typeof Settings.globalConfig[settingKey] !== "undefined") {
if (Settings.globalConfig && typeof Settings.globalConfig[settingKey] !== "undefined") {
setting = Settings.globalConfig[settingKey];
}
@@ -85,18 +94,15 @@ export class Settings {
* @param value
*/
public static async update<T>(name: string, value: T, updateGlobal: boolean = false) {
const wsFolder = Folders.getWorkspaceFolder();
const fmConfig = Settings.projectConfigPath;
if (updateGlobal) {
if (wsFolder) {
const fmConfig = join(wsFolder.fsPath, Settings.globalFile);
if (existsSync(fmConfig)) {
const localConfig = readFileSync(fmConfig, 'utf8');
Settings.globalConfig = JSON.parse(localConfig);
Settings.globalConfig[`${CONFIG_KEY}.${name}`] = value;
writeFileSync(fmConfig, JSON.stringify(Settings.globalConfig, null, 2), 'utf8');
return;
}
if (fmConfig && existsSync(fmConfig)) {
const localConfig = readFileSync(fmConfig, 'utf8');
Settings.globalConfig = JSON.parse(localConfig);
Settings.globalConfig[`${CONFIG_KEY}.${name}`] = value;
writeFileSync(fmConfig, JSON.stringify(Settings.globalConfig, null, 2), 'utf8');
return;
}
} else {
await Settings.config.update(name, value);
@@ -108,11 +114,16 @@ export class Settings {
}
/**
* Retrieves the config
* @returns
* Create team settings
*/
public static getConfig(): vscode.WorkspaceConfiguration {
return Settings.config;
public static createTeamSettings() {
const wsFolder = Folders.getWorkspaceFolder();
if (wsFolder) {
const configPath = join(wsFolder.fsPath, Settings.globalFile);
if (!existsSync(configPath)) {
writeFileSync(configPath, JSON.stringify(Settings.initialConfig, null, 2), 'utf8');
}
}
}
/**
@@ -138,11 +149,40 @@ export class Settings {
* @param type
* @param options
*/
static async updateTaxonomy(type: TaxonomyType, options: string[]) {
public static async updateTaxonomy(type: TaxonomyType, options: string[]) {
const config = vscode.workspace.getConfiguration(CONFIG_KEY);
const configSetting = type === TaxonomyType.Tag ? SETTING_TAXONOMY_TAGS : SETTING_TAXONOMY_CATEGORIES;
options = [...new Set(options)];
options = options.sort().filter(o => !!o);
await config.update(configSetting, options);
}
/**
* Check if its the project config
* @param filePath
* @returns
*/
private static checkProjectConfig(filePath: string) {
const fmConfig = Settings.projectConfigPath;
if (fmConfig && existsSync(fmConfig)) {
return filePath &&
basename(filePath).toLowerCase() === Settings.globalFile.toLowerCase() &&
fmConfig.toLowerCase() === filePath.toLowerCase();
}
return false;
}
/**
* Get the project config path
* @returns
*/
private static get projectConfigPath() {
const wsFolder = Folders.getWorkspaceFolder();
if (wsFolder) {
const fmConfig = join(wsFolder.fsPath, Settings.globalFile);
return fmConfig;
}
return undefined;
}
}