In a Visual Studio Code extension, is there a way to get the extension's settings (defined in package.json) at runtime? There are a few values (like displayName) that I'd like to get.
In a Visual Studio Code extension, is there a way to get the extension's settings (defined in package.json) at runtime? There are a few values (like displayName) that I'd like to get.
Share Improve this question edited Sep 13, 2018 at 19:01 Jeff asked Sep 6, 2018 at 20:32 JeffJeff 1991 silver badge10 bronze badges2 Answers
Reset to default 5A Visual Studio Code extension is written in JavaScript and no different from a standard Node script, so generally speaking you can use fs.readFile
to read the extension manifest and JSON.parse
to read its values.
Depending on your use-case there might be simpler options.
require()
To read your own extension's package.json
, you could simply use require()
Example:
// lib/extension.js
const meta = require('../package.json')
import
The same as above is possible with an import
, at least when using TypeScript.
Example:
// src/extension.ts
import * as meta from '../package.json'
Make sure to add a type declarations for JSON files
// src/index.d.ts
declare module '*.json' {
const value: any;
export default value;
}
Node dependency
Last but not least, you can read any extension's package.json
programmatically. Using a Node packages such as vscode-read-manifest, read-pkg (or read-pkg-up) make it easy.
Example:
const readManifest = require('vscode-read-manifest');
// Async
(async () => {
let manifest = await readManifest('ms-python.python');
})();
// Sync
let manifest = readManifest.sync('ms-python.python');
You can access package.json
content from the vscode.ExtensionContext
:
import * as vscode from 'vscode';
export function activate(ctx: vscode.ExtensionContext) {
const id = ctx.extension.id;
const version = ctx.extension.packageJSON.version;
vscode.window.showInformationMessage(`${id} ${version}`);
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742415665a4439693.html
评论列表(0条)