VSCode Extension-更新扩展配置的值

时间:2019-07-17 16:40:52

标签: typescript visual-studio-code vscode-extensions

在处理VS Code扩展时,我试图在异步函数内部添加功能,该功能将从扩展中更新配置的值。

package.json的相关部分:

    "contributes": {
        "configuration": {
            "title": "Extension Features",
            "properties": {
                "myextension.currentOrg": {
                    "type": "object",
                    "orgId": "",
                    "orgUsername": "",
                    "orgSecret": "",
                    "orgLoginUrl": "",
                    "description": "myextension.currentOrg description",
                    "default": {
                        "orgId": "",
                        "orgUsername": "",
                        "orgSecret": "",
                        "orgLoginUrl": "https://login.salesforce.com"
                    } 
                },
                "myextension.saveOrgData": {
                    "type": "boolean",
                    "default": true,
                    "description": "myextension.saveOrgData description"
                }
            }
        }
     }

在我的异步函数中,我试图做到这一点:

let currentSecret = vscode.workspace.getConfiguration("myextension.currentOrg").get("orgSecret");

if(currentSecret == "") {
  vscode.workspace.getConfiguration("myextension.currentOrg").update("orgSecret", "1234");
}

由于.update()返回了Thenable,有人可以帮忙解释如何将其正确包装在Promise中吗?

如果我什至尝试在调试模式下从REPL运行上面的.update()命令,配置也不会更改,并且不清楚原因。

这是我的功能示例

  public async run(): Promise<any> {

    let clientSecret: string;
    const currentDoc = this.getCurrentDocument();
    const currentWorkspace = this.getWorkspaceRoot();
    const orgData = customUtils.getConnectedOrgSync();

    let clientIdPreSplit = orgData.split("\n")[7].split(' ');
    let clientId = clientIdPreSplit[clientIdPreSplit.length - 1];
    // check the extension config

    // extension's default value for myextension.currentOrg.orgSecret is set to ""
    let currentSecret = vscode.workspace.getConfiguration("wfdx.currentOrg").get("orgSecret");

if(currentSecret == "") {
  vscode.window.showInformationMessage("updating config...");

  // this does not appear to update the extension's config in the 'contributes' section of package.json
  await vscode.workspace.getConfiguration("myextension.currentOrg").update("orgSecret", "1234");
} else {
    clientSecret = await showInputBox();
}

    const jsonProjectInfo = fs.readFileSync(`${currentWorkspace}`+'/sfdx-project.json', "utf-8");
    let projectAsJSON = JSON.parse(jsonProjectInfo);
    let loginURL = projectAsJSON["sfdcLoginUrl"];

    const jsf = new jsforce.Connection({
      loginUrl: loginURL,
      clientId: clientId,
      clientSecret: clientSecret
    });
...
// more file manipulation logic
return jsf;
}

任何人和所有帮助表示赞赏。谢谢!

0 个答案:

没有答案