VSCode extensions location variable

╄→尐↘猪︶ㄣ 提交于 2019-11-26 14:57:47

问题


In an extension I am writing, I want to redefine an existing setting in the workspace to point at a script I am packaging with the extension. On a mac this script lives in ~/.vscode/extensions/publisher.name.version/script for example.

If I assume that this is where the extension lives then in my activate function I can update this value using

export async function activate(context: vscode.ExtensionContext) {
  const home = process.env.HOME;
  const execLocation = home + "/.vscode/extensions/publisher.name.version/script";
  ...

and then updating the workspace setting.

However - I would like to access the locally installed extensions location, together with the id and version of my extension - I cannot find the correct setting in VSCode to do this. I would be very grateful if someone knew the correct environment variable so I could access them.

I know it is possible to call code from the command line with the option --extensionHomePath - I am not sure how to access this variable programmatically.

Also I am not sure how to find the version, publisher and name from the context parameter - obviously I know these from the package.json file but it would be nice to be able to access them programmatically if possible.


回答1:


You can get that information by the asAbsolutePath() method of the ExtensionContext.
This method gets you the absolute path of a resource for a given relative path (regarding your project root).

Therefore I suggest you to change your code to the following:

export async function activate(context: vscode.ExtensionContext) {
    const execLocation = context.asAbsolutePath("script");
    console.log("Absolute exec location: " + execLocation);


来源:https://stackoverflow.com/questions/51670505/vscode-extensions-location-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!