remove getAddonByPath caller and add its functionality optionally to getCaller

pull/67/head
dperolio 3 years ago
parent d3b9c04e30
commit df0a4dcf2b
No known key found for this signature in database
GPG Key ID: 3E9BBAA710D3DDCE

@ -148,7 +148,7 @@ export default class Actions extends API {
getActionsByCaller (addonId) {
try {
assertString(addonId);
return this._actions.filter(action => action.caller === addonId);
return this._actions.filter(action => action.caller?.id === addonId);
} catch (err) {
return this.error(this._labels.concat('getActionsByCaller'), err);
}
@ -193,7 +193,7 @@ export default class Actions extends API {
unregisterActionsByCaller (addonId) {
try {
assertString(addonId);
this._actions = this._actions.filter(action => action.caller !== addonId);
this._actions = this._actions.filter(action => action.caller?.id !== addonId);
this.emit(Events.VIZALITY_ACTION_REMOVE_ALL_BY_CALLER, addonId);
} catch (err) {
return this.error(this._labels.concat('unregisterActionsByCaller'), err);

@ -218,7 +218,7 @@ export default class Commands extends API {
getCommandsByCaller (addonId) {
try {
assertString(addonId);
return commands.filter(command => command.caller === addonId);
return commands.filter(command => command.caller?.id === addonId);
} catch (err) {
return this.error(this._labels.concat('getCommandsByCaller'), err);
}
@ -262,7 +262,7 @@ export default class Commands extends API {
unregisterCommandsByCaller (addonId) {
try {
assertString(addonId);
commands = commands.filter(command => command.caller !== addonId);
commands = commands.filter(command => command.caller?.id !== addonId);
this.emit(Events.VIZALITY_COMMAND_REMOVE_ALL_BY_CALLER, addonId);
} catch (err) {
return this.error(this._labels.concat('unregisterCommandsByCaller'), err);

@ -62,7 +62,7 @@ export default class Keybinds extends API {
try {
assertObject(keybind);
keybind.caller = getCaller();
keybind.id = keybind.id || `${toSnakeCase(keybind.caller).toUpperCase()}_KEYBIND_${this.getKeybindsByCaller(keybind.caller)?.length + 1 || '1'}`;
keybind.id = keybind.id || `${toSnakeCase(keybind.caller?.id).toUpperCase()}_KEYBIND_${this.getKeybindsByCaller(keybind.caller?.id)?.length + 1 || '1'}`;
assertString(keybind.id);
if (this.isKeybind(keybind.id)) {
throw new Error(`Keybind "${keybind.id}" is already registered!`);
@ -220,7 +220,7 @@ export default class Keybinds extends API {
getKeybindsByCaller (addonId) {
try {
assertString(addonId);
return keybinds.filter(keybind => keybind.caller === addonId);
return keybinds.filter(keybind => keybind.caller?.id === addonId);
} catch (err) {
return this.error(this._labels.concat('getKeybindsByCaller'), err);
}
@ -307,7 +307,7 @@ export default class Keybinds extends API {
unregisterKeybindsByCaller (addonId) {
try {
assertString(addonId);
keybinds = keybinds.filter(keybind => keybind.caller !== addonId);
keybinds = keybinds.filter(keybind => keybind.caller?.id !== addonId);
this.emit(Events.VIZALITY_KEYBIND_REMOVE_ALL_BY_CALLER, addonId);
} catch (err) {
return this.error(this._labels.concat('unregisterKeybindsByCaller'), err);

@ -111,7 +111,7 @@ export default class Notifications extends API {
throw new Error(`Notice "${notice.id}" is already active!`);
}
} else {
notice.id = `${toSnakeCase(notice.caller).toUpperCase()}_NOTICE_${this.getNoticesByCaller(notice.caller)?.length + 1 || '1'}`;
notice.id = `${toSnakeCase(notice.caller?.id).toUpperCase()}_NOTICE_${this.getNoticesByCaller(notice.caller?.id)?.length + 1 || '1'}`;
}
if (notice.callback && typeof notice.callback !== 'function') {
throw new TypeError('Notice "callback" property value must be a function!');
@ -162,7 +162,7 @@ export default class Notifications extends API {
throw new Error(`Toast "${toast.id}" already exists!`);
}
} else {
toast.id = `${toSnakeCase(toast.caller).toUpperCase()}_TOAST_${this.getToastsByCaller(toast.caller)?.length + 1 || '1'}`;
toast.id = `${toSnakeCase(toast.caller?.id).toUpperCase()}_TOAST_${this.getToastsByCaller(toast.caller?.id)?.length + 1 || '1'}`;
}
if (toast.callback && typeof toast.callback !== 'function') {
throw new TypeError('Toast "callback" property value must be a function!');
@ -427,7 +427,7 @@ export default class Notifications extends API {
getNoticesByCaller (addonId) {
try {
assertString(addonId);
return notifications.notices.filter(notice => notice.caller === addonId);
return notifications.notices.filter(notice => notice.caller?.id === addonId);
} catch (err) {
return this.error(this._labels.concat('getNoticesByCaller'), err);
}
@ -441,7 +441,7 @@ export default class Notifications extends API {
getToastsByCaller (addonId) {
try {
assertString(addonId);
return notifications.toasts.filter(toast => toast.caller === addonId);
return notifications.toasts.filter(toast => toast.caller?.id === addonId);
} catch (err) {
return this.error(this._labels.concat('getToastsByCaller'), err);
}
@ -564,7 +564,7 @@ export default class Notifications extends API {
try {
assertString(addonId);
notifications.notices
.filter(notice => notice.caller === addonId)
.filter(notice => notice.caller?.id === addonId)
.forEach(notice => this.closeNotice(notice.id));
this.emit(Events.VIZALITY_NOTICE_REMOVE_ALL_BY_CALLER, addonId);
} catch (err) {
@ -581,7 +581,7 @@ export default class Notifications extends API {
try {
assertString(addonId);
notifications.toasts
.filter(toast => toast.caller === addonId)
.filter(toast => toast.caller?.id === addonId)
.forEach(toast => this.closeToast(toast.id));
this.emit(Events.VIZALITY_TOAST_REMOVE_ALL_BY_CALLER, addonId);
} catch (err) {

@ -215,7 +215,7 @@ export default class Patches extends API {
*/
getPatchesByAddon (addonId) {
try {
return patches.filter(patch => patch.caller === addonId);
return patches.filter(patch => patch.caller?.id === addonId);
} catch (err) {
return _error(_labels.concat('getPatchesByAddon'), err);
}
@ -250,7 +250,7 @@ export default class Patches extends API {
*/
unpatchAllByAddon (addonId) {
try {
patches = patches.filter(patch => patch.caller !== addonId);
patches = patches.filter(patch => patch.caller?.id !== addonId);
} catch (err) {
return _error(_labels.concat('unpatchAllByAddon'), err);
}

@ -212,7 +212,7 @@ export default class Settings extends API {
*/
_registerBuiltinSection (props) {
try {
const addonId = getCaller();
const addonId = getCaller()?.id;
const { header, description, icon, render } = props;
const builtin = vizality.manager.builtins.get(addonId);
builtin.sections.settings = props;
@ -280,6 +280,7 @@ export default class Settings extends API {
};
return [ settingValue, setSetting ];
} catch (err) {
console.log('this-out', this);
return this.error(err);
}
}
@ -293,7 +294,7 @@ export default class Settings extends API {
/**
* If no addonId is provided, try to use getCaller to determine one.
*/
addonId = addonId || getCaller();
addonId = addonId || getCaller()?.id;
/**
* If the addonId is vizality, it was most likely returned from getCaller
* and is used in some core area, so we want to use the core Vizality settings.json

@ -122,10 +122,10 @@ export default class Commands extends Builtin {
vizalitySectionInner.push(renderHeader('', null, 'Vizality', `vz-asset://images/logo.png`, `vz-cmd-vizality`));
results.forEach(result => {
const { command } = findInReactTree(result, r => r.command);
if (!callers.includes(command.caller) && command.caller !== 'vizality' && !Vizality.BUILTINS.includes(command.caller)) {
if (!callers.includes(command.caller) && command.caller.id !== 'vizality' && command.caller.type !== 'builtin') {
callers.push(command.caller);
}
if (command.caller === 'vizality' || Vizality.BUILTINS.includes(command.caller)) {
if (command.caller === 'vizality' || command.caller.type === 'builtin') {
vizalitySectionInner.push(result);
}
});

@ -8,7 +8,7 @@
*/
import { getConfig, statusMatrix } from 'isomorphic-git';
import { getAddonByPath } from '@vizality/util/file';
import { getCaller } from '@vizality/util/file';
import http from 'isomorphic-git/http/node';
import { promisify } from 'util';
import { existsSync } from 'fs';
@ -40,7 +40,7 @@ export default class Updatable extends Events {
}
this.path = join(this.dir, this.addonId);
if (!updateIdentifier) {
updateIdentifier = `${getAddonByPath(this.path)?.type}_${this.addonId}`;
updateIdentifier = `${getCaller(this.path)?.type}_${this.addonId}`;
}
this._updateIdentifier = updateIdentifier;
}

@ -22,7 +22,7 @@ export default memo(props => {
<FormItem className={joinClassNames('vz-settings-item', 'vz-settings-button-item', flex, marginBottom20)}>
<div
className='vz-settings-button-item-inner'
id={id || `setting-button-${toKebabCase(getCaller())}-${children.toString()}`}
id={id || `setting-button-${toKebabCase(getCaller()?.id)}-${children.toString()}`}
>
<div className='vz-settings-button-item-info'>
<div className={labelRow}>

@ -64,7 +64,7 @@ export default memo(({ separator = true, description, requiresRestart, info, swi
>
<div
className='vz-settings-switch-item-inner'
id={id || `setting-switch-${toKebabCase(getCaller())}-${children.toString()}`}
id={id || `setting-switch-${toKebabCase(getCaller()?.id)}-${children.toString()}`}
>
{children}
{info && (

@ -14,7 +14,7 @@ export function runPatches (patches, type, returnValue, _this, args) {
if (typeof tempReturn !== 'undefined') returnValue = tempReturn;
} catch (err) {
error({ labels: _labels.concat('runPatch'), message: [ `Failed to run ${type} callback for ${patch.caller}:\n`, err ] });
error({ labels: _labels.concat('runPatch'), message: [ `Failed to run ${type} callback for ${patch.caller?.id}:\n`, err ] });
patch.errorsOccurred++;
}
}
@ -73,7 +73,7 @@ export function createPatch (caller, moduleToPatch, functionName) {
}
export function patch (...args) {
if (typeof args[0] !== 'string') args.unshift(getCaller());
if (typeof args[0] !== 'string') args.unshift(getCaller()?.id);
let [ id, moduleToPatch, func, patchFunction, type = 'after', { failSave = true } = {} ] = args;
if (typeof type === 'boolean') {
@ -121,9 +121,9 @@ export function patch (...args) {
}
export function getPatchesByCaller (caller = getCaller()) {
export function getPatchesByCaller (caller = getCaller()?.id) {
const found = [];
for (const patch of patches) for (const child of patch.childs) if (child.caller === caller) found.push(child);
for (const patch of patches) for (const child of patch.childs) if (child.caller?.id === caller) found.push(child);
return found;
}

@ -10,8 +10,10 @@ import { promises, existsSync, lstatSync, readFileSync, readdirSync } from 'fs';
import { lookup as _getMimeType } from 'mime-types';
import { extname, join, parse } from 'path';
import { escapeRegExp } from 'lodash';
import { get } from '@vizality/http';
import imageSize from 'image-size';
import { promisify } from 'util';
import { log, warn, error } from './Logger';
import { Directories } from '../constants';
import { isString } from './String';
@ -25,52 +27,64 @@ const _log = (labels, ...message) => log({ labels, message });
const _warn = (labels, ...message) => warn({ labels, message });
const _error = (labels, ...message) => error({ labels, message });
export const getCaller = () => {
try {
const stackTrace = (new Error()).stack;
const plugin = stackTrace.match(new RegExp(`${escapeRegExp(Directories.PLUGINS)}.([-\\w]+)`));
if (plugin) {
return plugin[1];
}
const builtin = stackTrace.match(new RegExp(`${escapeRegExp(Directories.BUILTINS)}.([-\\w]+)`));
if (builtin) {
return builtin[1];
}
return 'vizality';
} catch (err) {
_error(_labels.concat('getCaller'), err);
}
};
export const getAddonByPath = path => {
export const getCaller = path => {
try {
const plugin = path.match(new RegExp(`${escapeRegExp(Directories.PLUGINS)}.([-\\w]+)`));
if (plugin) {
return {
type: 'plugin',
id: plugin[1]
};
}
const builtin = path.match(new RegExp(`${escapeRegExp(Directories.BUILTINS)}.([-\\w]+)`));
if (builtin) {
if (path) {
const plugin = path.match(new RegExp(`${escapeRegExp(Directories.PLUGINS)}.([-\\w]+)`));
if (plugin) {
return {
type: 'plugin',
id: plugin[1]
};
}
const builtin = path.match(new RegExp(`${escapeRegExp(Directories.BUILTINS)}.([-\\w]+)`));
if (builtin) {
return {
type: 'builtin',
id: builtin[1]
};
}
const theme = path.match(new RegExp(`${escapeRegExp(Directories.THEMES)}.([-\\w]+)`));
if (theme) {
return {
type: 'theme',
id: theme[1]
};
}
return {
type: 'builtin',
id: builtin[1]
type: 'core',
id: 'vizality'
};
}
const theme = path.match(new RegExp(`${escapeRegExp(Directories.THEMES)}.([-\\w]+)`));
if (theme) {
} else {
const stackTrace = (new Error()).stack;
const plugin = stackTrace.match(new RegExp(`${escapeRegExp(Directories.PLUGINS)}.([-\\w]+)`));
if (plugin) {
return {
type: 'plugin',
id: plugin[1]
};
}
const builtin = stackTrace.match(new RegExp(`${escapeRegExp(Directories.BUILTINS)}.([-\\w]+)`));
if (builtin) {
return {
type: 'builtin',
id: builtin[1]
};
}
const theme = stackTrace.match(new RegExp(`${escapeRegExp(Directories.THEMES)}.([-\\w]+)`));
if (theme) {
return {
type: 'theme',
id: theme[1]
};
}
return {
type: 'theme',
id: theme[1]
type: 'core',
id: 'vizality'
};
}
return {
type: 'core',
id: 'vizality'
};
} catch (err) {
_error(_labels.concat('getAddonByPath'), err);
_error(_labels.concat('getCaller'), err);
}
};
@ -80,7 +94,7 @@ export const getMimeType = async input => {
type = _getMimeType(input);
if (!type) {
type = await fetch(input).then(res => res.blob().then(blob => blob.type));
type = await get(input).then(res => res.blob().then(blob => blob.type));
}
if (!type) {
@ -148,7 +162,7 @@ export const getMediaDimensions = async (url, mimeType) => {
};
export const convertUrlToFile = (url, fileName) => {
return fetch(url)
return get(url)
.then(res => res.arrayBuffer())
.then(async buffer => new File([ buffer ], fileName, { type: await this.getMimeType(url) }));
};

Loading…
Cancel
Save