From c79d0ee603a7e80e6ab1d103324583891b6a1bd0 Mon Sep 17 00:00:00 2001 From: Elio Struyf Date: Mon, 18 Sep 2023 16:51:21 +0200 Subject: [PATCH] #553 - Dynamic settings update --- CHANGELOG.md | 1 + package-lock.json | 13 ++++++++++++ package.json | 6 ++++++ package.nls.de.json | 3 ++- package.nls.ja.json | 3 ++- package.nls.json | 3 ++- src/constants/settings.ts | 1 + src/helpers/SettingsHelper.ts | 40 ++++++++++++++++++++++++++++++++++- webpack/extension.config.js | 5 ++++- 9 files changed, 70 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 215428f2..79bc5238 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ### ✨ New features +- [#553](https://github.com/estruyf/vscode-front-matter/issues/553): New `frontMatter.config.dynamicFilePath` setting which allows you to dynamically update the settings from a custom JS file - [#563](https://github.com/estruyf/vscode-front-matter/issues/563): New `fieldCollection` to inherit/reuse fields in multiple content-types ### 🎨 Enhancements diff --git a/package-lock.json b/package-lock.json index e1e87d23..18bba483 100644 --- a/package-lock.json +++ b/package-lock.json @@ -110,6 +110,7 @@ "webpack-bundle-analyzer": "^4.7.0", "webpack-cli": "^4.10.0", "webpack-dev-server": "^4.11.1", + "webpack-ignore-dynamic-require": "^1.0.0", "yaml": "^2.2.1", "yawn-yaml": "^1.5.0" }, @@ -12724,6 +12725,12 @@ } } }, + "node_modules/webpack-ignore-dynamic-require": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/webpack-ignore-dynamic-require/-/webpack-ignore-dynamic-require-1.0.0.tgz", + "integrity": "sha512-WeGFPgwDochKPwizAu5XsHcPq3aaQLl2E+n1piD/VPGNUo5HIwrtURWNMfrPDfkHVOx+flkAihXbUiILAv5x4Q==", + "dev": true + }, "node_modules/webpack-merge": { "version": "5.8.0", "dev": true, @@ -21882,6 +21889,12 @@ } } }, + "webpack-ignore-dynamic-require": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/webpack-ignore-dynamic-require/-/webpack-ignore-dynamic-require-1.0.0.tgz", + "integrity": "sha512-WeGFPgwDochKPwizAu5XsHcPq3aaQLl2E+n1piD/VPGNUo5HIwrtURWNMfrPDfkHVOx+flkAihXbUiILAv5x4Q==", + "dev": true + }, "webpack-merge": { "version": "5.8.0", "dev": true, diff --git a/package.json b/package.json index cdc51460..b1bec0d4 100644 --- a/package.json +++ b/package.json @@ -155,6 +155,11 @@ "type": "string" } }, + "frontMatter.config.dynamicFilePath": { + "type": "string", + "markdownDescription": "%setting.frontMatter.config.dynamicFilePath.markdownDescription%", + "default": "" + }, "frontMatter.content.autoUpdateDate": { "type": "boolean", "default": false, @@ -2575,6 +2580,7 @@ "webpack-bundle-analyzer": "^4.7.0", "webpack-cli": "^4.10.0", "webpack-dev-server": "^4.11.1", + "webpack-ignore-dynamic-require": "^1.0.0", "yaml": "^2.2.1", "yawn-yaml": "^1.5.0" }, diff --git a/package.nls.de.json b/package.nls.de.json index a0fbff97..cad5722a 100644 --- a/package.nls.de.json +++ b/package.nls.de.json @@ -242,5 +242,6 @@ "setting.frontMatter.taxonomy.contentTypes.items.properties.fields.items.properties.customType.description": "🚧: Specify the name of the custom field type to use.", "setting.frontMatter.taxonomy.contentTypes.items.properties.fields.items.properties.clearEmpty.description": "🚧: Specify if the empty values should be cleared.", "setting.frontMatter.website.host.markdownDescription": "🚧: Specify the host URL of your website. [Check in the docs](https://frontmatter.codes/docs/settings/overview#frontmatter.website.url)", - "command.frontMatter.settings.refresh": "🚧: Refresh Front Matter Settings" + "command.frontMatter.settings.refresh": "🚧: Refresh Front Matter Settings", + "setting.frontMatter.config.dynamicFilePath.markdownDescription": "🚧: Specify the path to the dynamic config file (ex: [[workspace]]/config.js). [Check in the docs](https://frontmatter.codes/docs/settings/overview#frontmatter.config.dynamicfilepath)" } \ No newline at end of file diff --git a/package.nls.ja.json b/package.nls.ja.json index e1ee8fcd..bd3db0dd 100644 --- a/package.nls.ja.json +++ b/package.nls.ja.json @@ -243,5 +243,6 @@ "setting.frontMatter.taxonomy.contentTypes.items.properties.fields.items.properties.customType.description": "🚧: Specify the name of the custom field type to use.", "setting.frontMatter.taxonomy.contentTypes.items.properties.fields.items.properties.clearEmpty.description": "🚧: Specify if the empty values should be cleared.", "setting.frontMatter.website.host.markdownDescription": "🚧: Specify the host URL of your website. [Check in the docs](https://frontmatter.codes/docs/settings/overview#frontmatter.website.url)", - "command.frontMatter.settings.refresh": "🚧: Refresh Front Matter Settings" + "command.frontMatter.settings.refresh": "🚧: Refresh Front Matter Settings", + "setting.frontMatter.config.dynamicFilePath.markdownDescription": "🚧: Specify the path to the dynamic config file (ex: [[workspace]]/config.js). [Check in the docs](https://frontmatter.codes/docs/settings/overview#frontmatter.config.dynamicfilepath)" } \ No newline at end of file diff --git a/package.nls.json b/package.nls.json index f69ab838..972c54de 100644 --- a/package.nls.json +++ b/package.nls.json @@ -242,5 +242,6 @@ "setting.frontMatter.taxonomy.contentTypes.items.properties.fields.items.properties.customType.description": "Specify the name of the custom field type to use.", "setting.frontMatter.taxonomy.contentTypes.items.properties.fields.items.properties.clearEmpty.description": "Specify if the empty values should be cleared.", "setting.frontMatter.website.host.markdownDescription": "Specify the host URL of your website. [Check in the docs](https://frontmatter.codes/docs/settings/overview#frontmatter.website.url)", - "command.frontMatter.settings.refresh": "Refresh Front Matter Settings" + "command.frontMatter.settings.refresh": "Refresh Front Matter Settings", + "setting.frontMatter.config.dynamicFilePath.markdownDescription": "Specify the path to the dynamic config file (ex: [[workspace]]/config.js). [Check in the docs](https://frontmatter.codes/docs/settings/overview#frontmatter.config.dynamicfilepath)" } \ No newline at end of file diff --git a/src/constants/settings.ts b/src/constants/settings.ts index fecbae26..c5829b38 100644 --- a/src/constants/settings.ts +++ b/src/constants/settings.ts @@ -7,6 +7,7 @@ export const SETTING_EXPERIMENTAL = 'experimental'; export const SETTING_EXTENSIBILITY_SCRIPTS = 'extensibility.scripts'; export const SETTING_EXTENDS = 'extends'; +export const SETTING_CONFIG_DYNAMIC_FILE_PATH = 'config.dynamicFilePath'; export const SETTING_GLOBAL_NOTIFICATIONS = 'global.notifications'; export const SETTING_GLOBAL_NOTIFICATIONS_DISABLED = 'global.disabledNotifications'; diff --git a/src/helpers/SettingsHelper.ts b/src/helpers/SettingsHelper.ts index b567a3fd..09f62e6a 100644 --- a/src/helpers/SettingsHelper.ts +++ b/src/helpers/SettingsHelper.ts @@ -1,4 +1,8 @@ -import { SETTING_PROJECTS } from './../constants/settings'; +import { + EXTENSION_NAME, + SETTING_CONFIG_DYNAMIC_FILE_PATH, + SETTING_PROJECTS +} from './../constants/settings'; import { parseWinPath } from './parseWinPath'; import { Telemetry } from './Telemetry'; import { Notifications } from './Notifications'; @@ -620,6 +624,40 @@ export class Settings { for await (const configFile of configFiles) { await Settings.processConfigFile(configFile); } + + // Check if there is a dynamic config file and use it to update the global config + const dynamicConfigPath = + Settings.globalConfig[`${CONFIG_KEY}.${SETTING_CONFIG_DYNAMIC_FILE_PATH}`]; + if (dynamicConfigPath) { + try { + await window.withProgress( + { + location: vscode.ProgressLocation.Notification, + title: `${EXTENSION_NAME}: Reading dynamic config file...` + }, + async () => { + const absFilePath = Folders.getAbsFilePath(dynamicConfigPath); + Logger.info(`Reading dynamic config file: ${absFilePath}`); + if (absFilePath) { + if (await existsAsync(absFilePath)) { + const configFunction = require(absFilePath); + const dynamicConfig = await configFunction( + Object.assign({}, Settings.globalConfig) + ); + + if (dynamicConfig) { + Settings.globalConfig = dynamicConfig; + Logger.info(`Dynamic config file loaded`); + } + } + } + } + ); + } catch (e) { + Logger.error(`Error reading dynamic config file: ${dynamicConfigPath}`); + Logger.error((e as Error).message); + } + } } catch (e) { Settings.globalConfig = undefined; Notifications.error( diff --git a/webpack/extension.config.js b/webpack/extension.config.js index ea2617af..1c5b0c3d 100644 --- a/webpack/extension.config.js +++ b/webpack/extension.config.js @@ -4,6 +4,7 @@ const path = require('path'); const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin; +const IgnoreDynamicRequire = require('webpack-ignore-dynamic-require'); const config = [ { @@ -56,7 +57,9 @@ const config = [ } } }, - plugins: [] + plugins: [ + new IgnoreDynamicRequire() + ] } ];