Skip to content

Commit

Permalink
feat: Search for Node executable additionally in paths of PATH envi…
Browse files Browse the repository at this point in the history
…ronment variable
  • Loading branch information
DevDavido committed Dec 24, 2023
1 parent b254975 commit 55e5fff
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 23 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Changelog
## 3.1.0
- Added: Search for Node executable additionally in paths of `PATH` environment variable
- Improved: Provide directory search path in error message if executable cannot be found

## 3.0.0
- Release stable version for Matomo 5
- Fixed: CORS template issue
Expand Down
66 changes: 44 additions & 22 deletions ExecutableFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

require PIWIK_INCLUDE_PATH . '/plugins/PerformanceAudit/vendor/autoload.php';

use Piwik\Container\StaticContainer;
use Piwik\Log\Logger;
use Piwik\Plugins\PerformanceAudit\Exceptions\DependencyMissingException;
use Symfony\Component\Process\ExecutableFinder as BaseExecutableFinder;

Expand All @@ -28,6 +30,9 @@ public static function search($name)
[__DIR__ . DIRECTORY_SEPARATOR . 'node_modules' . DIRECTORY_SEPARATOR . '.bin'],
explode(PATH_SEPARATOR, self::getDefaultPath())
);
StaticContainer::get(Logger::class)->debug('Searching for executable in directories.',
['executable' => $name, 'directories' => $extraDirs]);

$executablePath = (new parent())->find($name, false, $extraDirs);
if (!$executablePath) {
throw new DependencyMissingException($name, $extraDirs);
Expand All @@ -42,28 +47,45 @@ public static function search($name)
* @return string
*/
public static function getDefaultPath() {
return ExecutableFinder::isRunningOnWindows() ?
implode(";", [
'%SystemRoot%\system32',
'%SystemRoot%',
'%SystemRoot%\System32\Wbem'
]) :
implode(":", [
'/usr/local/sbin',
'/usr/local/bin',
'/usr/sbin',
'/usr/bin',
'/sbin',
'/bin',
'/opt/plesk/node/24/bin',
'/opt/plesk/node/22/bin',
'/opt/plesk/node/20/bin',
'/opt/plesk/node/18/bin',
'/opt/plesk/node/16/bin',
'/opt/plesk/node/14/bin',
'/opt/plesk/node/12/bin',
'/opt/plesk/node/10/bin'
]);
$searchPaths = ExecutableFinder::isRunningOnWindows() ? [
'%SystemRoot%\system32',
'%SystemRoot%',
'%SystemRoot%\System32\Wbem'
] : [
'/usr/local/sbin',
'/usr/local/bin',
'/usr/sbin',
'/usr/bin',
'/sbin',
'/bin',
'/opt/plesk/node/24/bin',
'/opt/plesk/node/22/bin',
'/opt/plesk/node/20/bin',
'/opt/plesk/node/18/bin',
'/opt/plesk/node/16/bin',
'/opt/plesk/node/14/bin',
'/opt/plesk/node/12/bin',
'/opt/plesk/node/10/bin'
];
$additionalSearchPaths = ExecutableFinder::getPathsFromEnvironmentVariablePath();
$finalSearchPaths = array_unique(array_merge($searchPaths, $additionalSearchPaths));

return implode(PATH_SEPARATOR, $finalSearchPaths);
}

/**
* Return paths as array if `PATH` environment variable is set,
* empty array otherwise.
*
* @return array
*/
private static function getPathsFromEnvironmentVariablePath() {
$envPath = getenv('PATH');
if (!is_string($envPath)) {
return [];
}

return explode(PATH_SEPARATOR, $envPath);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion plugin.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "PerformanceAudit",
"description": "Daily performance audits of all your sites in Matomo.",
"version": "3.0.0",
"version": "3.1.0",
"theme": false,
"require": {
"php": ">=7.2.5",
Expand Down

0 comments on commit 55e5fff

Please sign in to comment.