使用php exec检查npm插件是否存在
我有简单的PHP脚本来检查npm和npm插件。它通过命令行运行良好,但在一定程度上与PHPExec配合使用使用php exec检查npm插件是否存在,php,exec,Php,Exec,我有简单的PHP脚本来检查npm和npm插件。它通过命令行运行良好,但在一定程度上与PHPExec配合使用 $errors = ''; $outPut = null; $exitCode = null; exec('npm -v', $outPut, $exitCode); //<- this works if($exitCode !== 0) { $errors = 'npm is not installed!'; return false; } $outPut = nu
$errors = '';
$outPut = null;
$exitCode = null;
exec('npm -v', $outPut, $exitCode); //<- this works
if($exitCode !== 0) {
$errors = 'npm is not installed!';
return false;
}
$outPut = null;
$exitCode = null;
exec('lessc -v', $outPut, $exitCode); //<- this works
if($exitCode !== 0) {
$errors = 'lessc is not installed!';
return false;
}
//check version of plugin: npm view/info less-plugin-autoprefix version
$outPut = null;
$exitCode = null;
exec('npm info less-plugin-autoprefix version', $outPut, $exitCode); //<- this not works !!!, $exitCode is 7 and $outPut is empty
if($exitCode !== 0) {
$errors = 'lessc plugin autoprefix is not installed!';
return false;
}
是否可以通过编程方式进行修复?
我不知道是否有一些问题,但在命令行
“npm info less plugin autoprefix version”
中运行良好,没有错误。将错误输出重定向到标准输出:
npm info less-plugin-autoprefix version 2>&1
这样,经过一些实验,我得到了有效的解决方案,错误也将出现在
$outPut
数组中。问题是Windows平台的工作方式不同
$c = get_defined_constants();
if(isset($c['PHP_OS']) && (strtoupper($c['PHP_OS']) === 'WINNT' || strtoupper($c['PHP_OS']) === 'WIN32')) {
define('OS', 'WIN');
}
else {
define('OS', 'LIN');
}
function array_search_partial($arr, $keyword) {
foreach($arr as $index => $string) {
if (strpos($string, $keyword) !== FALSE)
return $string;
}
}
$outPut = null;
$exitCode = null;
$appdata = getenv('APPDATA');
if(!$appdata) {
if(OS === 'WIN') {
$paths = $_SERVER['PATH'];
if($paths) {
//C:\Users\my_user_name\AppData\Roaming\npm
$userPath = array_search_partial(explode(';', $paths), 'AppData\Roaming\npm');
$userPath = mb_substr($userPath, 0, -4);
putenv("APPDATA=$userPath");
}
}
else {
}
}
然后命令:
exec('npm info less-plugin-autoprefix version', $outPut, $exitCode);
一切正常
exec('npm info less-plugin-autoprefix version', $outPut, $exitCode);