Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 1x 4x 1x 5x 4x 1x 5x 5x 1x 4x 1x 1x 4x 4x 5x 1x 7x 7x 7x 1x 6x 1x 5x 4x 1x 8x 8x 8x 7x 4x 5x | import * as path from 'node:path';
import {ERRORS} from '@grnsft/if-core/utils';
import {logger} from '../util/logger';
import {memoizedLog} from '../util/log-memoize';
import {pluginStorage} from '../util/plugin-storage';
import {CONFIG, STRINGS} from '../config';
import {PluginInterface} from '../types/interface';
import {GlobalPlugins, PluginOptions} from '../types/manifest';
import {PluginStorageInterface} from '../types/plugin-storage';
const {
PluginInitializationError,
MissingPluginMethodError,
MissingPluginPathError,
} = ERRORS;
const {GITHUB_PATH, NATIVE_PLUGIN} = CONFIG;
const {
MISSING_METHOD,
MISSING_PATH,
NOT_NATIVE_PLUGIN,
INVALID_MODULE_PATH,
LOADING_PLUGIN_FROM_PATH,
INITIALIZING_PLUGIN,
INITIALIZING_PLUGINS,
} = STRINGS;
/**
* Imports module by given `path`.
*/
const importModuleFrom = async (path: string) => {
const module = await import(path).catch(error => {
throw new PluginInitializationError(INVALID_MODULE_PATH(path, error));
});
return module;
};
/**
* Imports `module` from given `path`, then checks if it's `ModelPluginInterface` extension.
*/
const importAndVerifyModule = async (method: string, path: string) => {
const pluginModule = await importModuleFrom(path);
return pluginModule[method];
};
/**
* Checks if plugin is missing then rejects with error.
* Then checks if `path` is starting with github, then grabs the repository name.
* Imports module, then checks if it's a valid plugin.
*/
const handModule = (method: string, pluginPath: string) => {
console.debug(LOADING_PLUGIN_FROM_PATH(method, pluginPath));
if (pluginPath === 'builtin') {
pluginPath = path.normalize(`${__dirname}/../builtins`);
} else {
if (pluginPath?.startsWith(GITHUB_PATH)) {
const parts = pluginPath.split('/');
pluginPath = parts[parts.length - 1];
}
if (!pluginPath.includes(NATIVE_PLUGIN)) {
memoizedLog(logger.warn, NOT_NATIVE_PLUGIN(pluginPath));
}
}
return importAndVerifyModule(method, pluginPath);
};
/**
* Initializes plugin with global config.
*/
const initPlugin = async (
initPluginParams: PluginOptions
): Promise<PluginInterface> => {
const {method, path, 'global-config': globalConfig} = initPluginParams;
console.debug(INITIALIZING_PLUGIN(method));
if (!method) {
throw new MissingPluginMethodError(MISSING_METHOD);
}
if (!path) {
throw new MissingPluginPathError(MISSING_PATH);
}
const plugin = await handModule(method, path);
return plugin(globalConfig);
};
/**
* Registers all plugins from `manifest`.`initialize` property.
*/
export const initialize = async (
plugins: GlobalPlugins
): Promise<PluginStorageInterface> => {
console.debug(INITIALIZING_PLUGINS);
const storage = pluginStorage();
for await (const pluginName of Object.keys(plugins)) {
const plugin = await initPlugin(plugins[pluginName]);
storage.set(pluginName, plugin);
}
return storage;
};
|