-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
NodeDependencyInstaller.php
165 lines (143 loc) · 5.72 KB
/
NodeDependencyInstaller.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
namespace Piwik\Plugins\PerformanceAudit;
require PIWIK_INCLUDE_PATH . '/plugins/PerformanceAudit/vendor/autoload.php';
use Exception;
use Piwik\Container\StaticContainer;
use Piwik\Log\Logger;
use Piwik\Plugins\PerformanceAudit\Exceptions\DependencyNpmMisconfigurationException;
use Piwik\Plugins\PerformanceAudit\Exceptions\DependencyOfChromeMissingException;
use Piwik\Plugins\PerformanceAudit\Exceptions\DependencyUnexpectedResultException;
use Piwik\Plugins\PerformanceAudit\Exceptions\InstallationFailedException;
use Piwik\Plugins\PerformanceAudit\Exceptions\InternetUnavailableException;
use Piwik\SettingsPiwik;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
class NodeDependencyInstaller
{
const MINIMUM_NPM_VERSION = 6.13;
const MINIMUM_CHROME_VERSION = 54.0;
/**
* Run checks and if no error occurs install dependencies.
*
* @return bool
*/
public function install()
{
try {
Helper::checkDirectoriesWriteable(['Audits', 'node_modules']);
$this->checkInternetAvailability();
$this->checkNpm();
$this->installNpmDependencies();
$this->checkNpmDependencies();
} catch (Exception $exception) {
StaticContainer::get(Logger::class)->error('Unable to install Node dependencies.', ['exception' => $exception]);
throw new InstallationFailedException('Node.js dependency installation failed due to the following error: ' . PHP_EOL . $exception->getMessage());
}
return true;
}
/**
* Uninstall dependencies.
*
* @return bool
*/
public function uninstall()
{
$filesToDelete =
new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(__DIR__ . DIRECTORY_SEPARATOR . 'node_modules', RecursiveDirectoryIterator::CURRENT_AS_FILEINFO | RecursiveDirectoryIterator::KEY_AS_PATHNAME | RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($filesToDelete as $file) {
if ($file->getFilename() === '.gitkeep') {
continue;
}
$action = ($file->isFile() || $file->isLink()) ? 'unlink' : 'rmdir';
if (!$action($file->getPathname())) {
return false;
}
}
unlink(__DIR__ . DIRECTORY_SEPARATOR . 'package-lock.json');
return true;
}
/**
* Check if NPM (from Node.js) is installed.
*
* @return void
* @throws DependencyUnexpectedResultException
*/
public function checkNpm()
{
$npmVersion = $this->checkDependency('npm', ['-v']);
if ($npmVersion < self::MINIMUM_NPM_VERSION) {
throw new DependencyUnexpectedResultException('npm needs to be at least v' . self::MINIMUM_NPM_VERSION . ' but v' . $npmVersion . ' is installed instead.');
}
}
/**
* Check if Chrome is properly installed.
*
* @return void
* @throws DependencyUnexpectedResultException
*/
public function checkNpmDependencies()
{
$lighthouse = new Lighthouse();
$chromeVersion = $this->checkDependency($lighthouse->getChromePath(), ['--version']);
if ($chromeVersion < self::MINIMUM_CHROME_VERSION) {
throw new DependencyUnexpectedResultException('Chrome needs to be at least v' . self::MINIMUM_CHROME_VERSION . ' but v' . $chromeVersion . ' is installed instead.');
}
}
/**
* Check if dependency is installed.
*
* @param string $executableName
* @param array $args
* @return float
* @throws DependencyUnexpectedResultException
*/
private function checkDependency($executableName, $args)
{
$executablePath = ExecutableFinder::search($executableName);
$process = new Process(array_merge([$executablePath], $args));
$process->run();
if (!$process->isSuccessful()) {
$errorOutput = $process->getErrorOutput();
if (stristr($errorOutput, 'libX11-xcb')) {
throw new DependencyOfChromeMissingException();
} elseif (stristr($errorOutput, 'cache folder contains root-owned files')) {
throw new DependencyNpmMisconfigurationException($executableName . ' has the following issue: ' . PHP_EOL . $errorOutput);
}
throw new DependencyUnexpectedResultException(ucfirst($executableName) . ' has the following unexpected output: ' . PHP_EOL . $errorOutput);
}
return floatval(trim(preg_replace('/[^0-9.]/', '', $process->getOutput())));
}
/**
* Install Puppeteer + Lighthouse and its dependencies.
*
* @return string
* @throws DependencyUnexpectedResultException
*/
private function installNpmDependencies()
{
$npmPath = ExecutableFinder::search('npm');
// Puppeteer + Lighthouse
$process = new Process([$npmPath, 'install', '--quiet', '--no-progress', '--no-audit', '--force', '--only=production', '--prefix=' . __DIR__, 'puppeteer@^3.0', 'lighthouse@^6.0']);
$process->setTimeout(300);
$process->run();
if (!$process->isSuccessful()) {
throw new DependencyUnexpectedResultException('NPM has the following unexpected output: ' . PHP_EOL . $process->getErrorOutput());
}
return trim($process->getOutput());
}
/**
* Check if internet connection is required.
*
* @return void
* @throws InternetUnavailableException
*/
public function checkInternetAvailability()
{
if (!SettingsPiwik::isInternetEnabled()) {
throw new InternetUnavailableException('Internet needs to be enabled in order to install plugin dependencies.');
}
}
}