Skip to content

Commit 565011c

Browse files
committed
chore: refactor npm command, revert event commit
1 parent 2d0b4e8 commit 565011c

File tree

2 files changed

+32
-66
lines changed

2 files changed

+32
-66
lines changed

src/legacy/api/EventAPI.cpp

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,11 @@ bool hasListened[int(EVENT_TYPES::EVENT_COUNT)] = {false};
7878

7979
//////////////////// APIs ////////////////////
8080

81-
Local<Value> listenCancellable(ScriptEngine* engine, const string& eventName, const Local<Function>& func);
8281
Local<Value> McClass::listen(const Arguments& args) {
8382
CHECK_ARGS_COUNT(args, 2);
8483
CHECK_ARG_TYPE(args[0], ValueKind::kString);
8584
CHECK_ARG_TYPE(args[1], ValueKind::kFunction);
8685

87-
if (args.size() >= 3 && args[2].isBoolean() && args[2].asBoolean().value()) {
88-
return listenCancellable(EngineScope::currentEngine(), args[0].asString().toString(), args[1].asFunction());
89-
}
9086
try {
9187
return Boolean::newBoolean(
9288
LLSEAddEventListener(EngineScope::currentEngine(), args[0].asString().toString(), args[1].asFunction())
@@ -95,29 +91,6 @@ Local<Value> McClass::listen(const Arguments& args) {
9591
CATCH("Fail to Bind Listener!");
9692
}
9793

98-
Local<Value> listenCancellable(ScriptEngine* engine, const string& eventName, const Local<Function>& func) {
99-
try {
100-
auto event_enum = magic_enum::enum_cast<EVENT_TYPES>(eventName);
101-
auto eventId = int(event_enum.value());
102-
auto event =
103-
listenerList[eventId].insert(listenerList[eventId].end(), {engine, script::Global<Function>(func)});
104-
if (!hasListened[eventId]) {
105-
hasListened[eventId] = true;
106-
EnableEventListener(eventId);
107-
}
108-
return Function::newFunction([eventId{eventId}, event{event}](const Arguments&) -> Local<Value> {
109-
listenerList[eventId].erase(event);
110-
return Local<Value>();
111-
});
112-
} catch (...) {
113-
lse::LegacyScriptEngine::getInstance().getSelf().getLogger().error("Event {} not found!"_tr(eventName));
114-
lse::LegacyScriptEngine::getInstance().getSelf().getLogger().error(
115-
"In Plugin: " + getEngineData(engine)->pluginName
116-
);
117-
return Local<Value>();
118-
}
119-
}
120-
12194
//////////////////// Funcs ////////////////////
12295

12396
bool LLSEAddEventListener(ScriptEngine* engine, const string& eventName, const Local<Function>& func) {

src/legacy/main/NodeJsHelper.cpp

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,7 @@ int executeNpmCommand(std::vector<std::string> npmArgs, std::string workingDir)
516516
std::string engineDir =
517517
ll::string_utils::u8str2str(lse::LegacyScriptEngine::getInstance().getSelf().getModDir().u8string());
518518
if (workingDir.empty()) workingDir = engineDir;
519+
workingDir = ll::string_utils::replaceAll(workingDir, "\\", "/");
519520

520521
auto npmPath = std::filesystem::absolute(engineDir) / "node_modules" / "npm" / "bin" / "npm-cli.js";
521522
std::vector<std::string>& env_args = npmArgs;
@@ -526,7 +527,7 @@ int executeNpmCommand(std::vector<std::string> npmArgs, std::string workingDir)
526527
env_args.erase(env_args.begin());
527528
}
528529
auto scriptPath = ll::string_utils::replaceAll(ll::string_utils::u8str2str(npmPath.u8string()), "\\", "/");
529-
env_args.insert(env_args.begin(), {args[0], scriptPath});
530+
env_args.insert(env_args.begin(), {args[0], scriptPath, engineDir, workingDir});
530531

531532
std::vector<std::string> errors;
532533

@@ -553,51 +554,43 @@ int executeNpmCommand(std::vector<std::string> npmArgs, std::string workingDir)
553554
node::Environment* env = setup->env();
554555
int exit_code = 0;
555556

556-
// Process workingDir
557-
workingDir = ll::string_utils::replaceAll(workingDir, "\\", "/");
558-
559557
{
560558
using namespace v8;
561559
v8::Locker locker(isolate);
562560
v8::Isolate::Scope isolate_scope(isolate);
563561
v8::HandleScope handle_scope(isolate);
564562
v8::Context::Scope context_scope(setup->context());
565563

566-
std::string executeJs = fmt::format(
567-
R"(
568-
const engineDir = require("path").resolve("{0}") + require("path").sep;
569-
const workingDir = "{1}";
570-
const scriptPath = "{2}";
571-
const publicRequire = require("module").createRequire(engineDir);
572-
// Record states and restore at exit
573-
const oldCwd = process.cwd();
574-
const oldEnv = Object.entries(process.env).filter(([k]) => k.startsWith("npm_"));
575-
const oldTitle = process.title;
576-
process.once("exit", () => {{
577-
Object.keys(process.env)
578-
.filter((k) => k.startsWith("npm_"))
579-
.forEach((k) => delete process.env[k]);
580-
oldEnv.forEach(([k, v]) => (process.env[k] = v));
581-
process.title = oldTitle;
582-
process.chdir(oldCwd);
583-
}});
584-
// disable npm input
585-
function inputHandler(type, resolve, reject) {{
586-
if (type === "read") {{
587-
console.error("Input is not allow in server command.");
588-
reject();
589-
}}
590-
}}
591-
process.on("input", inputHandler);
592-
process.once("exit", () => process.off("input", inputHandler));
593-
594-
process.chdir(workingDir);
595-
publicRequire(scriptPath);
596-
)",
597-
engineDir,
598-
workingDir,
599-
scriptPath
600-
);
564+
std::string executeJs = R"(
565+
const path = require("path");
566+
const util = require("util");
567+
const [root, cwd] = util.parseArgs({ strict: false }).positionals;
568+
const entry = process.argv[1];
569+
process.argv.splice(process.argv.findIndex((a) => a == root), 1);
570+
process.argv.splice(process.argv.findIndex((a) => a == cwd), 1);
571+
const publicRequire = require("module").createRequire(path.resolve(root ?? process.cwd()) + path.sep);
572+
// disable npm input
573+
process.stdin.destroy();
574+
function inputHandler(type) {
575+
if (type === "read")
576+
throw "Input is not allow in server command.";
577+
}
578+
process.on("input", inputHandler);
579+
process.once("exit", () => process.off("input", inputHandler));
580+
// keep env
581+
const modifiedEnv = {};
582+
process.env = new Proxy(process.env, {
583+
get: (target, k) => modifiedEnv[k] ?? target[k],
584+
set: (_, k, v) => (modifiedEnv[k] = v),
585+
});
586+
let fakeCwd = path.resolve(cwd ?? root ?? process.cwd());
587+
process.chdir = (cd) => (fakeCwd = path.resolve(fakeCwd, cd));
588+
process.cwd = () => fakeCwd;
589+
Object.defineProperties(process, {
590+
title: { get: () => "", set: () => true },
591+
});
592+
publicRequire(entry);
593+
)";
601594

602595
try {
603596
node::SetProcessExitHandler(env, [&](node::Environment*, int exit_code_) {

0 commit comments

Comments
 (0)