Skip to content

Commit 2995f2a

Browse files
committed
attempt to fix plugin load errors
1 parent 73d86fe commit 2995f2a

1 file changed

Lines changed: 22 additions & 19 deletions

File tree

src/shell/plugins.js

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import misc, { ss } from '#misc';
1111
//legacyshell: logging
1212
import log from 'puppylog';
1313
//
14+
import { createRequire } from 'module';
15+
const require = createRequire(import.meta.url);
16+
//
1417

1518
export class PluginManager {
1619
constructor(type) {
@@ -19,33 +22,30 @@ export class PluginManager {
1922
this.type = type || 'game';
2023
};
2124

22-
async retrievePluginAndDependencies(pluginFolder) {
23-
const out = {pluginFolder};
25+
async retrieveDependenciesCreatePluginObject(pluginFolder) {
26+
const pluginObject = {pluginFolder};
2427
const timeBeforeLoadFiles = Date.now();
2528

26-
const pluginPath = path.join(pluginFolder, 'index.js');
27-
if (fs.existsSync(pluginPath)) {
28-
const dependenciesPath = path.join(pluginFolder, 'dependencies.js');
29-
if (fs.existsSync(dependenciesPath)) {
30-
out.dependencies = await import(pathToFileURL(dependenciesPath).href);
31-
};
29+
const dependenciesPath = path.join(pluginFolder, 'dependencies.js');
30+
if (fs.existsSync(dependenciesPath)) {
31+
pluginObject.dependencies = await import(pathToFileURL(dependenciesPath).href);
32+
};
3233

33-
out.currentHash = execSync(`cd ${path.join(pluginFolder)} && git rev-parse HEAD`, { encoding: 'utf-8' }).trim();
34-
out.Plugin = await import(pathToFileURL(pluginPath).href);
34+
pluginObject.currentHash = execSync(`cd ${path.join(pluginFolder)} && git rev-parse HEAD`, { encoding: 'utf-8' }).trim();
3535

36-
out.timeToLoadFiles = Date.now() - timeBeforeLoadFiles;
36+
pluginObject.timeToLoadFiles = Date.now() - timeBeforeLoadFiles;
3737

38-
return out;
39-
} else return null;
38+
return pluginObject;
4039
};
4140

4241
async preloadPlugin(dirPath, pluginFolder) {
4342
try {
4443
log.boldGray("preloading plugin folder", pluginFolder);
45-
const pluginObject = await this.retrievePluginAndDependencies(dirPath);
44+
const pluginObject = await this.retrieveDependenciesCreatePluginObject(dirPath);
4645
if (!pluginObject) return;
4746

48-
const { currentHash, dependencies } = pluginObject.Plugin;
47+
const { currentHash } = pluginObject.currentHash;
48+
const { dependencies } = pluginObject?.dependencies || {};
4949
const timeBeforeDeps = Date.now();
5050

5151
if (currentHash) {
@@ -71,8 +71,11 @@ export class PluginManager {
7171

7272
let failed = false;
7373

74+
console.log(pluginFolder, "dependencies", dependencies, !!dependencies);
75+
7476
if (dependencies) {
7577
for (const [dependency, version] of Object.entries(dependencies)) {
78+
// console.log(`Checking dependency for plugin ${dirPath}:`, dependency, version);
7679
if (version === "plugin") {
7780
if (this.pluginsList.includes(dependency)) {
7881
log.green(`Plugin dependency ${dependency} found`);
@@ -82,10 +85,7 @@ export class PluginManager {
8285
};
8386
} else {
8487
try {
85-
const modulePath = path.join(ss.rootDir, 'node_modules', dependency);
86-
if (!fs.existsSync(modulePath)) {
87-
await import(dependency);
88-
};
88+
require.resolve(dependency);
8989
// log.dim(`${dependency} is already installed.`);
9090
} catch (error) {
9191
log.warning(`${dependency} is not installed. Attempting to install (${version})...`);
@@ -107,6 +107,9 @@ export class PluginManager {
107107
log.error(`Plugin ${dirPath} couldn't be loaded:\n${failed}`);
108108
return null;
109109
};
110+
111+
const pluginPath = path.join(dirPath, 'index.js');
112+
pluginObject.Plugin = await import(pathToFileURL(pluginPath).href);
110113

111114
pluginObject.timeToDoDeps = Date.now() - timeBeforeDeps;
112115

0 commit comments

Comments
 (0)