mirror of
https://github.com/quine-global/hyper.git
synced 2026-01-17 14:08:41 -09:00
Add decorateEnv to the extensions API (#370)
* plugins: add decorateEnv to the extensions API * session: replace - with _ in LANG * plugins: refactor the at-least-one extension check
This commit is contained in:
parent
aaed99abac
commit
8c53d25b71
3 changed files with 39 additions and 46 deletions
|
|
@ -1,7 +1,6 @@
|
||||||
const { app, BrowserWindow, shell, Menu } = require('electron');
|
const { app, BrowserWindow, shell, Menu } = require('electron');
|
||||||
const createRPC = require('./rpc');
|
const createRPC = require('./rpc');
|
||||||
const createMenu = require('./menu');
|
const createMenu = require('./menu');
|
||||||
const Session = require('./session');
|
|
||||||
const genUid = require('uid2');
|
const genUid = require('uid2');
|
||||||
const { resolve } = require('path');
|
const { resolve } = require('path');
|
||||||
const isDev = require('electron-is-dev');
|
const isDev = require('electron-is-dev');
|
||||||
|
|
@ -12,6 +11,7 @@ const toHex = require('convert-css-color-name-to-hex');
|
||||||
const config = require('./config');
|
const config = require('./config');
|
||||||
config.init();
|
config.init();
|
||||||
const plugins = require('./plugins');
|
const plugins = require('./plugins');
|
||||||
|
const Session = require('./session');
|
||||||
|
|
||||||
const windowSet = new Set([]);
|
const windowSet = new Set([]);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,13 @@ const cache = new Config();
|
||||||
// modules path
|
// modules path
|
||||||
const path = resolve(homedir(), '.hyperterm_plugins');
|
const path = resolve(homedir(), '.hyperterm_plugins');
|
||||||
const localPath = resolve(homedir(), '.hyperterm_plugins', 'local');
|
const localPath = resolve(homedir(), '.hyperterm_plugins', 'local');
|
||||||
|
const availableExtensions = new Set([
|
||||||
|
'onApp', 'onWindow', 'onUnload', 'middleware',
|
||||||
|
'reduceUI', 'reduceSessions', 'decorateMenu',
|
||||||
|
'decorateTerm', 'decorateHyperTerm', 'decorateTab',
|
||||||
|
'decorateNotification', 'decorateNotifications',
|
||||||
|
'decorateTabs', 'decorateConfig', 'decorateEnv'
|
||||||
|
]);
|
||||||
|
|
||||||
// init plugin directories if not present
|
// init plugin directories if not present
|
||||||
mkdirpSync(path);
|
mkdirpSync(path);
|
||||||
|
|
@ -239,14 +246,8 @@ function requirePlugins () {
|
||||||
let mod;
|
let mod;
|
||||||
try {
|
try {
|
||||||
mod = require(path);
|
mod = require(path);
|
||||||
|
const exposed = mod && Object.keys(mod).some(key => availableExtensions.has(key));
|
||||||
if (!mod || (!mod.onApp && !mod.onWindow && !mod.onUnload &&
|
if (!exposed) {
|
||||||
!mod.middleware && !mod.reduceUI && !mod.reduceSessions &&
|
|
||||||
!mod.decorateConfig && !mod.decorateMenu &&
|
|
||||||
!mod.decorateTerm && !mod.decorateHyperTerm &&
|
|
||||||
!mod.decorateTab && !mod.decorateNotification &&
|
|
||||||
!mod.decorateNotifications && !mod.decorateTabs &&
|
|
||||||
!mod.decorateConfig)) {
|
|
||||||
notify('Plugin error!', `Plugin "${basename(path)}" does not expose any ` +
|
notify('Plugin error!', `Plugin "${basename(path)}" does not expose any ` +
|
||||||
'HyperTerm extension API methods');
|
'HyperTerm extension API methods');
|
||||||
return;
|
return;
|
||||||
|
|
@ -282,48 +283,37 @@ exports.onWindow = function (win) {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.decorateMenu = function (tpl) {
|
// decorates the base object by calling plugin[key]
|
||||||
let decorated = tpl;
|
// for all the available plugins
|
||||||
|
function decorateObject (base, key) {
|
||||||
|
let decorated = base;
|
||||||
modules.forEach((plugin) => {
|
modules.forEach((plugin) => {
|
||||||
if (plugin.decorateMenu) {
|
if (plugin[key]) {
|
||||||
const res = plugin.decorateMenu(decorated);
|
const res = plugin[key](decorated);
|
||||||
if (res) {
|
if (res && 'object' === typeof res) {
|
||||||
decorated = res;
|
decorated = res;
|
||||||
} else {
|
} else {
|
||||||
console.error('incompatible response type for `decorateMenu`');
|
notify('Plugin error!', `"${plugin._name}": invalid return type for \`${key}\``);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return decorated;
|
return decorated;
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.decorateMenu = function (tpl) {
|
||||||
|
return decorateObject(tpl, 'decorateMenu');
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.getDecoratedEnv = function (baseEnv) {
|
||||||
|
return decorateObject(baseEnv, 'decorateEnv');
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.getDecoratedConfig = function () {
|
exports.getDecoratedConfig = function () {
|
||||||
let decorated = config.getConfig();
|
const baseConfig = config.getConfig();
|
||||||
modules.forEach((plugin) => {
|
return decorateObject(baseConfig, 'decorateConfig');
|
||||||
if (plugin.decorateConfig) {
|
|
||||||
const res = plugin.decorateConfig(decorated);
|
|
||||||
if (res && 'object' === typeof res) {
|
|
||||||
decorated = res;
|
|
||||||
} else {
|
|
||||||
notify('Plugin error!', `"${plugin._name}": invalid return type for \`decorateConfig\``);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return decorated;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.getDecoratedBrowserOptions = function (defaults) {
|
exports.getDecoratedBrowserOptions = function (defaults) {
|
||||||
let decorated = defaults;
|
return decorateObject(defaults, 'decorateBrowserOptions');
|
||||||
modules.forEach((plugin) => {
|
|
||||||
if (plugin.decorateBrowserOptions) {
|
|
||||||
const res = plugin.decorateBrowserOptions(decorated);
|
|
||||||
if (res && 'object' === typeof res) {
|
|
||||||
decorated = res;
|
|
||||||
} else {
|
|
||||||
notify('Plugin error!', `"${plugin._name}": invalid return type for \`decorateBrowserOptions\``);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return decorated;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ const { app } = require('electron');
|
||||||
const { EventEmitter } = require('events');
|
const { EventEmitter } = require('events');
|
||||||
const { exec } = require('child_process');
|
const { exec } = require('child_process');
|
||||||
const defaultShell = require('default-shell');
|
const defaultShell = require('default-shell');
|
||||||
|
const { getDecoratedEnv } = require('./plugins');
|
||||||
const { productName, version } = require('./package');
|
const { productName, version } = require('./package');
|
||||||
|
|
||||||
let spawn;
|
let spawn;
|
||||||
|
|
@ -22,16 +23,18 @@ module.exports = class Session extends EventEmitter {
|
||||||
|
|
||||||
constructor ({ rows, cols: columns, cwd, shell }) {
|
constructor ({ rows, cols: columns, cwd, shell }) {
|
||||||
super();
|
super();
|
||||||
|
const baseEnv = Object.assign({}, process.env, {
|
||||||
|
LANG: app.getLocale().replace('-', '_'),
|
||||||
|
TERM: 'xterm-256color',
|
||||||
|
TERM_PROGRAM: productName,
|
||||||
|
TERM_PROGRAM_VERSION: version
|
||||||
|
});
|
||||||
|
|
||||||
this.pty = spawn(shell || defaultShell, ['--login'], {
|
this.pty = spawn(shell || defaultShell, ['--login'], {
|
||||||
columns,
|
columns,
|
||||||
rows,
|
rows,
|
||||||
cwd,
|
cwd,
|
||||||
env: Object.assign({}, process.env, {
|
env: getDecoratedEnv(baseEnv)
|
||||||
LANG: app.getLocale(),
|
|
||||||
TERM: 'xterm-256color',
|
|
||||||
TERM_PROGRAM: productName,
|
|
||||||
TERM_PROGRAM_VERSION: version
|
|
||||||
})
|
|
||||||
});
|
});
|
||||||
|
|
||||||
this.pty.stdout.on('data', (data) => {
|
this.pty.stdout.on('data', (data) => {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue