|
9 | 9 | #include "ll/api/chrono/GameChrono.h" |
10 | 10 | #include "ll/api/coro/CoroTask.h" |
11 | 11 | #include "ll/api/io/FileUtils.h" |
| 12 | +#include "ll/api/io/LogLevel.h" |
12 | 13 | #include "ll/api/service/GamingStatus.h" |
13 | 14 | #include "ll/api/service/ServerInfo.h" |
14 | 15 | #include "ll/api/thread/ServerThreadExecutor.h" |
@@ -162,16 +163,18 @@ bool loadPluginCode(script::ScriptEngine* engine, std::string entryScriptPath, s |
162 | 163 | if (esm) { |
163 | 164 | compiler += fmt::format( |
164 | 165 | R"( |
165 | | - import('url').then(url => {{ |
166 | | - const moduleUrl = url.pathToFileURL('{1}').href; |
167 | | - import(moduleUrl).catch(error => {{ |
168 | | - console.error('Failed to load ESM module:', error); |
| 166 | + Promise.all([import("url"), import("util")]) |
| 167 | + .then(([url, util]) => {{ |
| 168 | + const moduleUrl = url.pathToFileURL("{1}").href; |
| 169 | + import(moduleUrl).catch((error) => {{ |
| 170 | + logger.error(`Failed to load ESM module: `, util.inspect(error)); |
| 171 | + process.exit(1); |
| 172 | + }}); |
| 173 | + }}) |
| 174 | + .catch((error) => {{ |
| 175 | + console.error(`Failed to import "url" or "util" module:`, error); |
169 | 176 | process.exit(1); |
170 | 177 | }}); |
171 | | - }}).catch(error => {{ |
172 | | - console.error('Failed to import url module:', error); |
173 | | - process.exit(1); |
174 | | - }}); |
175 | 178 | )", |
176 | 179 | pluginDirPath, |
177 | 180 | entryScriptPath |
@@ -206,10 +209,19 @@ bool loadPluginCode(script::ScriptEngine* engine, std::string entryScriptPath, s |
206 | 209 | } |
207 | 210 |
|
208 | 211 | // Set exit handler |
209 | | - node::SetProcessExitHandler(env, [](node::Environment* env_, int exit_code) { stopEngine(getEngine(env_)); }); |
| 212 | + node::SetProcessExitHandler(env, [](node::Environment* env_, int exit_code) { |
| 213 | + auto engine = getEngine(env_); |
| 214 | + lse::LegacyScriptEngine::getInstance().getSelf().getLogger().log( |
| 215 | + exit_code == 0 ? ll::io::LogLevel::Debug : ll::io::LogLevel::Error, |
| 216 | + "NodeJs plugin {} exited with code {}.", |
| 217 | + getEngineData(engine)->pluginName, |
| 218 | + exit_code |
| 219 | + ); |
| 220 | + stopEngine(engine); |
| 221 | + }); |
210 | 222 |
|
211 | 223 | // Load code |
212 | | - MaybeLocal<v8::Value> loadenv_ret = node::LoadEnvironment(env, compiler.c_str()); |
| 224 | + MaybeLocal<v8::Value> loadenv_ret = node::LoadEnvironment(env, compiler); |
213 | 225 | if (loadenv_ret.IsEmpty()) // There has been a JS exception. |
214 | 226 | { |
215 | 227 | node::Stop(env); |
@@ -278,10 +290,6 @@ bool stopEngine(node::Environment* env) { |
278 | 290 | } |
279 | 291 |
|
280 | 292 | bool stopEngine(script::ScriptEngine* engine) { |
281 | | - lse::LegacyScriptEngine::getInstance().getSelf().getLogger().info( |
282 | | - "NodeJs plugin {} exited.", |
283 | | - getEngineData(engine)->pluginName |
284 | | - ); |
285 | 293 | auto env = NodeJsHelper::getEnvironmentOf(engine); |
286 | 294 | return stopEngine(env); |
287 | 295 | } |
@@ -324,7 +332,7 @@ std::string getPluginPackageName(const std::string& dirPath) { |
324 | 332 | std::ifstream file(ll::string_utils::u8str2str(packageFilePath.make_preferred().u8string())); |
325 | 333 | nlohmann::json j; |
326 | 334 | file >> j; |
327 | | - std::string packageName = ""; |
| 335 | + std::string packageName{}; |
328 | 336 | if (j.contains("name")) { |
329 | 337 | packageName = j["name"].get<std::string>(); |
330 | 338 | } |
@@ -385,7 +393,7 @@ bool processConsoleNpmCmd(const std::string& cmd) { |
385 | 393 | #endif |
386 | 394 | } |
387 | 395 |
|
388 | | -int executeNpmCommand(std::string cmd, std::string workingDir) { |
| 396 | +int executeNpmCommand(const std::string& cmd, std::string workingDir) { |
389 | 397 | if (!nodeJsInited && !initNodeJs()) { |
390 | 398 | return -1; |
391 | 399 | } |
@@ -431,7 +439,7 @@ int executeNpmCommand(std::string cmd, std::string workingDir) { |
431 | 439 |
|
432 | 440 | try { |
433 | 441 | node::SetProcessExitHandler(env, [&](node::Environment* env_, int exit_code) { node::Stop(env); }); |
434 | | - MaybeLocal<v8::Value> loadenv_ret = node::LoadEnvironment(env, executeJs.c_str()); |
| 442 | + MaybeLocal<v8::Value> loadenv_ret = node::LoadEnvironment(env, executeJs); |
435 | 443 | if (loadenv_ret.IsEmpty()) // There has been a JS exception. |
436 | 444 | throw "error"; |
437 | 445 | exit_code = node::SpinEventLoop(env).FromMaybe(0); |
|
0 commit comments