如何使用扩展包更新 vscode 中的用户 settings.json?

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

我开发了一个vscode extension VSCodeWebDeveloperExperiencePack,现在我正面临其中一些之间的冲突,比如turbo consoledeploy,我写了一个手册对于扩展页面上的配置,如下所示:

您可能想要使用的预定义设置:

  1. ctrl+shift+p
  2. 输入settings
  3. 单击 Prefrences: Open settings (JSON) 打开您的 settings.json 文件
  4. 添加以下几行设置:
  "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/**"
  ]

此外,还有一些 keybindings 配置如下:

[
  {
    "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"
  }
]

但我想让它成为可能或向用户展示 select configure automatically 的选择,点击它或安装扩展程序时,它会将所需的配置放入用户的 settings.json.

我阅读了 VScode ,但无法解决问题,例如我确实将其添加到我的 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"
        }
      }
    }
  }

我已经根据 vscode 文档解决了这个问题,这里有一个你可以使用的例子:

  1. 把这个放在 package.json zpack-package.json:
 "contributes": {
    "commands": [
      {
        "command": "zpack.updateConfig",
        "title": "Update Essentials Web Extension Pack (ZPack series) Config"
      }
    ]
  },
  1. 使用这个来触发动作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() {}