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.
A 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.
To read your own extension's package.json, you could simply use require()
Example:
// lib/extension.js
const meta = require('../package.json')
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;
}
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');