Skip to content

Commit 3706caf

Browse files
committed
fix: improve cross-platform compatibility for npm execution in CodeInjector
1 parent e0e5038 commit 3706caf

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

adminforth/modules/codeInjector.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,9 @@ class CodeInjector implements ICodeInjector {
116116
envOverrides?: { [key: string]: string }
117117
}) {
118118
const nodeBinary = process.execPath; // Path to the Node.js binary running this script
119-
const npmPath = path.join(path.dirname(nodeBinary), 'npm'); // Path to the npm executable
119+
// On Windows, npm is npm.cmd, on Unix systems it's npm
120+
const npmExecutable = process.platform === 'win32' ? 'npm.cmd' : 'npm';
121+
const npmPath = path.join(path.dirname(nodeBinary), npmExecutable); // Path to the npm executable
120122
const env = {
121123
VITE_ADMINFORTH_PUBLIC_PATH: this.adminforth.config.baseUrl,
122124
FORCE_COLOR: '1',
@@ -127,7 +129,12 @@ class CodeInjector implements ICodeInjector {
127129
console.log(`⚙️ exec: npm ${command}`);
128130
process.env.HEAVY_DEBUG && console.log(`🪲 npm ${command} cwd:`, cwd);
129131
process.env.HEAVY_DEBUG && console.time(`npm ${command} done in`);
130-
const { stdout: out, stderr: err } = await execAsync(`${nodeBinary} ${npmPath} ${command}`, {
132+
133+
// Quote paths that contain spaces (especially important on Windows)
134+
const quotedNodeBinary = nodeBinary.includes(' ') ? `"${nodeBinary}"` : nodeBinary;
135+
const quotedNpmPath = npmPath.includes(' ') ? `"${npmPath}"` : npmPath;
136+
137+
const { stdout: out, stderr: err } = await execAsync(`${quotedNodeBinary} ${quotedNpmPath} ${command}`, {
131138
cwd,
132139
env,
133140
});
@@ -872,17 +879,17 @@ class CodeInjector implements ICodeInjector {
872879
const command = 'run dev';
873880
console.log(`⚙️ spawn: npm ${command}...`);
874881
const nodeBinary = process.execPath;
875-
const npmPath = path.join(path.dirname(nodeBinary), 'npm');
882+
// On Windows, npm is npm.cmd, on Unix systems it's npm
883+
const npmExecutable = process.platform === 'win32' ? 'npm.cmd' : 'npm';
884+
const npmPath = path.join(path.dirname(nodeBinary), npmExecutable);
876885
const env = {
877886
VITE_ADMINFORTH_PUBLIC_PATH: this.adminforth.config.baseUrl,
878887
FORCE_COLOR: '1',
879888
...process.env,
880889
};
881890

882-
const devServer = spawn(`${nodeBinary}`, [`${npmPath}`, ...command.split(' ')], {
883-
cwd,
884-
env,
885-
});
891+
// Execute npm directly (npm.cmd on Windows, npm on Unix)
892+
const devServer = spawn(npmPath, command.split(' '), { cwd, env });
886893
devServer.stdout.on('data', (data) => {
887894
if (data.includes('➜')) {
888895
// TODO: maybe better use our string "App port: 5174. HMR port: 5274", it is more reliable because vue might change their output

0 commit comments

Comments
 (0)