How to update users settings.json in vscode with extension packs?

Viewed 1884

I've developed a vscode extension VSCodeWebDeveloperExperiencePack, Now I'm facing the conflicting between some of them, like turbo console and deploy, I wrote a manual for the configuration on the extension page, as you can see in below:

Pre-defined settings which you maybe want to use:

  1. press ctrl+shift+p
  2. type settings
  3. click on Prefrences: Open settings (JSON) to open your settings.json file
  4. add these lines of settings:
  "workbench.iconTheme": "material-icon-theme",
  "workbench.colorTheme": "Atom One Dark",
  "files.autoSave": "afterDelay",
  "editor.fontFamily": "Fira Code",
  "editor.fontLigatures": "",
  "editor.formatOnSave": true,
  "editor.defaultFormatter": "esbenp.prettier-vscode",
  "editor.fontSize": 16,
  "window.zoomLevel": 1,
  "sync.gist": "70a5fe700fe4e46aebdf678a5c1db398",
  "typescript.preferences.importModuleSpecifier": "non-relative",
  "local-history.exclude": [
    "**/.history/**",
    "**/.vscode/**",
    "**/node_modules/**",
    "**/typings/**",
    "**/out/**",
    "**/Code/User/**"
  ]

Also, there are some keybindings configs like below:

[
  {
    "key": "ctrl+shift+alt+l",
    "command": "bookmarks.jumpToNext",
    "when": "editorTextFocus"
  },
  {
    "key": "ctrl+alt+l",
    "command": "-bookmarks.jumpToNext",
    "when": "editorTextFocus"
  },
  {
    "key": "ctrl+shift+alt+k",
    "command": "extension.deploy.listen"
  },
  {
    "key": "ctrl+alt+l",
    "command": "-extension.deploy.listen"
  }
]

But I want to make it possible or show the user a choice to select configure automatically, which on click on it, or on installing the extension, it put the desiered config to user's settings.json.

I read VScode , but couldn't fix the problem, for example I did add this to my ext-pack package.json:

  "contributes": {
    "configuration": {
      "title": "VsCode Web Developer Experience",
      "properties": {
        "editor.fontSize": {
          "type": "number",
          "default": 22,
          "description": "this will changes the font settings"
        }
      }
    }
  }
1 Answers

I've solved this problem based on vscode docs, here is an example which you can use:

  1. Put this in package.json zpack-package.json:
 "contributes": {
    "commands": [
      {
        "command": "zpack.updateConfig",
        "title": "Update Essentials Web Extension Pack (ZPack series) Config"
      }
    ]
  },
  1. Use this for triggering the action zpack-extension.ts:
import * as vscode from "vscode";
import { extractAsKeyValue, GeneralObject } from "./util";
import { defaultSettings } from "./defaultSettings";

const updateUserSettings = async (settings: GeneralObject[]) => {
  settings.forEach(async setting => {
    const { key, value } = extractAsKeyValue(setting);
    await vscode.workspace
      .getConfiguration()
      .update(key, value, vscode.ConfigurationTarget.Global);
  });
};
export async function activate(context: vscode.ExtensionContext) {
  console.log(
    'Congratulations, your extension "Essentials Web Extension Pack (ZPack series)" is now active!'
  );
  let disposable = vscode.commands.registerCommand(
    "zpack.updateConfig",
    async () => {
      console.log(JSON.stringify(defaultSettings, null, 1));
      await updateUserSettings(defaultSettings);
      await vscode.window.showInformationMessage(
        "ZPack Config has been updated"
      );
    }
  );
  context.subscriptions.push(disposable);
}

export function deactivate() {}
Related