最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is there a way to get the extension's settings (defined in package.json) at runtime? - Stack Overflow

programmeradmin0浏览0评论

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 badges
Add a ment  | 

2 Answers 2

Reset to default 5

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.

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}`);
}
发布评论

评论列表(0)

  1. 暂无评论