UNPKG

@tomjs/vscode

Version:

Some utilities to simplify the development of VSCode Extensions

181 lines (180 loc) 5.13 kB
var _ctx; function setExtensionContext(ctx) { _ctx = ctx; } function getExtensionContext() { return _ctx; } function getCtx() { return _ctx; } import fs from 'fs'; import path from 'path'; import { readJsonSync } from '@tomjs/node'; import { env } from 'vscode'; var DEFAULT_LANGUAGE = 'en'; var DEFAULT_NLS = 'package.nls.json'; function loadI18n(extensionPath, language) { const lang = language ?? env.language.toLocaleLowerCase(); let name = lang === DEFAULT_LANGUAGE ? DEFAULT_NLS : `package.nls.${lang}.json`; const nlsPath = path.join(extensionPath, name); if (!fs.existsSync(nlsPath)) { name = DEFAULT_NLS; } return Object.assign( {}, readJsonSync(path.join(extensionPath, DEFAULT_NLS)), readJsonSync(nlsPath), ); } var I18n = class { constructor(messages, language) { this.use(messages, language); } use(messages, language) { if (typeof messages === 'string') { this.messages = loadI18n(messages, language); } else if (typeof messages === 'object') { this.messages = messages; } } t(...params) { if (params.length === 0 || !this.messages) { return ''; } const [key, ...args] = params; const text = this.messages[key] ?? ''; if (args[0] === null || args[0] === void 0 || args[0] === '') { return text; } const values = typeof args[0] === 'object' ? args[0] : args; return text.replace(/{([^}]+)}/g, (match, group) => values[group] ?? match); } }; var i18n = new I18n(); import cloneDeep from 'lodash.clonedeep'; import { ConfigurationTarget, workspace } from 'vscode'; var Configuration = class { constructor(identifier, defaultValues) { this._defaultValues = {}; this._values = {}; this.identifier = identifier; this._defaultValues = Object.assign({}, defaultValues); } configuration() { return workspace.getConfiguration(this.identifier); } get(section, defaultValue) { return this.configuration().get(section, defaultValue ?? this._defaultValues[section]); } values() { const values = Object.assign({}); const cfg = this.configuration(); Object.keys(cfg) .filter(key => typeof cfg[key] !== 'function') .forEach(key => { values[key] = cfg.get(key) ?? cloneDeep(this._defaultValues[key]); }); return values; } async update(section, value, target) { const values = {}; let _target; if (typeof section === 'string') { values[section] = value; _target = target; } else if (typeof section === 'object') { Object.assign(values, section); _target = value; } else { throw new Error(''); } const cfg = this.configuration(); await Promise.all( Object.keys(values).map(key => cfg.update(key, values[key], _target ?? ConfigurationTarget.Global).then(() => { this._values[key] = values[key]; }), ), ); } }; import { version } from 'vscode'; var isInsider = version.includes('-insider'); import { ExtensionMode } from 'vscode'; function isUnderDevelopment() { return getCtx().extensionMode == ExtensionMode.Development; } function isUnderProduction() { return getCtx().extensionMode == ExtensionMode.Production; } import os from 'os'; import path2 from 'path'; function getUserDataPath() { const storageDir = path2.dirname(getCtx().globalStorageUri.fsPath); return path2.dirname(storageDir); } function getDotVSCodePath() { return path2.join(os.homedir(), isInsider ? '.vscode-insiders' : '.vscode'); } import { window, workspace as workspace2 } from 'vscode'; function getActiveWorkspaceFolder() { let activeWorkspace; const editor = window.activeTextEditor; if (editor) { activeWorkspace = workspace2.getWorkspaceFolder(editor.document.uri); } else { if (workspace2.workspaceFolders && workspace2.workspaceFolders.length > 0) { activeWorkspace = workspace2.workspaceFolders[0]; } } return activeWorkspace; } function getActiveWorkspaceFolderUri() { var _a; return (_a = getActiveWorkspaceFolder()) == null ? void 0 : _a.uri; } function getActiveWorkspaceFolderPath() { var _a; return (_a = getActiveWorkspaceFolder()) == null ? void 0 : _a.uri.fsPath; } function getAllWorkspaceFolders() { const folders = []; if (workspace2.workspaceFolders) { folders.push(...workspace2.workspaceFolders.map(s => ({ ...s, current: false }))); } if (folders.length === 0) { return []; } const activePath = getActiveWorkspaceFolderPath(); const current = folders.find(s => s.uri.fsPath === activePath); if (current) { current.active = true; } return folders; } function initExtension(ctx) { setExtensionContext(ctx); i18n.use(ctx.extensionPath); } var src_default = { init: initExtension }; export { Configuration, I18n, src_default as default, getActiveWorkspaceFolder, getActiveWorkspaceFolderPath, getActiveWorkspaceFolderUri, getAllWorkspaceFolders, getCtx, getDotVSCodePath, getExtensionContext, getUserDataPath, i18n, initExtension, isInsider, isUnderDevelopment, isUnderProduction, loadI18n, setExtensionContext, };