Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
C
cpas6-install
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
李金钢
cpas6-install
Commits
ec362f68
Commit
ec362f68
authored
Mar 19, 2026
by
Auto Backup
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: 修改命令执行函数,使用父进程的shell执行命令
parent
adf6cd49
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
90 additions
and
8 deletions
+90
-8
check-environment.js
check-environment.js
+90
-8
No files found.
check-environment.js
View file @
ec362f68
#!/usr/bin/env node
const
{
exec
,
spawn
}
=
require
(
'child_process'
);
const
{
spawn
,
exec
}
=
require
(
'child_process'
);
const
{
promisify
}
=
require
(
'util'
);
const
execAsync
=
promisify
(
exec
);
const
fs
=
require
(
'fs'
);
const
os
=
require
(
'os'
);
const
path
=
require
(
'path'
);
// Platform detection
const
isWindows
=
os
.
platform
()
===
'win32'
;
...
...
@@ -14,19 +15,100 @@ const isLinux = os.platform() === 'linux';
console
.
log
(
`Platform:
${
os
.
platform
()}
(
${
isWindows
?
'Windows'
:
isMacOS
?
'macOS'
:
isLinux
?
'Linux'
:
'Other'
}
)`
);
/**
* Execute command and return result
* @param {string} command - Command to execute
* @returns {Promise<{success: boolean, stdout: string, stderr: string}>}
* 获取父进程的 shell 路径
*/
function
getParentShell
()
{
try
{
const
parentPid
=
process
.
ppid
;
if
(
isWindows
)
{
// Windows: 使用 PowerShell 获取父进程信息
return
new
Promise
((
resolve
)
=>
{
exec
(
`wmic process where ProcessId=
${
parentPid
}
get ExecutablePath`
,
(
error
,
stdout
)
=>
{
if
(
error
)
{
console
.
log
(
'⚠️ 无法获取父进程路径,使用默认 shell'
);
resolve
(
null
);
return
;
}
const
lines
=
stdout
.
trim
().
split
(
'
\
n'
);
if
(
lines
.
length
>=
2
)
{
const
parentPath
=
lines
[
1
].
trim
();
// 检查是否是 PowerShell
if
(
parentPath
.
toLowerCase
().
includes
(
'powershell'
)
||
parentPath
.
toLowerCase
().
includes
(
'pwsh'
))
{
resolve
(
parentPath
);
}
else
{
resolve
(
null
);
}
}
else
{
resolve
(
null
);
}
});
});
}
else
{
// Unix: 通过 /proc/<pid>/exe 获取
return
new
Promise
((
resolve
)
=>
{
fs
.
readlink
(
`/proc/
${
parentPid
}
/exe`
,
(
error
,
link
)
=>
{
if
(
error
)
{
console
.
log
(
'⚠️ 无法获取父进程路径,使用默认 shell'
);
resolve
(
null
);
return
;
}
resolve
(
link
);
});
});
}
}
catch
(
error
)
{
console
.
log
(
`⚠️ 获取父进程 shell 失败:
${
error
.
message
}
`
);
return
Promise
.
resolve
(
null
);
}
}
/**
* 使用指定 shell 执行命令
*/
function
execWithShell
(
command
,
shellPath
)
{
return
new
Promise
((
resolve
,
reject
)
=>
{
const
options
=
{
shell
:
shellPath
||
true
,
windowsHide
:
true
};
exec
(
command
,
options
,
(
error
,
stdout
,
stderr
)
=>
{
if
(
error
)
{
reject
({
stdout
:
stdout
.
trim
(),
stderr
:
stderr
.
trim
(),
error
:
error
.
message
});
}
else
{
resolve
({
stdout
:
stdout
.
trim
(),
stderr
:
stderr
.
trim
()
});
}
});
});
}
/**
* Execute command using parent's shell if possible
*/
async
function
executeCommand
(
command
)
{
try
{
const
{
stdout
,
stderr
}
=
await
execAsync
(
command
);
return
{
success
:
true
,
stdout
:
stdout
.
trim
(),
stderr
:
stderr
.
trim
()
};
let
parentShell
=
null
;
if
(
isWindows
)
{
try
{
parentShell
=
await
getParentShell
();
if
(
parentShell
)
{
console
.
log
(
`🔍 父进程 shell:
${
parentShell
}
`
);
}
}
catch
(
e
)
{
// 忽略错误,使用默认
}
}
const
result
=
await
execWithShell
(
command
,
parentShell
);
return
{
success
:
true
,
stdout
:
result
.
stdout
,
stderr
:
result
.
stderr
};
}
catch
(
error
)
{
return
{
success
:
false
,
stdout
:
error
.
stdout
?
error
.
stdout
.
trim
()
:
''
,
stderr
:
error
.
stderr
?
error
.
stderr
.
trim
()
:
error
.
message
stdout
:
error
.
stdout
||
''
,
stderr
:
error
.
stderr
||
error
.
error
||
''
};
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment