Skip to content

Commit

Permalink
#553 - Dynamic settings update
Browse files Browse the repository at this point in the history
  • Loading branch information
estruyf committed Sep 18, 2023
1 parent ac3b4a0 commit c79d0ee
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"
},
Expand Down
3 changes: 2 additions & 1 deletion package.nls.de.json
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
}
3 changes: 2 additions & 1 deletion package.nls.ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
}
3 changes: 2 additions & 1 deletion package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
}
1 change: 1 addition & 0 deletions src/constants/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
40 changes: 39 additions & 1 deletion src/helpers/SettingsHelper.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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(
Expand Down
5 changes: 4 additions & 1 deletion webpack/extension.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

const path = require('path');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const IgnoreDynamicRequire = require('webpack-ignore-dynamic-require');

const config = [
{
Expand Down Expand Up @@ -56,7 +57,9 @@ const config = [
}
}
},
plugins: []
plugins: [
new IgnoreDynamicRequire()
]
}
];

Expand Down

0 comments on commit c79d0ee

Please sign in to comment.