Skip to content

Commit c64ef6f

Browse files
committed
fix: fix compatibility with custom mods directory
1 parent 07742ca commit c64ef6f

File tree

4 files changed

+30
-15
lines changed

4 files changed

+30
-15
lines changed

src/baselib/BaseLib.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ SOFTWARE.
2828

2929
let coreModules = new Map([]);
3030

31-
let basePath = "plugins/";
31+
let basePath = ll.pluginsRoot;
32+
if(!basePath.endsWith("/")) basePath += "/";
3233

3334
let utilPath = {
3435
normalize(path) {
@@ -135,9 +136,9 @@ SOFTWARE.
135136
throw new Error("cannot require file out of root dir");
136137
}
137138
path = path.replace(/^\//, "");
138-
let cjsSelf = "plugins/cjs.js";
139+
let cjsSelf = basePath + "lib/BaseLib.js";
139140
if (path === cjsSelf) {
140-
throw new Error("cjs.js is trying to load itself");
141+
throw new Error("BaseLib.js is trying to load itself");
141142
}
142143
requestPaths.push(path);
143144
let content = file.readFrom(path);
@@ -173,7 +174,7 @@ SOFTWARE.
173174
}
174175
result = tryFile(`${id}.json`);
175176
if (result != undefined) {
176-
throw new Error(`cannot require a JSON file ${id}.mjs`);
177+
throw new Error(`cannot require a JSON file ${id}.json`);
177178
}
178179
result = tryFile(`${id}.mjs`);
179180
if (result != undefined) {
@@ -194,7 +195,7 @@ SOFTWARE.
194195
}
195196
result = tryFile(utilPath.join(id, "index.json"));
196197
if (result != undefined) {
197-
throw new Error(`cannot require a JSON file ${id}.mjs`);
198+
throw new Error(`cannot require a JSON file ${id}.json`);
198199
}
199200
result = tryFile(utilPath.join(id, "index.mjs"));
200201
if (result != undefined) {
@@ -229,7 +230,7 @@ SOFTWARE.
229230
let dirs = node_modules_paths(start);
230231
let result;
231232
for (let dir of dirs) {
232-
result = loadPackagExports(utilPath.join(dir, id));
233+
result = loadPackageExports(utilPath.join(dir, id));
233234
if (result != undefined) {
234235
return result;
235236
}
@@ -271,7 +272,7 @@ SOFTWARE.
271272
return undefined;
272273
}
273274

274-
function loadPackagExports(id) {
275+
function loadPackageExports(id) {
275276
//TODO
276277
let package = file.readFrom(utilPath.join(id, "package.json"));
277278
if (package == undefined) {

src/baselib/BaseLib.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- ---------------------
22
-- For require
33
-- ---------------------
4-
package.path = "plugins/lib/?.lua;" .. package.path
4+
package.path = ll.pluginsRoot:match("^(.-)/?$") .. "/lib/?.lua;" .. package.path
55

66

77
-- ---------------------

src/legacy/main/NodeJsHelper.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,9 @@ int executeNpmCommand(const std::string& cmd, std::string workingDir) {
397397
if (!nodeJsInited && !initNodeJs()) {
398398
return -1;
399399
}
400+
std::string engineDir =
401+
ll::string_utils::u8str2str(lse::LegacyScriptEngine::getInstance().getSelf().getModDir().u8string());
402+
if (workingDir.empty()) workingDir = engineDir;
400403
std::vector<std::string> errors;
401404
std::unique_ptr<node::CommonEnvironmentSetup> setup = node::CommonEnvironmentSetup::Create(
402405
platform.get(),
@@ -430,12 +433,23 @@ int executeNpmCommand(const std::string& cmd, std::string workingDir) {
430433
v8::HandleScope handle_scope(isolate);
431434
v8::Context::Scope context_scope(setup->context());
432435

433-
string executeJs = "const oldCwd = process.cwd();"
434-
"const publicRequire = require('module').createRequire(oldCwd + "
435-
"'/plugins/legacy-script-engine-nodejs/');"
436-
"require('process').chdir('"
437-
+ workingDir + "');" + "publicRequire('npm-js-interface')('" + cmd + "');"
438-
+ "require('process').chdir(oldCwd);";
436+
std::string executeJs = fmt::format(
437+
R"(
438+
const engineDir = "{0}";
439+
const workingDir = "{1}";
440+
const command = "{2}";
441+
const oldCwd = process.cwd();
442+
const publicRequire = require("module").createRequire(
443+
require("path").resolve(engineDir) + require("path").sep
444+
);
445+
process.chdir(workingDir);
446+
publicRequire("npm-js-interface")(command);
447+
process.chdir(oldCwd);
448+
)",
449+
engineDir.erase(0,2)+"/",
450+
workingDir,
451+
cmd
452+
);
439453

440454
try {
441455
node::SetProcessExitHandler(env, [&](node::Environment* env_, int exit_code) { node::Stop(env); });

src/legacy/main/NodeJsHelper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ bool doesPluginPackHasDependency(const std::string& dirPath);
3030
bool isESModulesSystem(const std::string& dirPath);
3131

3232
bool processConsoleNpmCmd(const std::string& cmd);
33-
int executeNpmCommand(const std::string& cmd, std::string workingDir = LLSE_NPM_EXECUTE_PATH);
33+
int executeNpmCommand(const std::string& cmd, std::string workingDir = "");
3434

3535
} // namespace NodeJsHelper

0 commit comments

Comments
 (0)