@@ -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